27
27
//!
28
28
//! It is not recommended to use this type for idiomatic rust use. These types
29
29
//! 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.
31
32
//!
32
33
//! # Example
33
34
//!
34
- //! use std::unstable::mutex::{StaticNativeMutex, NATIVE_MUTEX_INIT};
35
+ //! ```rust
36
+ //! use std::unstable::mutex::{NativeMutex, StaticNativeMutex, NATIVE_MUTEX_INIT};
35
37
//!
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;
38
40
//!
39
- //! unsafe {
40
- //! let _guard = LOCK.lock();
41
- //! } // automatically unlocked here
41
+ //! unsafe {
42
+ //! let _guard = LOCK.lock();
43
+ //! } // automatically unlocked here
42
44
//!
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();
46
48
//!
47
- //! {
48
- //! let _guard = lock.lock();
49
- //! } // unlocked here
49
+ //! {
50
+ //! let _guard = lock.lock();
51
+ //! } // unlocked here
50
52
//!
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
+ //! ```
55
58
56
59
#[ allow( non_camel_case_types) ] ;
57
60
@@ -61,7 +64,8 @@ use ops::Drop;
61
64
/// A native mutex suitable for storing in statics (that is, it has
62
65
/// the `destroy` method rather than a destructor).
63
66
///
64
- /// Prefer the `NativeMutex` type where possible.
67
+ /// Prefer the `NativeMutex` type where possible, since that does not
68
+ /// require manual deallocation.
65
69
pub struct StaticNativeMutex {
66
70
priv inner : imp:: Mutex ,
67
71
}
@@ -128,14 +132,16 @@ impl StaticNativeMutex {
128
132
129
133
/// Acquire the lock without creating a `LockGuard`.
130
134
///
131
- /// Prefer using `.lock`.
135
+ /// These needs to be paired with a call to `.unlock_noguard`. Prefer using
136
+ /// `.lock`.
132
137
pub unsafe fn lock_noguard ( & mut self ) { self . inner . lock ( ) }
133
138
134
139
/// Attempts to acquire the lock without creating a
135
140
/// `LockGuard`. The value returned is whether the lock was
136
141
/// acquired or not.
137
142
///
138
- /// Prefer using `.trylock`.
143
+ /// If `true` is returned, this needs to be paired with a call to
144
+ /// `.unlock_noguard`. Prefer using `.trylock`.
139
145
pub unsafe fn trylock_noguard ( & mut self ) -> bool {
140
146
self . inner . trylock ( )
141
147
}
@@ -175,11 +181,14 @@ impl NativeMutex {
175
181
/// # Example
176
182
/// ```rust
177
183
/// use std::unstable::mutex::NativeMutex;
178
- /// let mut lock = NativeMutex::new();
179
184
/// 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
+ /// }
183
192
/// ```
184
193
pub unsafe fn lock < ' a > ( & ' a mut self ) -> LockGuard < ' a > {
185
194
self . inner . lock ( )
@@ -193,14 +202,16 @@ impl NativeMutex {
193
202
194
203
/// Acquire the lock without creating a `LockGuard`.
195
204
///
196
- /// Prefer using `.lock`.
205
+ /// These needs to be paired with a call to `.unlock_noguard`. Prefer using
206
+ /// `.lock`.
197
207
pub unsafe fn lock_noguard ( & mut self ) { self . inner . lock_noguard ( ) }
198
208
199
209
/// Attempts to acquire the lock without creating a
200
210
/// `LockGuard`. The value returned is whether the lock was
201
211
/// acquired or not.
202
212
///
203
- /// Prefer using `.trylock`.
213
+ /// If `true` is returned, this needs to be paired with a call to
214
+ /// `.unlock_noguard`. Prefer using `.trylock`.
204
215
pub unsafe fn trylock_noguard ( & mut self ) -> bool {
205
216
self . inner . trylock_noguard ( )
206
217
}
0 commit comments