@@ -115,6 +115,18 @@ impl StaticNativeMutex {
115
115
/// // critical section...
116
116
/// } // automatically unlocked in `_guard`'s destructor
117
117
/// ```
118
+ ///
119
+ /// # Unsafety
120
+ ///
121
+ /// This method is unsafe because it will not function correctly if this
122
+ /// mutex has been *moved* since it was last used. The mutex can move an
123
+ /// arbitrary number of times before its first usage, but once a mutex has
124
+ /// been used once it is no longer allowed to move (or otherwise it invokes
125
+ /// undefined behavior).
126
+ ///
127
+ /// Additionally, this type does not take into account any form of
128
+ /// scheduling model. This will unconditionally block the *os thread* which
129
+ /// is not always desired.
118
130
pub unsafe fn lock < ' a > ( & ' a self ) -> LockGuard < ' a > {
119
131
self . inner . lock ( ) ;
120
132
@@ -123,6 +135,10 @@ impl StaticNativeMutex {
123
135
124
136
/// Attempts to acquire the lock. The value returned is `Some` if
125
137
/// the attempt succeeded.
138
+ ///
139
+ /// # Unsafety
140
+ ///
141
+ /// This method is unsafe for the same reasons as `lock`.
126
142
pub unsafe fn trylock < ' a > ( & ' a self ) -> Option < LockGuard < ' a > > {
127
143
if self . inner . trylock ( ) {
128
144
Some ( LockGuard { lock : self } )
@@ -135,6 +151,12 @@ impl StaticNativeMutex {
135
151
///
136
152
/// These needs to be paired with a call to `.unlock_noguard`. Prefer using
137
153
/// `.lock`.
154
+ ///
155
+ /// # Unsafety
156
+ ///
157
+ /// This method is unsafe for the same reasons as `lock`. Additionally, this
158
+ /// does not guarantee that the mutex will ever be unlocked, and it is
159
+ /// undefined to drop an already-locked mutex.
138
160
pub unsafe fn lock_noguard ( & self ) { self . inner . lock ( ) }
139
161
140
162
/// Attempts to acquire the lock without creating a
@@ -143,22 +165,42 @@ impl StaticNativeMutex {
143
165
///
144
166
/// If `true` is returned, this needs to be paired with a call to
145
167
/// `.unlock_noguard`. Prefer using `.trylock`.
168
+ ///
169
+ /// # Unsafety
170
+ ///
171
+ /// This method is unsafe for the same reasons as `lock_noguard`.
146
172
pub unsafe fn trylock_noguard ( & self ) -> bool {
147
173
self . inner . trylock ( )
148
174
}
149
175
150
176
/// Unlocks the lock. This assumes that the current thread already holds the
151
177
/// lock.
178
+ ///
179
+ /// # Unsafety
180
+ ///
181
+ /// This method is unsafe for the same reasons as `lock`. Additionally, it
182
+ /// is not guaranteed that this is unlocking a previously locked mutex. It
183
+ /// is undefined to unlock an unlocked mutex.
152
184
pub unsafe fn unlock_noguard ( & self ) { self . inner . unlock ( ) }
153
185
154
186
/// Block on the internal condition variable.
155
187
///
156
188
/// This function assumes that the lock is already held. Prefer
157
189
/// using `LockGuard.wait` since that guarantees that the lock is
158
190
/// held.
191
+ ///
192
+ /// # Unsafety
193
+ ///
194
+ /// This method is unsafe for the same reasons as `lock`. Additionally, this
195
+ /// is unsafe because the mutex may not be currently locked.
159
196
pub unsafe fn wait_noguard ( & self ) { self . inner . wait ( ) }
160
197
161
198
/// Signals a thread in `wait` to wake up
199
+ ///
200
+ /// # Unsafety
201
+ ///
202
+ /// This method is unsafe for the same reasons as `lock`. Additionally, this
203
+ /// is unsafe because the mutex may not be currently locked.
162
204
pub unsafe fn signal_noguard ( & self ) { self . inner . signal ( ) }
163
205
164
206
/// This function is especially unsafe because there are no guarantees made
@@ -181,6 +223,7 @@ impl NativeMutex {
181
223
/// already hold the lock.
182
224
///
183
225
/// # Example
226
+ ///
184
227
/// ```rust
185
228
/// use std::rt::mutex::NativeMutex;
186
229
/// unsafe {
@@ -192,12 +235,22 @@ impl NativeMutex {
192
235
/// } // automatically unlocked in `_guard`'s destructor
193
236
/// }
194
237
/// ```
238
+ ///
239
+ /// # Unsafety
240
+ ///
241
+ /// This method is unsafe due to the same reasons as
242
+ /// `StaticNativeMutex::lock`.
195
243
pub unsafe fn lock < ' a > ( & ' a self ) -> LockGuard < ' a > {
196
244
self . inner . lock ( )
197
245
}
198
246
199
247
/// Attempts to acquire the lock. The value returned is `Some` if
200
248
/// the attempt succeeded.
249
+ ///
250
+ /// # Unsafety
251
+ ///
252
+ /// This method is unsafe due to the same reasons as
253
+ /// `StaticNativeMutex::trylock`.
201
254
pub unsafe fn trylock < ' a > ( & ' a self ) -> Option < LockGuard < ' a > > {
202
255
self . inner . trylock ( )
203
256
}
@@ -206,6 +259,11 @@ impl NativeMutex {
206
259
///
207
260
/// These needs to be paired with a call to `.unlock_noguard`. Prefer using
208
261
/// `.lock`.
262
+ ///
263
+ /// # Unsafety
264
+ ///
265
+ /// This method is unsafe due to the same reasons as
266
+ /// `StaticNativeMutex::lock_noguard`.
209
267
pub unsafe fn lock_noguard ( & self ) { self . inner . lock_noguard ( ) }
210
268
211
269
/// Attempts to acquire the lock without creating a
@@ -214,22 +272,42 @@ impl NativeMutex {
214
272
///
215
273
/// If `true` is returned, this needs to be paired with a call to
216
274
/// `.unlock_noguard`. Prefer using `.trylock`.
275
+ ///
276
+ /// # Unsafety
277
+ ///
278
+ /// This method is unsafe due to the same reasons as
279
+ /// `StaticNativeMutex::trylock_noguard`.
217
280
pub unsafe fn trylock_noguard ( & self ) -> bool {
218
281
self . inner . trylock_noguard ( )
219
282
}
220
283
221
284
/// Unlocks the lock. This assumes that the current thread already holds the
222
285
/// lock.
286
+ ///
287
+ /// # Unsafety
288
+ ///
289
+ /// This method is unsafe due to the same reasons as
290
+ /// `StaticNativeMutex::unlock_noguard`.
223
291
pub unsafe fn unlock_noguard ( & self ) { self . inner . unlock_noguard ( ) }
224
292
225
293
/// Block on the internal condition variable.
226
294
///
227
295
/// This function assumes that the lock is already held. Prefer
228
296
/// using `LockGuard.wait` since that guarantees that the lock is
229
297
/// held.
298
+ ///
299
+ /// # Unsafety
300
+ ///
301
+ /// This method is unsafe due to the same reasons as
302
+ /// `StaticNativeMutex::wait_noguard`.
230
303
pub unsafe fn wait_noguard ( & self ) { self . inner . wait_noguard ( ) }
231
304
232
305
/// Signals a thread in `wait` to wake up
306
+ ///
307
+ /// # Unsafety
308
+ ///
309
+ /// This method is unsafe due to the same reasons as
310
+ /// `StaticNativeMutex::signal_noguard`.
233
311
pub unsafe fn signal_noguard ( & self ) { self . inner . signal_noguard ( ) }
234
312
}
235
313
0 commit comments