@@ -65,7 +65,7 @@ use crate::util::time::Time;
65
65
66
66
use crate :: prelude:: * ;
67
67
use core:: { cmp, fmt} ;
68
- use core:: cell:: { RefCell , RefMut } ;
68
+ use core:: cell:: { RefCell , RefMut , Ref } ;
69
69
use core:: convert:: TryInto ;
70
70
use core:: ops:: { Deref , DerefMut } ;
71
71
use core:: time:: Duration ;
@@ -145,6 +145,7 @@ impl<S: Score, T: DerefMut<Target=S> $(+ $supertrait)*> Score for T {
145
145
146
146
#[ cfg( c_bindings) ]
147
147
define_score ! ( Writeable ) ;
148
+
148
149
#[ cfg( not( c_bindings) ) ]
149
150
define_score ! ( ) ;
150
151
@@ -160,11 +161,17 @@ pub trait LockableScore<'a> {
160
161
/// The [`Score`] type.
161
162
type Score : ' a + Score ;
162
163
163
- /// The locked [`Score`] type.
164
- type Locked : DerefMut < Target = Self :: Score > + Sized ;
164
+ /// The Write locked [`Score`] type.
165
+ type WriteLocked : DerefMut < Target = Self :: Score > + Sized ;
166
+
167
+ /// The Read locked [`Score`] type.
168
+ type ReadLocked : Deref < Target = Self :: Score > + Sized ;
169
+
170
+ /// Returns read locked scorer.
171
+ fn read_lock ( & ' a self ) -> Self :: ReadLocked ;
165
172
166
- /// Returns the locked scorer.
167
- fn lock ( & ' a self ) -> Self :: Locked ;
173
+ /// Returns write locked scorer.
174
+ fn write_lock ( & ' a self ) -> Self :: WriteLocked ;
168
175
}
169
176
170
177
/// Refers to a scorer that is accessible under lock and also writeable to disk
@@ -178,20 +185,30 @@ impl<'a, T> WriteableScore<'a> for T where T: LockableScore<'a> + Writeable {}
178
185
/// This is not exported to bindings users
179
186
impl < ' a , T : ' a + Score > LockableScore < ' a > for Mutex < T > {
180
187
type Score = T ;
181
- type Locked = MutexGuard < ' a , T > ;
188
+ type WriteLocked = MutexGuard < ' a , T > ;
189
+ type ReadLocked = MutexGuard < ' a , T > ;
190
+
191
+ fn read_lock ( & ' a self ) -> Self :: ReadLocked {
192
+ Mutex :: lock ( self ) . unwrap ( )
193
+ }
182
194
183
- fn lock ( & ' a self ) -> Self :: Locked {
195
+ fn write_lock ( & ' a self ) -> Self :: WriteLocked {
184
196
Mutex :: lock ( self ) . unwrap ( )
185
197
}
186
198
}
187
199
188
200
impl < ' a , T : ' a + Score > LockableScore < ' a > for RefCell < T > {
189
201
type Score = T ;
190
- type Locked = RefMut < ' a , T > ;
202
+ type WriteLocked = RefMut < ' a , T > ;
203
+ type ReadLocked = Ref < ' a , T > ;
191
204
192
- fn lock ( & ' a self ) -> Self :: Locked {
205
+ fn write_lock ( & ' a self ) -> Self :: WriteLocked {
193
206
self . borrow_mut ( )
194
207
}
208
+
209
+ fn read_lock ( & ' a self ) -> Self :: ReadLocked {
210
+ self . borrow ( )
211
+ }
195
212
}
196
213
197
214
#[ cfg( c_bindings) ]
@@ -203,17 +220,22 @@ pub struct MultiThreadedLockableScore<T: Score> {
203
220
#[ cfg( c_bindings) ]
204
221
impl < ' a , T : ' a + Score > LockableScore < ' a > for MultiThreadedLockableScore < T > {
205
222
type Score = T ;
206
- type Locked = MultiThreadedScoreLock < ' a , T > ;
223
+ type ReadLocked = MultiThreadedScoreLock < ' a , T > ;
224
+ type WriteLocked = MultiThreadedScoreLock < ' a , T > ;
225
+
226
+ fn read_lock ( & ' a self ) -> Self :: WriteLocked {
227
+ MultiThreadedScoreLock ( Mutex :: lock ( & self . score ) . unwrap ( ) )
228
+ }
207
229
208
- fn lock ( & ' a self ) -> Self :: Locked {
230
+ fn write_lock ( & ' a self ) -> Self :: ReadLocked {
209
231
MultiThreadedScoreLock ( Mutex :: lock ( & self . score ) . unwrap ( ) )
210
232
}
211
233
}
212
234
213
235
#[ cfg( c_bindings) ]
214
236
impl < T : Score > Writeable for MultiThreadedLockableScore < T > {
215
237
fn write < W : Writer > ( & self , writer : & mut W ) -> Result < ( ) , io:: Error > {
216
- self . lock ( ) . write ( writer)
238
+ self . score . lock ( ) . unwrap ( ) . write ( writer)
217
239
}
218
240
}
219
241
0 commit comments