diff options
| author | Huon Wilson <dbau.pp+github@gmail.com> | 2014-02-15 11:18:49 +1100 |
|---|---|---|
| committer | Huon Wilson <dbau.pp+github@gmail.com> | 2014-02-16 10:13:56 +1100 |
| commit | b87ed605c0ef27f5532ec30c92128bd890de5955 (patch) | |
| tree | 55810612bec1de77a67738853ea98539582fa809 /src/libstd | |
| parent | 75d92dbabe5bab3a1ca85c305a3773bca2e38145 (diff) | |
| download | rust-b87ed605c0ef27f5532ec30c92128bd890de5955.tar.gz rust-b87ed605c0ef27f5532ec30c92128bd890de5955.zip | |
std: Rename unstable::mutex::Mutex to StaticNativeMutex.
This better reflects its purpose and design.
Diffstat (limited to 'src/libstd')
| -rw-r--r-- | src/libstd/comm/shared.rs | 6 | ||||
| -rw-r--r-- | src/libstd/os.rs | 4 | ||||
| -rw-r--r-- | src/libstd/rt/args.rs | 4 | ||||
| -rw-r--r-- | src/libstd/unstable/dynamic_lib.rs | 4 | ||||
| -rw-r--r-- | src/libstd/unstable/mutex.rs | 70 | ||||
| -rw-r--r-- | src/libstd/unstable/sync.rs | 6 |
6 files changed, 49 insertions, 45 deletions
diff --git a/src/libstd/comm/shared.rs b/src/libstd/comm/shared.rs index fcd00b70dd1..61fc700c3c0 100644 --- a/src/libstd/comm/shared.rs +++ b/src/libstd/comm/shared.rs @@ -28,7 +28,7 @@ use rt::local::Local; use rt::task::{Task, BlockedTask}; use rt::thread::Thread; use sync::atomics; -use unstable::mutex::Mutex; +use unstable::mutex::StaticNativeMutex; use vec::OwnedVector; use mpsc = sync::mpsc_queue; @@ -53,7 +53,7 @@ pub struct Packet<T> { // this lock protects various portions of this implementation during // select() - select_lock: Mutex, + select_lock: StaticNativeMutex, } pub enum Failure { @@ -72,7 +72,7 @@ impl<T: Send> Packet<T> { channels: atomics::AtomicInt::new(2), port_dropped: atomics::AtomicBool::new(false), sender_drain: atomics::AtomicInt::new(0), - select_lock: unsafe { Mutex::new() }, + select_lock: unsafe { StaticNativeMutex::new() }, }; // see comments in inherit_blocker about why we grab this lock unsafe { p.select_lock.lock_noguard() } diff --git a/src/libstd/os.rs b/src/libstd/os.rs index 4a5958c2cfb..719ed62d03d 100644 --- a/src/libstd/os.rs +++ b/src/libstd/os.rs @@ -144,9 +144,9 @@ Accessing environment variables is not generally threadsafe. Serialize access through a global lock. */ fn with_env_lock<T>(f: || -> T) -> T { - use unstable::mutex::{Mutex, MUTEX_INIT}; + use unstable::mutex::{StaticNativeMutex, NATIVE_MUTEX_INIT}; - static mut lock: Mutex = MUTEX_INIT; + static mut lock: StaticNativeMutex = NATIVE_MUTEX_INIT; unsafe { let _guard = lock.lock(); diff --git a/src/libstd/rt/args.rs b/src/libstd/rt/args.rs index c91797c9559..6f73265978b 100644 --- a/src/libstd/rt/args.rs +++ b/src/libstd/rt/args.rs @@ -68,11 +68,11 @@ mod imp { use option::{Option, Some, None}; use ptr::RawPtr; use iter::Iterator; - use unstable::mutex::{Mutex, MUTEX_INIT}; + use unstable::mutex::{StaticNativeMutex, NATIVE_MUTEX_INIT}; use mem; static mut global_args_ptr: uint = 0; - static mut lock: Mutex = MUTEX_INIT; + static mut lock: StaticNativeMutex = NATIVE_MUTEX_INIT; #[cfg(not(test))] pub unsafe fn init(argc: int, argv: **u8) { diff --git a/src/libstd/unstable/dynamic_lib.rs b/src/libstd/unstable/dynamic_lib.rs index 4828c4ee5af..84fa528ebf1 100644 --- a/src/libstd/unstable/dynamic_lib.rs +++ b/src/libstd/unstable/dynamic_lib.rs @@ -152,8 +152,8 @@ pub mod dl { } pub fn check_for_errors_in<T>(f: || -> T) -> Result<T, ~str> { - use unstable::mutex::{Mutex, MUTEX_INIT}; - static mut lock: Mutex = MUTEX_INIT; + use unstable::mutex::{StaticNativeMutex, NATIVE_MUTEX_INIT}; + static mut lock: StaticNativeMutex = NATIVE_MUTEX_INIT; unsafe { // dlerror isn't thread safe, so we need to lock around this entire // sequence diff --git a/src/libstd/unstable/mutex.rs b/src/libstd/unstable/mutex.rs index f0e76de789d..d8d051236b3 100644 --- a/src/libstd/unstable/mutex.rs +++ b/src/libstd/unstable/mutex.rs @@ -11,9 +11,9 @@ //! A native mutex and condition variable type //! //! This module contains bindings to the platform's native mutex/condition -//! variable primitives. It provides a single type, `Mutex`, which can be -//! statically initialized via the `MUTEX_INIT` value. This object serves as both a -//! mutex and a condition variable simultaneously. +//! variable primitives. It provides a single type, `StaticNativeMutex`, which can be +//! statically initialized via the `NATIVE_MUTEX_INIT` value. This object serves as +//! both a mutex and a condition variable simultaneously. //! //! The lock is lazily initialized, but it can only be unsafely destroyed. A //! statically initialized lock doesn't necessarily have a time at which it can @@ -27,21 +27,23 @@ //! //! # Example //! -//! use std::unstable::mutex::{Mutex, MUTEX_INIT}; +//! use std::unstable::mutex::{StaticNativeMutex, NATIVE_MUTEX_INIT}; //! //! // Use a statically initialized mutex -//! static mut lock: Mutex = MUTEX_INIT; +//! static mut lock: StaticNativeMutex = NATIVE_MUTEX_INIT; //! //! unsafe { -//! lock.lock(); -//! lock.unlock(); -//! } +//! let _guard = lock.lock(); +//! } // automatically unlocked here //! //! // Use a normally initialized mutex -//! let mut lock = Mutex::new(); //! unsafe { -//! lock.lock(); -//! lock.unlock(); +//! let mut lock = StaticNativeMutex::new(); +//! +//! // sometimes the RAII guard isn't appropriate +//! lock.lock_noguard(); +//! lock.unlock_noguard(); +//! //! lock.destroy(); //! } @@ -50,7 +52,9 @@ use option::{Option, None, Some}; use ops::Drop; -pub struct Mutex { +/// A native mutex suitable for storing in statics (that is, it has +/// the `destroy` method rather than a destructor). +pub struct StaticNativeMutex { priv inner: imp::Mutex, } @@ -62,33 +66,33 @@ pub struct Mutex { /// then. #[must_use] pub struct LockGuard<'a> { - priv lock: &'a mut Mutex + priv lock: &'a mut StaticNativeMutex } -pub static MUTEX_INIT: Mutex = Mutex { +pub static NATIVE_MUTEX_INIT: StaticNativeMutex = StaticNativeMutex { inner: imp::MUTEX_INIT, }; -impl Mutex { - /// Creates a new mutex - pub unsafe fn new() -> Mutex { - Mutex { inner: imp::Mutex::new() } +impl StaticNativeMutex { + /// Creates a new mutex. + /// + /// Note that a mutex created in this way needs to be explicit + /// freed with a call to `destroy` or it will leak. + pub unsafe fn new() -> StaticNativeMutex { + StaticNativeMutex { inner: imp::Mutex::new() } } /// Acquires this lock. This assumes that the current thread does not /// already hold the lock. /// /// # Example - /// ``` - /// use std::unstable::mutex::Mutex; + /// ```rust + /// use std::unstable::mutex::{StaticNativeMutex, NATIVE_MUTEX_INIT}; + /// static mut LOCK: StaticNativeMutex = NATIVE_MUTEX_INIT; /// unsafe { - /// let mut lock = Mutex::new(); - /// - /// { - /// let _guard = lock.lock(); - /// // critical section... - /// } // automatically unlocked in `_guard`'s destructor - /// } + /// let _guard = LOCK.lock(); + /// // critical section... + /// } // automatically unlocked in `_guard`'s destructor /// ``` pub unsafe fn lock<'a>(&'a mut self) -> LockGuard<'a> { self.inner.lock(); @@ -455,12 +459,12 @@ mod test { use prelude::*; use mem::drop; - use super::{Mutex, MUTEX_INIT}; + use super::{StaticNativeMutex, NATIVE_MUTEX_INIT}; use rt::thread::Thread; #[test] fn smoke_lock() { - static mut lock: Mutex = MUTEX_INIT; + static mut lock: StaticNativeMutex = NATIVE_MUTEX_INIT; unsafe { let _guard = lock.lock(); } @@ -468,7 +472,7 @@ mod test { #[test] fn smoke_cond() { - static mut lock: Mutex = MUTEX_INIT; + static mut lock: StaticNativeMutex = NATIVE_MUTEX_INIT; unsafe { let mut guard = lock.lock(); let t = Thread::start(proc() { @@ -484,7 +488,7 @@ mod test { #[test] fn smoke_lock_noguard() { - static mut lock: Mutex = MUTEX_INIT; + static mut lock: StaticNativeMutex = NATIVE_MUTEX_INIT; unsafe { lock.lock_noguard(); lock.unlock_noguard(); @@ -493,7 +497,7 @@ mod test { #[test] fn smoke_cond_noguard() { - static mut lock: Mutex = MUTEX_INIT; + static mut lock: StaticNativeMutex = NATIVE_MUTEX_INIT; unsafe { lock.lock_noguard(); let t = Thread::start(proc() { @@ -511,7 +515,7 @@ mod test { #[test] fn destroy_immediately() { unsafe { - let mut m = Mutex::new(); + let mut m = StaticNativeMutex::new(); m.destroy(); } } diff --git a/src/libstd/unstable/sync.rs b/src/libstd/unstable/sync.rs index 343eacbf429..0a6de50bf23 100644 --- a/src/libstd/unstable/sync.rs +++ b/src/libstd/unstable/sync.rs @@ -13,10 +13,10 @@ use kinds::Send; use ops::Drop; use option::Option; use sync::arc::UnsafeArc; -use unstable::mutex::{Mutex, LockGuard}; +use unstable::mutex::{StaticNativeMutex, LockGuard}; pub struct LittleLock { - priv l: Mutex, + priv l: StaticNativeMutex, } pub struct LittleGuard<'a> { @@ -31,7 +31,7 @@ impl Drop for LittleLock { impl LittleLock { pub fn new() -> LittleLock { - unsafe { LittleLock { l: Mutex::new() } } + unsafe { LittleLock { l: StaticNativeMutex::new() } } } pub unsafe fn lock<'a>(&'a mut self) -> LittleGuard<'a> { |
