@@ -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
@@ -211,31 +211,47 @@ impl<'a, T: 'a + Score> LockableScore<'a> for RefCell<T> {
211
211
}
212
212
}
213
213
214
+ impl < ' a , T : ' a + Score > LockableScore < ' a > for RwLock < T > {
215
+ type Score = T ;
216
+ type WriteLocked = RwLockWriteGuard < ' a , T > ;
217
+ type ReadLocked = RwLockReadGuard < ' a , T > ;
218
+
219
+ fn read_lock ( & ' a self ) -> Self :: ReadLocked {
220
+ RwLock :: read ( self ) . unwrap ( )
221
+ }
222
+
223
+ fn write_lock ( & ' a self ) -> Self :: WriteLocked {
224
+ RwLock :: write ( self ) . unwrap ( )
225
+ }
226
+ }
227
+
214
228
#[ cfg( c_bindings) ]
215
229
/// A concrete implementation of [`LockableScore`] which supports multi-threading.
216
230
pub struct MultiThreadedLockableScore < T : Score > {
217
- score : Mutex < T > ,
231
+ score : RwLock < T > ,
218
232
}
219
233
220
234
#[ cfg( c_bindings) ]
221
235
impl < ' a , T : ' a + Score > LockableScore < ' a > for MultiThreadedLockableScore < T > {
222
236
type Score = T ;
223
- type ReadLocked = MultiThreadedScoreLock < ' a , T > ;
224
- type WriteLocked = MultiThreadedScoreLock < ' a , T > ;
237
+ // type ReadLocked = MultiThreadedScoreLock<'a, T>;
238
+ // type WriteLocked = Rw<'a, T>;
239
+ type WriteLocked = RwLockWriteGuard < ' a , T > ;
240
+ type ReadLocked = RwLockReadGuard < ' a , T > ;
225
241
226
- fn read_lock ( & ' a self ) -> Self :: WriteLocked {
227
- MultiThreadedScoreLock ( Mutex :: lock ( & self . score ) . unwrap ( ) )
242
+ fn read_lock ( & ' a self ) -> Self :: ReadLocked {
243
+ RwLock :: read ( & self . score ) . unwrap ( )
228
244
}
229
245
230
- fn write_lock ( & ' a self ) -> Self :: ReadLocked {
231
- MultiThreadedScoreLock ( Mutex :: lock ( & self . score ) . unwrap ( ) )
246
+ fn write_lock ( & ' a self ) -> Self :: WriteLocked {
247
+ RwLock :: write ( & self . score ) . unwrap ( )
232
248
}
233
249
}
234
250
235
251
#[ cfg( c_bindings) ]
236
252
impl < T : Score > Writeable for MultiThreadedLockableScore < T > {
237
253
fn write < W : Writer > ( & self , writer : & mut W ) -> Result < ( ) , io:: Error > {
238
- self . score . lock ( ) . unwrap ( ) . write ( writer)
254
+ self . score . read ( ) . unwrap ( ) . write ( writer)
239
255
}
240
256
}
241
257
@@ -246,7 +262,7 @@ impl<'a, T: 'a + Score> WriteableScore<'a> for MultiThreadedLockableScore<T> {}
246
262
impl < T : Score > MultiThreadedLockableScore < T > {
247
263
/// Creates a new [`MultiThreadedLockableScore`] given an underlying [`Score`].
248
264
pub fn new ( score : T ) -> Self {
249
- MultiThreadedLockableScore { score : Mutex :: new ( score) }
265
+ MultiThreadedLockableScore { score : RwLock :: new ( score) }
250
266
}
251
267
}
252
268
0 commit comments