Skip to content

Commit 5d86e24

Browse files
committed
std::unstable::mutex: streamline & clarify documentation.
1 parent 0f4294b commit 5d86e24

File tree

1 file changed

+37
-26
lines changed

1 file changed

+37
-26
lines changed

src/libstd/unstable/mutex.rs

+37-26
Original file line numberDiff line numberDiff line change
@@ -27,31 +27,34 @@
2727
//!
2828
//! It is not recommended to use this type for idiomatic rust use. These types
2929
//! are appropriate where no other options are available, but other rust
30-
//! concurrency primitives should be used before them.
30+
//! concurrency primitives should be used before them: the `sync` crate defines
31+
//! `StaticMutex` and `Mutex` types.
3132
//!
3233
//! # Example
3334
//!
34-
//! use std::unstable::mutex::{StaticNativeMutex, NATIVE_MUTEX_INIT};
35+
//! ```rust
36+
//! use std::unstable::mutex::{NativeMutex, StaticNativeMutex, NATIVE_MUTEX_INIT};
3537
//!
36-
//! // Use a statically initialized mutex
37-
//! static mut LOCK: StaticNativeMutex = NATIVE_MUTEX_INIT;
38+
//! // Use a statically initialized mutex
39+
//! static mut LOCK: StaticNativeMutex = NATIVE_MUTEX_INIT;
3840
//!
39-
//! unsafe {
40-
//! let _guard = LOCK.lock();
41-
//! } // automatically unlocked here
41+
//! unsafe {
42+
//! let _guard = LOCK.lock();
43+
//! } // automatically unlocked here
4244
//!
43-
//! // Use a normally initialized mutex
44-
//! unsafe {
45-
//! let mut lock = NativeMutex::new();
45+
//! // Use a normally initialized mutex
46+
//! unsafe {
47+
//! let mut lock = NativeMutex::new();
4648
//!
47-
//! {
48-
//! let _guard = lock.lock();
49-
//! } // unlocked here
49+
//! {
50+
//! let _guard = lock.lock();
51+
//! } // unlocked here
5052
//!
51-
//! // sometimes the RAII guard isn't appropriate
52-
//! lock.lock_noguard();
53-
//! lock.unlock_noguard();
54-
//! } // `lock` is deallocated here
53+
//! // sometimes the RAII guard isn't appropriate
54+
//! lock.lock_noguard();
55+
//! lock.unlock_noguard();
56+
//! } // `lock` is deallocated here
57+
//! ```
5558
5659
#[allow(non_camel_case_types)];
5760

@@ -61,7 +64,8 @@ use ops::Drop;
6164
/// A native mutex suitable for storing in statics (that is, it has
6265
/// the `destroy` method rather than a destructor).
6366
///
64-
/// Prefer the `NativeMutex` type where possible.
67+
/// Prefer the `NativeMutex` type where possible, since that does not
68+
/// require manual deallocation.
6569
pub struct StaticNativeMutex {
6670
priv inner: imp::Mutex,
6771
}
@@ -128,14 +132,16 @@ impl StaticNativeMutex {
128132

129133
/// Acquire the lock without creating a `LockGuard`.
130134
///
131-
/// Prefer using `.lock`.
135+
/// These needs to be paired with a call to `.unlock_noguard`. Prefer using
136+
/// `.lock`.
132137
pub unsafe fn lock_noguard(&mut self) { self.inner.lock() }
133138

134139
/// Attempts to acquire the lock without creating a
135140
/// `LockGuard`. The value returned is whether the lock was
136141
/// acquired or not.
137142
///
138-
/// Prefer using `.trylock`.
143+
/// If `true` is returned, this needs to be paired with a call to
144+
/// `.unlock_noguard`. Prefer using `.trylock`.
139145
pub unsafe fn trylock_noguard(&mut self) -> bool {
140146
self.inner.trylock()
141147
}
@@ -175,11 +181,14 @@ impl NativeMutex {
175181
/// # Example
176182
/// ```rust
177183
/// use std::unstable::mutex::NativeMutex;
178-
/// let mut lock = NativeMutex::new();
179184
/// unsafe {
180-
/// let _guard = lock.lock();
181-
/// // critical section...
182-
/// } // automatically unlocked in `_guard`'s destructor
185+
/// let mut lock = NativeMutex::new();
186+
///
187+
/// {
188+
/// let _guard = lock.lock();
189+
/// // critical section...
190+
/// } // automatically unlocked in `_guard`'s destructor
191+
/// }
183192
/// ```
184193
pub unsafe fn lock<'a>(&'a mut self) -> LockGuard<'a> {
185194
self.inner.lock()
@@ -193,14 +202,16 @@ impl NativeMutex {
193202

194203
/// Acquire the lock without creating a `LockGuard`.
195204
///
196-
/// Prefer using `.lock`.
205+
/// These needs to be paired with a call to `.unlock_noguard`. Prefer using
206+
/// `.lock`.
197207
pub unsafe fn lock_noguard(&mut self) { self.inner.lock_noguard() }
198208

199209
/// Attempts to acquire the lock without creating a
200210
/// `LockGuard`. The value returned is whether the lock was
201211
/// acquired or not.
202212
///
203-
/// Prefer using `.trylock`.
213+
/// If `true` is returned, this needs to be paired with a call to
214+
/// `.unlock_noguard`. Prefer using `.trylock`.
204215
pub unsafe fn trylock_noguard(&mut self) -> bool {
205216
self.inner.trylock_noguard()
206217
}

0 commit comments

Comments
 (0)