]> git.lizzy.rs Git - rust.git/blobdiff - src/libstd/thread.rs
rollup merge of #20223: aochagavia/typo
[rust.git] / src / libstd / thread.rs
index 8c66fd39c0c37f6b4f32ac19233554dd0d321a88..a7b3ee996a34dc428bf77bb827feceb2ba5742ef 100644 (file)
 use boxed::Box;
 use cell::UnsafeCell;
 use clone::Clone;
-use kinds::Send;
+use kinds::{Send, Sync};
 use ops::{Drop, FnOnce};
 use option::Option::{mod, Some, None};
 use result::Result::{Err, Ok};
@@ -211,8 +211,8 @@ pub fn spawn<T, F>(self, f: F) -> JoinGuard<T> where
     }
 
     fn spawn_inner<T: Send>(self, f: Thunk<(), T>) -> JoinGuard<T> {
-        let my_packet = Arc::new(UnsafeCell::new(None));
-        let their_packet = my_packet.clone();
+        let my_packet = Packet(Arc::new(UnsafeCell::new(None)));
+        let their_packet = Packet(my_packet.0.clone());
 
         let Builder { name, stack_size, stdout, stderr } = self;
 
@@ -266,7 +266,7 @@ fn spawn_inner<T: Send>(self, f: Thunk<(), T>) -> JoinGuard<T> {
                 }
             };
             unsafe {
-                *their_packet.get() = Some(match (output, try_result) {
+                *their_packet.0.get() = Some(match (output, try_result) {
                     (Some(data), Ok(_)) => Ok(data),
                     (None, Err(cause)) => Err(cause),
                     _ => unreachable!()
@@ -289,12 +289,16 @@ struct Inner {
     cvar: Condvar,
 }
 
+unsafe impl Sync for Inner {}
+
 #[deriving(Clone)]
 /// A handle to a thread.
 pub struct Thread {
     inner: Arc<Inner>,
 }
 
+unsafe impl Sync for Thread {}
+
 impl Thread {
     // Used only internally to construct a thread object without spawning
     fn new(name: Option<String>) -> Thread {
@@ -330,6 +334,7 @@ pub fn yield_now() {
     }
 
     /// Determines whether the current thread is panicking.
+    #[inline]
     pub fn panicking() -> bool {
         unwind::panicking()
     }
@@ -345,9 +350,9 @@ pub fn panicking() -> bool {
     // or futuxes, and in either case may allow spurious wakeups.
     pub fn park() {
         let thread = Thread::current();
-        let mut guard = thread.inner.lock.lock();
+        let mut guard = thread.inner.lock.lock().unwrap();
         while !*guard {
-            thread.inner.cvar.wait(&guard);
+            guard = thread.inner.cvar.wait(guard).unwrap();
         }
         *guard = false;
     }
@@ -356,7 +361,7 @@ pub fn park() {
     ///
     /// See the module doc for more detail.
     pub fn unpark(&self) {
-        let mut guard = self.inner.lock.lock();
+        let mut guard = self.inner.lock.lock().unwrap();
         if !*guard {
             *guard = true;
             self.inner.cvar.notify_one();
@@ -379,6 +384,11 @@ fn new(name: Option<String>) -> Thread { Thread::new(name) }
 /// A thread that completes without panicking is considered to exit successfully.
 pub type Result<T> = ::result::Result<T, Box<Any + Send>>;
 
+struct Packet<T>(Arc<UnsafeCell<Option<Result<T>>>>);
+
+unsafe impl<T:'static+Send> Send for Packet<T> {}
+unsafe impl<T> Sync for Packet<T> {}
+
 #[must_use]
 /// An RAII-style guard that will block until thread termination when dropped.
 ///
@@ -387,9 +397,11 @@ pub struct JoinGuard<T> {
     native: imp::rust_thread,
     thread: Thread,
     joined: bool,
-    packet: Arc<UnsafeCell<Option<Result<T>>>>,
+    packet: Packet<T>,
 }
 
+unsafe impl<T: Send> Sync for JoinGuard<T> {}
+
 impl<T: Send> JoinGuard<T> {
     /// Extract a handle to the thread this guard will join on.
     pub fn thread(&self) -> &Thread {
@@ -406,7 +418,7 @@ pub fn join(mut self) -> Result<T> {
         unsafe { imp::join(self.native) };
         self.joined = true;
         unsafe {
-            (*self.packet.get()).take().unwrap()
+            (*self.packet.0.get()).take().unwrap()
         }
     }