@@ -70,7 +70,7 @@ use core::convert::TryInto;
70
70
use core:: ops:: { Deref , DerefMut } ;
71
71
use core:: time:: Duration ;
72
72
use crate :: io:: { self , Read } ;
73
- use crate :: sync:: { Mutex , MutexGuard } ;
73
+ use crate :: sync:: { Mutex , MutexGuard , RwLock , RwLockReadGuard , RwLockWriteGuard } ;
74
74
75
75
/// We define Score ever-so-slightly differently based on whether we are being built for C bindings
76
76
/// or not. For users, `LockableScore` must somehow be writeable to disk. For Rust users, this is
@@ -212,31 +212,47 @@ impl<'a, T: 'a + Score> LockableScore<'a> for RefCell<T> {
212
212
}
213
213
}
214
214
215
+ impl < ' a , T : ' a + Score > LockableScore < ' a > for RwLock < T > {
216
+ type Score = T ;
217
+ type WriteLocked = RwLockWriteGuard < ' a , T > ;
218
+ type ReadLocked = RwLockReadGuard < ' a , T > ;
219
+
220
+ fn read_lock ( & ' a self ) -> Self :: ReadLocked {
221
+ RwLock :: read ( self ) . unwrap ( )
222
+ }
223
+
224
+ fn write_lock ( & ' a self ) -> Self :: WriteLocked {
225
+ RwLock :: write ( self ) . unwrap ( )
226
+ }
227
+ }
228
+
215
229
#[ cfg( c_bindings) ]
216
230
/// A concrete implementation of [`LockableScore`] which supports multi-threading.
217
231
pub struct MultiThreadedLockableScore < T : Score > {
218
- score : Mutex < T > ,
232
+ score : RwLock < T > ,
219
233
}
220
234
221
235
#[ cfg( c_bindings) ]
222
236
impl < ' a , T : ' a + Score > LockableScore < ' a > for MultiThreadedLockableScore < T > {
223
237
type Score = T ;
224
- type ReadLocked = MultiThreadedScoreLock < ' a , T > ;
225
- type WriteLocked = MultiThreadedScoreLock < ' a , T > ;
238
+ // type ReadLocked = MultiThreadedScoreLock<'a, T>;
239
+ // type WriteLocked = Rw<'a, T>;
240
+ type WriteLocked = RwLockWriteGuard < ' a , T > ;
241
+ type ReadLocked = RwLockReadGuard < ' a , T > ;
226
242
227
- fn read_lock ( & ' a self ) -> Self :: WriteLocked {
228
- MultiThreadedScoreLock ( Mutex :: lock ( & self . score ) . unwrap ( ) )
243
+ fn read_lock ( & ' a self ) -> Self :: ReadLocked {
244
+ RwLock :: read ( & self . score ) . unwrap ( )
229
245
}
230
246
231
- fn write_lock ( & ' a self ) -> Self :: ReadLocked {
232
- MultiThreadedScoreLock ( Mutex :: lock ( & self . score ) . unwrap ( ) )
247
+ fn write_lock ( & ' a self ) -> Self :: WriteLocked {
248
+ RwLock :: write ( & self . score ) . unwrap ( )
233
249
}
234
250
}
235
251
236
252
#[ cfg( c_bindings) ]
237
253
impl < T : Score > Writeable for MultiThreadedLockableScore < T > {
238
254
fn write < W : Writer > ( & self , writer : & mut W ) -> Result < ( ) , io:: Error > {
239
- self . score . lock ( ) . unwrap ( ) . write ( writer)
255
+ self . score . read ( ) . unwrap ( ) . write ( writer)
240
256
}
241
257
}
242
258
@@ -247,7 +263,7 @@ impl<'a, T: 'a + Score> WriteableScore<'a> for MultiThreadedLockableScore<T> {}
247
263
impl < T : Score > MultiThreadedLockableScore < T > {
248
264
/// Creates a new [`MultiThreadedLockableScore`] given an underlying [`Score`].
249
265
pub fn new ( score : T ) -> Self {
250
- MultiThreadedLockableScore { score : Mutex :: new ( score) }
266
+ MultiThreadedLockableScore { score : RwLock :: new ( score) }
251
267
}
252
268
}
253
269
0 commit comments