@@ -25,6 +25,7 @@ unsafe impl Sync for RawMutex {}
25
25
26
26
impl RawMutex {
27
27
/// Creates a new raw mutex.
28
+ #[ inline]
28
29
pub fn new ( ) -> RawMutex {
29
30
RawMutex {
30
31
state : AtomicUsize :: new ( 0 ) ,
@@ -35,6 +36,7 @@ impl RawMutex {
35
36
/// Acquires the lock.
36
37
///
37
38
/// We don't use `async` signature here for performance concern.
39
+ #[ inline]
38
40
pub fn lock ( & self ) -> RawLockFuture < ' _ > {
39
41
RawLockFuture {
40
42
mutex : self ,
@@ -44,24 +46,31 @@ impl RawMutex {
44
46
}
45
47
46
48
/// Attempts to acquire the lock.
49
+ #[ inline]
47
50
pub fn try_lock ( & self ) -> bool {
48
51
self . state . fetch_or ( LOCK , Ordering :: Acquire ) & LOCK == 0
49
52
}
50
53
54
+ #[ cold]
55
+ fn unlock_slow ( & self ) {
56
+ let mut blocked = self . blocked . lock ( ) . unwrap ( ) ;
57
+
58
+ if let Some ( ( _, opt_waker) ) = blocked. iter_mut ( ) . next ( ) {
59
+ // If there is no waker in this entry, that means it was already woken.
60
+ if let Some ( w) = opt_waker. take ( ) {
61
+ w. wake ( ) ;
62
+ }
63
+ }
64
+ }
65
+
51
66
/// Unlock this mutex.
67
+ #[ inline]
52
68
pub fn unlock ( & self ) {
53
69
let state = self . state . fetch_and ( !LOCK , Ordering :: AcqRel ) ;
54
70
55
71
// If there are any blocked tasks, wake one of them up.
56
72
if state & BLOCKED != 0 {
57
- let mut blocked = self . blocked . lock ( ) . unwrap ( ) ;
58
-
59
- if let Some ( ( _, opt_waker) ) = blocked. iter_mut ( ) . next ( ) {
60
- // If there is no waker in this entry, that means it was already woken.
61
- if let Some ( w) = opt_waker. take ( ) {
62
- w. wake ( ) ;
63
- }
64
- }
73
+ self . unlock_slow ( ) ;
65
74
}
66
75
}
67
76
}
@@ -190,6 +199,7 @@ impl<T> Mutex<T> {
190
199
///
191
200
/// let mutex = Mutex::new(0);
192
201
/// ```
202
+ #[ inline]
193
203
pub fn new ( t : T ) -> Mutex < T > {
194
204
Mutex {
195
205
mutex : RawMutex :: new ( ) ,
@@ -261,6 +271,7 @@ impl<T> Mutex<T> {
261
271
/// #
262
272
/// # })
263
273
/// ```
274
+ #[ inline]
264
275
pub fn try_lock ( & self ) -> Option < MutexGuard < ' _ , T > > {
265
276
if self . mutex . try_lock ( ) {
266
277
Some ( MutexGuard ( self ) )
@@ -279,6 +290,7 @@ impl<T> Mutex<T> {
279
290
/// let mutex = Mutex::new(10);
280
291
/// assert_eq!(mutex.into_inner(), 10);
281
292
/// ```
293
+ #[ inline]
282
294
pub fn into_inner ( self ) -> T {
283
295
self . value . into_inner ( )
284
296
}
@@ -301,6 +313,7 @@ impl<T> Mutex<T> {
301
313
/// #
302
314
/// # })
303
315
/// ```
316
+ #[ inline]
304
317
pub fn get_mut ( & mut self ) -> & mut T {
305
318
unsafe { & mut * self . value . get ( ) }
306
319
}
@@ -326,12 +339,14 @@ impl<T: fmt::Debug> fmt::Debug for Mutex<T> {
326
339
}
327
340
328
341
impl < T > From < T > for Mutex < T > {
342
+ #[ inline]
329
343
fn from ( val : T ) -> Mutex < T > {
330
344
Mutex :: new ( val)
331
345
}
332
346
}
333
347
334
348
impl < T : Default > Default for Mutex < T > {
349
+ #[ inline]
335
350
fn default ( ) -> Mutex < T > {
336
351
Mutex :: new ( Default :: default ( ) )
337
352
}
@@ -344,6 +359,7 @@ unsafe impl<T: Send> Send for MutexGuard<'_, T> {}
344
359
unsafe impl < T : Sync > Sync for MutexGuard < ' _ , T > { }
345
360
346
361
impl < T > Drop for MutexGuard < ' _ , T > {
362
+ #[ inline]
347
363
fn drop ( & mut self ) {
348
364
self . 0 . mutex . unlock ( ) ;
349
365
}
@@ -364,12 +380,14 @@ impl<T: fmt::Display> fmt::Display for MutexGuard<'_, T> {
364
380
impl < T > Deref for MutexGuard < ' _ , T > {
365
381
type Target = T ;
366
382
383
+ #[ inline]
367
384
fn deref ( & self ) -> & T {
368
385
unsafe { & * self . 0 . value . get ( ) }
369
386
}
370
387
}
371
388
372
389
impl < T > DerefMut for MutexGuard < ' _ , T > {
390
+ #[ inline]
373
391
fn deref_mut ( & mut self ) -> & mut T {
374
392
unsafe { & mut * self . 0 . value . get ( ) }
375
393
}
0 commit comments