@@ -163,6 +163,19 @@ impl<T: ?Sized> Mutex<T> {
163
163
pub fn get_mut ( & mut self ) -> & mut T {
164
164
unsafe { & mut * self . data . get ( ) }
165
165
}
166
+
167
+ /// Unlocks the mutex directly.
168
+ ///
169
+ /// # Safety
170
+ ///
171
+ /// This function is intended to be used only in the case where the mutex is locked,
172
+ /// and the guard is subsequently forgotten. Calling this while you don't hold a lock
173
+ /// on the mutex will likely lead to UB.
174
+ pub ( crate ) unsafe fn unlock_unchecked ( & self ) {
175
+ // Remove the last bit and notify a waiting lock operation.
176
+ self . state . fetch_sub ( 1 , Ordering :: Release ) ;
177
+ self . lock_ops . notify ( 1 ) ;
178
+ }
166
179
}
167
180
168
181
impl < T : ?Sized > Mutex < T > {
@@ -562,10 +575,12 @@ impl<'a, T: ?Sized> MutexGuard<'a, T> {
562
575
}
563
576
564
577
impl < T : ?Sized > Drop for MutexGuard < ' _ , T > {
578
+ #[ inline]
565
579
fn drop ( & mut self ) {
566
- // Remove the last bit and notify a waiting lock operation.
567
- self . 0 . state . fetch_sub ( 1 , Ordering :: Release ) ;
568
- self . 0 . lock_ops . notify ( 1 ) ;
580
+ // SAFETY: we are dropping the mutex guard, therefore unlocking the mutex.
581
+ unsafe {
582
+ self . 0 . unlock_unchecked ( ) ;
583
+ }
569
584
}
570
585
}
571
586
@@ -623,10 +638,12 @@ impl<T: ?Sized> MutexGuardArc<T> {
623
638
}
624
639
625
640
impl < T : ?Sized > Drop for MutexGuardArc < T > {
641
+ #[ inline]
626
642
fn drop ( & mut self ) {
627
- // Remove the last bit and notify a waiting lock operation.
628
- self . 0 . state . fetch_sub ( 1 , Ordering :: Release ) ;
629
- self . 0 . lock_ops . notify ( 1 ) ;
643
+ // SAFETY: we are dropping the mutex guard, therefore unlocking the mutex.
644
+ unsafe {
645
+ self . 0 . unlock_unchecked ( ) ;
646
+ }
630
647
}
631
648
}
632
649
0 commit comments