@@ -6,7 +6,6 @@ use std::cell::RefCell;
6
6
use std:: fmt:: { Debug , Display } ;
7
7
use std:: ops:: { Deref , DerefMut } ;
8
8
use std:: panic:: { RefUnwindSafe , UnwindSafe } ;
9
- use std:: rc:: Rc ;
10
9
use std:: sync:: { LockResult , PoisonError , TryLockError , TryLockResult } ;
11
10
use tracing:: trace;
12
11
@@ -16,7 +15,7 @@ use tracing::trace;
16
15
/// `RwLock` more than once. The `std` version is ambiguous about what behavior is allowed here, so
17
16
/// we choose the most conservative one.
18
17
pub struct RwLock < T : ?Sized > {
19
- state : Rc < RefCell < RwLockState > > ,
18
+ state : RefCell < RwLockState > ,
20
19
inner : std:: sync:: RwLock < T > ,
21
20
}
22
21
@@ -43,7 +42,7 @@ enum RwLockType {
43
42
44
43
impl < T > RwLock < T > {
45
44
/// Create a new instance of an `RwLock<T>` which is unlocked.
46
- pub fn new ( value : T ) -> Self {
45
+ pub const fn new ( value : T ) -> Self {
47
46
let state = RwLockState {
48
47
holder : RwLockHolder :: None ,
49
48
waiting_readers : TaskSet :: new ( ) ,
@@ -53,7 +52,7 @@ impl<T> RwLock<T> {
53
52
54
53
Self {
55
54
inner : std:: sync:: RwLock :: new ( value) ,
56
- state : Rc :: new ( RefCell :: new ( state) ) ,
55
+ state : RefCell :: new ( state) ,
57
56
}
58
57
}
59
58
}
@@ -67,12 +66,12 @@ impl<T: ?Sized> RwLock<T> {
67
66
match self . inner . try_read ( ) {
68
67
Ok ( guard) => Ok ( RwLockReadGuard {
69
68
inner : Some ( guard) ,
70
- state : Rc :: clone ( & self . state ) ,
69
+ rwlock : self ,
71
70
me : ExecutionState :: me ( ) ,
72
71
} ) ,
73
72
Err ( TryLockError :: Poisoned ( err) ) => Err ( PoisonError :: new ( RwLockReadGuard {
74
73
inner : Some ( err. into_inner ( ) ) ,
75
- state : Rc :: clone ( & self . state ) ,
74
+ rwlock : self ,
76
75
me : ExecutionState :: me ( ) ,
77
76
} ) ) ,
78
77
Err ( TryLockError :: WouldBlock ) => panic ! ( "rwlock state out of sync" ) ,
@@ -87,12 +86,12 @@ impl<T: ?Sized> RwLock<T> {
87
86
match self . inner . try_write ( ) {
88
87
Ok ( guard) => Ok ( RwLockWriteGuard {
89
88
inner : Some ( guard) ,
90
- state : Rc :: clone ( & self . state ) ,
89
+ rwlock : self ,
91
90
me : ExecutionState :: me ( ) ,
92
91
} ) ,
93
92
Err ( TryLockError :: Poisoned ( err) ) => Err ( PoisonError :: new ( RwLockWriteGuard {
94
93
inner : Some ( err. into_inner ( ) ) ,
95
- state : Rc :: clone ( & self . state ) ,
94
+ rwlock : self ,
96
95
me : ExecutionState :: me ( ) ,
97
96
} ) ) ,
98
97
Err ( TryLockError :: WouldBlock ) => panic ! ( "rwlock state out of sync" ) ,
@@ -111,12 +110,12 @@ impl<T: ?Sized> RwLock<T> {
111
110
match self . inner . try_read ( ) {
112
111
Ok ( guard) => Ok ( RwLockReadGuard {
113
112
inner : Some ( guard) ,
114
- state : Rc :: clone ( & self . state ) ,
113
+ rwlock : self ,
115
114
me : ExecutionState :: me ( ) ,
116
115
} ) ,
117
116
Err ( TryLockError :: Poisoned ( err) ) => Err ( TryLockError :: Poisoned ( PoisonError :: new ( RwLockReadGuard {
118
117
inner : Some ( err. into_inner ( ) ) ,
119
- state : Rc :: clone ( & self . state ) ,
118
+ rwlock : self ,
120
119
me : ExecutionState :: me ( ) ,
121
120
} ) ) ) ,
122
121
Err ( TryLockError :: WouldBlock ) => panic ! ( "rwlock state out of sync" ) ,
@@ -135,12 +134,12 @@ impl<T: ?Sized> RwLock<T> {
135
134
match self . inner . try_write ( ) {
136
135
Ok ( guard) => Ok ( RwLockWriteGuard {
137
136
inner : Some ( guard) ,
138
- state : Rc :: clone ( & self . state ) ,
137
+ rwlock : self ,
139
138
me : ExecutionState :: me ( ) ,
140
139
} ) ,
141
140
Err ( TryLockError :: Poisoned ( err) ) => Err ( TryLockError :: Poisoned ( PoisonError :: new ( RwLockWriteGuard {
142
141
inner : Some ( err. into_inner ( ) ) ,
143
- state : Rc :: clone ( & self . state ) ,
142
+ rwlock : self ,
144
143
me : ExecutionState :: me ( ) ,
145
144
} ) ) ) ,
146
145
Err ( TryLockError :: WouldBlock ) => panic ! ( "rwlock state out of sync" ) ,
@@ -175,7 +174,7 @@ impl<T: ?Sized> RwLock<T> {
175
174
waiting_writers = ?state. waiting_writers,
176
175
"acquiring {:?} lock on rwlock {:p}" ,
177
176
typ,
178
- self . state ,
177
+ self ,
179
178
) ;
180
179
181
180
// We are waiting for the lock
@@ -250,7 +249,7 @@ impl<T: ?Sized> RwLock<T> {
250
249
waiting_writers = ?state. waiting_writers,
251
250
"acquired {:?} lock on rwlock {:p}" ,
252
251
typ,
253
- self . state
252
+ self
254
253
) ;
255
254
256
255
// Increment the current thread's clock and update this RwLock's clock to match.
@@ -283,7 +282,7 @@ impl<T: ?Sized> RwLock<T> {
283
282
waiting_writers = ?state. waiting_writers,
284
283
"trying to acquire {:?} lock on rwlock {:p}" ,
285
284
typ,
286
- self . state ,
285
+ self ,
287
286
) ;
288
287
289
288
let acquired = match ( typ, & mut state. holder ) {
@@ -309,7 +308,7 @@ impl<T: ?Sized> RwLock<T> {
309
308
"{} {:?} lock on rwlock {:p}" ,
310
309
if acquired { "acquired" } else { "failed to acquire" } ,
311
310
typ,
312
- self . state ,
311
+ self ,
313
312
) ;
314
313
315
314
// Update this thread's clock with the clock stored in the RwLock.
@@ -403,9 +402,9 @@ impl<T: ?Sized + Debug> Debug for RwLock<T> {
403
402
404
403
/// RAII structure used to release the shared read access of a `RwLock` when dropped.
405
404
pub struct RwLockReadGuard < ' a , T : ?Sized > {
406
- state : Rc < RefCell < RwLockState > > ,
407
- me : TaskId ,
408
405
inner : Option < std:: sync:: RwLockReadGuard < ' a , T > > ,
406
+ rwlock : & ' a RwLock < T > ,
407
+ me : TaskId ,
409
408
}
410
409
411
410
impl < T : ?Sized > Deref for RwLockReadGuard < ' _ , T > {
@@ -432,14 +431,14 @@ impl<T: ?Sized> Drop for RwLockReadGuard<'_, T> {
432
431
fn drop ( & mut self ) {
433
432
self . inner = None ;
434
433
435
- let mut state = self . state . borrow_mut ( ) ;
434
+ let mut state = self . rwlock . state . borrow_mut ( ) ;
436
435
437
436
trace ! (
438
437
holder = ?state. holder,
439
438
waiting_readers = ?state. waiting_readers,
440
439
waiting_writers = ?state. waiting_writers,
441
440
"releasing Read lock on rwlock {:p}" ,
442
- self . state
441
+ self . rwlock
443
442
) ;
444
443
445
444
match & mut state. holder {
@@ -471,7 +470,7 @@ impl<T: ?Sized> Drop for RwLockReadGuard<'_, T> {
471
470
/// RAII structure used to release the exclusive write access of a `RwLock` when dropped.
472
471
pub struct RwLockWriteGuard < ' a , T : ?Sized > {
473
472
inner : Option < std:: sync:: RwLockWriteGuard < ' a , T > > ,
474
- state : Rc < RefCell < RwLockState > > ,
473
+ rwlock : & ' a RwLock < T > ,
475
474
me : TaskId ,
476
475
}
477
476
@@ -505,13 +504,13 @@ impl<T: ?Sized> Drop for RwLockWriteGuard<'_, T> {
505
504
fn drop ( & mut self ) {
506
505
self . inner = None ;
507
506
508
- let mut state = self . state . borrow_mut ( ) ;
507
+ let mut state = self . rwlock . state . borrow_mut ( ) ;
509
508
trace ! (
510
509
holder = ?state. holder,
511
510
waiting_readers = ?state. waiting_readers,
512
511
waiting_writers = ?state. waiting_writers,
513
512
"releasing Write lock on rwlock {:p}" ,
514
- self . state
513
+ self . rwlock
515
514
) ;
516
515
517
516
assert_eq ! ( state. holder, RwLockHolder :: Write ( self . me) ) ;
0 commit comments