diff options
| author | Huon Wilson <dbau.pp+github@gmail.com> | 2014-02-13 17:17:50 +1100 |
|---|---|---|
| committer | Huon Wilson <dbau.pp+github@gmail.com> | 2014-02-16 10:13:56 +1100 |
| commit | 76a59fd6e2d5c8c42193c047fd5eaba982d499f7 (patch) | |
| tree | a913c967de98b492f47fdd0bbd5a11cf0be96ed5 /src/libstd/unstable/sync.rs | |
| parent | fba32ea79f1828ef441d91abca3635fad57f323d (diff) | |
std: add an RAII unlocker to Mutex.
This automatically unlocks its lock when it goes out of scope, and provides a safe(ish) method to call .wait.
Diffstat (limited to 'src/libstd/unstable/sync.rs')
| -rw-r--r-- | src/libstd/unstable/sync.rs | 24 |
1 files changed, 6 insertions, 18 deletions
diff --git a/src/libstd/unstable/sync.rs b/src/libstd/unstable/sync.rs index c2fa168a478..343eacbf429 100644 --- a/src/libstd/unstable/sync.rs +++ b/src/libstd/unstable/sync.rs @@ -11,16 +11,16 @@ use clone::Clone; use kinds::Send; use ops::Drop; -use option::{Option,Some,None}; +use option::Option; use sync::arc::UnsafeArc; -use unstable::mutex::Mutex; +use unstable::mutex::{Mutex, LockGuard}; pub struct LittleLock { priv l: Mutex, } pub struct LittleGuard<'a> { - priv l: &'a mut Mutex, + priv l: LockGuard<'a> } impl Drop for LittleLock { @@ -29,33 +29,21 @@ impl Drop for LittleLock { } } -#[unsafe_destructor] -impl<'a> Drop for LittleGuard<'a> { - fn drop(&mut self) { - unsafe { self.l.unlock(); } - } -} - impl LittleLock { pub fn new() -> LittleLock { unsafe { LittleLock { l: Mutex::new() } } } pub unsafe fn lock<'a>(&'a mut self) -> LittleGuard<'a> { - self.l.lock(); - LittleGuard { l: &mut self.l } + LittleGuard { l: self.l.lock() } } pub unsafe fn try_lock<'a>(&'a mut self) -> Option<LittleGuard<'a>> { - if self.l.trylock() { - Some(LittleGuard { l: &mut self.l }) - } else { - None - } + self.l.trylock().map(|guard| LittleGuard { l: guard }) } pub unsafe fn signal(&mut self) { - self.l.signal(); + self.l.signal_noguard(); } } |
