@@ -5,6 +5,7 @@ use crate::cell::UnsafeCell;
5
5
use crate :: fmt;
6
6
use crate :: mem;
7
7
use crate :: ops:: { Deref , DerefMut } ;
8
+ use crate :: pin:: Pin ;
8
9
use crate :: ptr;
9
10
use crate :: sys_common:: poison:: { self , LockResult , TryLockError , TryLockResult } ;
10
11
use crate :: sys_common:: rwlock as sys;
@@ -64,7 +65,7 @@ use crate::sys_common::rwlock as sys;
64
65
/// [`Mutex`]: super::Mutex
65
66
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
66
67
pub struct RwLock < T : ?Sized > {
67
- inner : Box < sys:: RWLock > ,
68
+ inner : Pin < Box < sys:: RWLock > > ,
68
69
poison : poison:: Flag ,
69
70
data : UnsafeCell < T > ,
70
71
}
@@ -128,7 +129,7 @@ impl<T> RwLock<T> {
128
129
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
129
130
pub fn new ( t : T ) -> RwLock < T > {
130
131
RwLock {
131
- inner : box sys:: RWLock :: new ( ) ,
132
+ inner : Box :: pin ( sys:: RWLock :: new ( ) ) ,
132
133
poison : poison:: Flag :: new ( ) ,
133
134
data : UnsafeCell :: new ( t) ,
134
135
}
@@ -178,10 +179,9 @@ impl<T: ?Sized> RwLock<T> {
178
179
#[ inline]
179
180
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
180
181
pub fn read ( & self ) -> LockResult < RwLockReadGuard < ' _ , T > > {
181
- unsafe {
182
- self . inner . read ( ) ;
183
- RwLockReadGuard :: new ( self )
184
- }
182
+ self . inner . as_ref ( ) . read ( ) ;
183
+ // SAFETY: We've gotten a read-lock on the rwlock.
184
+ unsafe { RwLockReadGuard :: new ( self ) }
185
185
}
186
186
187
187
/// Attempts to acquire this rwlock with shared read access.
@@ -217,12 +217,11 @@ impl<T: ?Sized> RwLock<T> {
217
217
#[ inline]
218
218
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
219
219
pub fn try_read ( & self ) -> TryLockResult < RwLockReadGuard < ' _ , T > > {
220
- unsafe {
221
- if self . inner . try_read ( ) {
222
- Ok ( RwLockReadGuard :: new ( self ) ?)
223
- } else {
224
- Err ( TryLockError :: WouldBlock )
225
- }
220
+ if self . inner . as_ref ( ) . try_read ( ) {
221
+ // SAFETY: We've gotten a read-lock on the rwlock.
222
+ unsafe { Ok ( RwLockReadGuard :: new ( self ) ?) }
223
+ } else {
224
+ Err ( TryLockError :: WouldBlock )
226
225
}
227
226
}
228
227
@@ -260,10 +259,9 @@ impl<T: ?Sized> RwLock<T> {
260
259
#[ inline]
261
260
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
262
261
pub fn write ( & self ) -> LockResult < RwLockWriteGuard < ' _ , T > > {
263
- unsafe {
264
- self . inner . write ( ) ;
265
- RwLockWriteGuard :: new ( self )
266
- }
262
+ self . inner . as_ref ( ) . write ( ) ;
263
+ // SAFETY: We've gotten a write-lock on the rwlock.
264
+ unsafe { RwLockWriteGuard :: new ( self ) }
267
265
}
268
266
269
267
/// Attempts to lock this rwlock with exclusive write access.
@@ -299,12 +297,11 @@ impl<T: ?Sized> RwLock<T> {
299
297
#[ inline]
300
298
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
301
299
pub fn try_write ( & self ) -> TryLockResult < RwLockWriteGuard < ' _ , T > > {
302
- unsafe {
303
- if self . inner . try_write ( ) {
304
- Ok ( RwLockWriteGuard :: new ( self ) ?)
305
- } else {
306
- Err ( TryLockError :: WouldBlock )
307
- }
300
+ if self . inner . as_ref ( ) . try_write ( ) {
301
+ // SAFETY: We've gotten a write-lock on the rwlock.
302
+ unsafe { Ok ( RwLockWriteGuard :: new ( self ) ?) }
303
+ } else {
304
+ Err ( TryLockError :: WouldBlock )
308
305
}
309
306
}
310
307
@@ -374,7 +371,6 @@ impl<T: ?Sized> RwLock<T> {
374
371
( ptr:: read ( inner) , ptr:: read ( poison) , ptr:: read ( data) )
375
372
} ;
376
373
mem:: forget ( self ) ;
377
- inner. destroy ( ) ; // Keep in sync with the `Drop` impl.
378
374
drop ( inner) ;
379
375
380
376
poison:: map_result ( poison. borrow ( ) , |_| data. into_inner ( ) )
@@ -409,14 +405,6 @@ impl<T: ?Sized> RwLock<T> {
409
405
}
410
406
}
411
407
412
- #[ stable( feature = "rust1" , since = "1.0.0" ) ]
413
- unsafe impl < #[ may_dangle] T : ?Sized > Drop for RwLock < T > {
414
- fn drop ( & mut self ) {
415
- // IMPORTANT: This code needs to be kept in sync with `RwLock::into_inner`.
416
- unsafe { self . inner . destroy ( ) }
417
- }
418
- }
419
-
420
408
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
421
409
impl < T : ?Sized + fmt:: Debug > fmt:: Debug for RwLock < T > {
422
410
fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
@@ -524,8 +512,10 @@ impl<T: ?Sized> DerefMut for RwLockWriteGuard<'_, T> {
524
512
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
525
513
impl < T : ?Sized > Drop for RwLockReadGuard < ' _ , T > {
526
514
fn drop ( & mut self ) {
515
+ // SAFETY: The fact that we have a RwLockReadGuard proves this thread
516
+ // has this rwlock read-locked.
527
517
unsafe {
528
- self . lock . inner . read_unlock ( ) ;
518
+ self . lock . inner . as_ref ( ) . read_unlock ( ) ;
529
519
}
530
520
}
531
521
}
@@ -534,8 +524,10 @@ impl<T: ?Sized> Drop for RwLockReadGuard<'_, T> {
534
524
impl < T : ?Sized > Drop for RwLockWriteGuard < ' _ , T > {
535
525
fn drop ( & mut self ) {
536
526
self . lock . poison . done ( & self . poison ) ;
527
+ // SAFETY: The fact that we have a RwLockWriteGuard proves this thread
528
+ // has this rwlock write-locked.
537
529
unsafe {
538
- self . lock . inner . write_unlock ( ) ;
530
+ self . lock . inner . as_ref ( ) . write_unlock ( ) ;
539
531
}
540
532
}
541
533
}
0 commit comments