diff options
| author | Flavio Percoco <flaper87@gmail.com> | 2014-12-23 20:21:14 +0100 |
|---|---|---|
| committer | Flavio Percoco <flaper87@gmail.com> | 2014-12-26 17:26:33 +0100 |
| commit | d35ebcb483722303d21882b369c315449457147f (patch) | |
| tree | 5f794d327edfc2b52aac23fc80f88abec72dc0e5 | |
| parent | 607f60712c7c9d3b96a162d2605a89ff2554fc7e (diff) | |
| download | rust-d35ebcb483722303d21882b369c315449457147f.tar.gz rust-d35ebcb483722303d21882b369c315449457147f.zip | |
Make Barrier and Condvar Sync/Send
| -rw-r--r-- | src/libstd/c_vec.rs | 8 | ||||
| -rw-r--r-- | src/libstd/sync/barrier.rs | 7 | ||||
| -rw-r--r-- | src/libstd/sync/condvar.rs | 6 | ||||
| -rw-r--r-- | src/libstd/sync/mutex.rs | 21 | ||||
| -rw-r--r-- | src/libstd/sync/rwlock.rs | 6 |
5 files changed, 36 insertions, 12 deletions
diff --git a/src/libstd/c_vec.rs b/src/libstd/c_vec.rs index 44e7291150e..f4338815f75 100644 --- a/src/libstd/c_vec.rs +++ b/src/libstd/c_vec.rs @@ -180,12 +180,12 @@ mod tests { fn malloc(n: uint) -> CVec<u8> { unsafe { - let mem = libc::malloc(n as libc::size_t); - if mem.is_null() { ::alloc::oom() } + let mem = ptr::Unique(libc::malloc(n as libc::size_t)); + if mem.0.is_null() { ::alloc::oom() } - CVec::new_with_dtor(mem as *mut u8, + CVec::new_with_dtor(mem.0 as *mut u8, n, - move|| { libc::free(mem as *mut libc::c_void); }) + move|| { libc::free(mem.0 as *mut libc::c_void); }) } } diff --git a/src/libstd/sync/barrier.rs b/src/libstd/sync/barrier.rs index 6573d9273ce..6cdb199819a 100644 --- a/src/libstd/sync/barrier.rs +++ b/src/libstd/sync/barrier.rs @@ -8,6 +8,7 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. +use kinds::{Send, Sync}; use sync::{Mutex, Condvar}; /// A barrier enables multiple tasks to synchronize the beginning @@ -35,12 +36,18 @@ pub struct Barrier { num_threads: uint, } +unsafe impl Send for Barrier {} +unsafe impl Sync for Barrier {} + // The inner state of a double barrier struct BarrierState { count: uint, generation_id: uint, } +unsafe impl Send for BarrierState {} +unsafe impl Sync for BarrierState {} + impl Barrier { /// Create a new barrier that can block a given number of threads. /// diff --git a/src/libstd/sync/condvar.rs b/src/libstd/sync/condvar.rs index be27c06b83c..f1940bfd829 100644 --- a/src/libstd/sync/condvar.rs +++ b/src/libstd/sync/condvar.rs @@ -58,6 +58,9 @@ use time::Duration; /// ``` pub struct Condvar { inner: Box<StaticCondvar> } +unsafe impl Send for Condvar {} +unsafe impl Sync for Condvar {} + /// Statically allocated condition variables. /// /// This structure is identical to `Condvar` except that it is suitable for use @@ -75,6 +78,9 @@ pub struct StaticCondvar { mutex: AtomicUint, } +unsafe impl Send for StaticCondvar {} +unsafe impl Sync for StaticCondvar {} + /// Constant initializer for a statically allocated condition variable. pub const CONDVAR_INIT: StaticCondvar = StaticCondvar { inner: sys::CONDVAR_INIT, diff --git a/src/libstd/sync/mutex.rs b/src/libstd/sync/mutex.rs index fbbbb3e77a4..4d2fbfc4055 100644 --- a/src/libstd/sync/mutex.rs +++ b/src/libstd/sync/mutex.rs @@ -284,6 +284,11 @@ mod test { use thread::Thread; use sync::{Arc, Mutex, StaticMutex, MUTEX_INIT, Condvar}; + struct Packet<T>(Arc<(Mutex<T>, Condvar)>); + + unsafe impl<T:'static+Send> Send for Packet<T> {} + unsafe impl<T> Sync for Packet<T> {} + #[test] fn smoke() { let m = Mutex::new(()); @@ -343,19 +348,19 @@ mod test { #[test] fn test_mutex_arc_condvar() { - let arc = Arc::new((Mutex::new(false), Condvar::new())); - let arc2 = arc.clone(); + let packet = Packet(Arc::new((Mutex::new(false), Condvar::new()))); + let packet2 = Packet(packet.0.clone()); let (tx, rx) = channel(); spawn(move|| { // wait until parent gets in rx.recv(); - let &(ref lock, ref cvar) = &*arc2; + let &(ref lock, ref cvar) = &*packet2.0; let mut lock = lock.lock(); *lock = true; cvar.notify_one(); }); - let &(ref lock, ref cvar) = &*arc; + let &(ref lock, ref cvar) = &*packet.0; let lock = lock.lock(); tx.send(()); assert!(!*lock); @@ -367,20 +372,20 @@ mod test { #[test] #[should_fail] fn test_arc_condvar_poison() { - let arc = Arc::new((Mutex::new(1i), Condvar::new())); - let arc2 = arc.clone(); + let packet = Packet(Arc::new((Mutex::new(1i), Condvar::new()))); + let packet2 = Packet(packet.0.clone()); let (tx, rx) = channel(); spawn(move|| { rx.recv(); - let &(ref lock, ref cvar) = &*arc2; + let &(ref lock, ref cvar) = &*packet2.0; let _g = lock.lock(); cvar.notify_one(); // Parent should fail when it wakes up. panic!(); }); - let &(ref lock, ref cvar) = &*arc; + let &(ref lock, ref cvar) = &*packet.0; let lock = lock.lock(); tx.send(()); while *lock == 1 { diff --git a/src/libstd/sync/rwlock.rs b/src/libstd/sync/rwlock.rs index 3f177a42f44..76d05d9bfd4 100644 --- a/src/libstd/sync/rwlock.rs +++ b/src/libstd/sync/rwlock.rs @@ -60,6 +60,9 @@ pub struct RWLock<T> { data: UnsafeCell<T>, } +unsafe impl<T:'static+Send> Send for RWLock<T> {} +unsafe impl<T> Sync for RWLock<T> {} + /// Structure representing a statically allocated RWLock. /// /// This structure is intended to be used inside of a `static` and will provide @@ -88,6 +91,9 @@ pub struct StaticRWLock { poison: UnsafeCell<poison::Flag>, } +unsafe impl Send for StaticRWLock {} +unsafe impl Sync for StaticRWLock {} + /// Constant initialization for a statically-initialized rwlock. pub const RWLOCK_INIT: StaticRWLock = StaticRWLock { inner: sys::RWLOCK_INIT, |
