@@ -62,7 +62,7 @@ cfg_if! {
62
62
pub use std:: cell:: RefMut as WriteGuard ;
63
63
pub use std:: cell:: RefMut as LockGuard ;
64
64
65
- pub use std:: cell:: RefCell as RwLock ;
65
+ use std:: cell:: RefCell as InnerRwLock ;
66
66
use std:: cell:: RefCell as InnerLock ;
67
67
68
68
use std:: cell:: Cell ;
@@ -159,13 +159,12 @@ cfg_if! {
159
159
160
160
pub use parking_lot:: MutexGuard as LockGuard ;
161
161
162
- use parking_lot;
163
-
164
162
pub use std:: sync:: Arc as Lrc ;
165
163
166
164
pub use self :: Lock as MTLock ;
167
165
168
166
use parking_lot:: Mutex as InnerLock ;
167
+ use parking_lot:: RwLock as InnerRwLock ;
169
168
170
169
pub type MetadataRef = OwningRef <Box <Erased + Send + Sync >, [ u8 ] >;
171
170
@@ -222,42 +221,6 @@ cfg_if! {
222
221
self . 0 . lock( ) . take( )
223
222
}
224
223
}
225
-
226
- #[ derive( Debug ) ]
227
- pub struct RwLock <T >( parking_lot:: RwLock <T >) ;
228
-
229
- impl <T > RwLock <T > {
230
- #[ inline( always) ]
231
- pub fn new( inner: T ) -> Self {
232
- RwLock ( parking_lot:: RwLock :: new( inner) )
233
- }
234
-
235
- #[ inline( always) ]
236
- pub fn borrow( & self ) -> ReadGuard <T > {
237
- if ERROR_CHECKING {
238
- self . 0 . try_read( ) . expect( "lock was already held" )
239
- } else {
240
- self . 0 . read( )
241
- }
242
- }
243
-
244
- #[ inline( always) ]
245
- pub fn borrow_mut( & self ) -> WriteGuard <T > {
246
- if ERROR_CHECKING {
247
- self . 0 . try_write( ) . expect( "lock was already held" )
248
- } else {
249
- self . 0 . write( )
250
- }
251
- }
252
- }
253
-
254
- // FIXME: Probably a bad idea
255
- impl <T : Clone > Clone for RwLock <T > {
256
- #[ inline]
257
- fn clone( & self ) -> Self {
258
- RwLock :: new( self . borrow( ) . clone( ) )
259
- }
260
- }
261
224
}
262
225
}
263
226
@@ -383,6 +346,11 @@ impl<T> Lock<T> {
383
346
self . 0 . borrow_mut ( )
384
347
}
385
348
349
+ #[ inline( always) ]
350
+ pub fn with_lock < F : FnOnce ( & mut T ) -> R , R > ( & self , f : F ) -> R {
351
+ f ( & mut * self . lock ( ) )
352
+ }
353
+
386
354
#[ inline( always) ]
387
355
pub fn borrow ( & self ) -> LockGuard < T > {
388
356
self . lock ( )
@@ -401,3 +369,83 @@ impl<T: Clone> Clone for Lock<T> {
401
369
Lock :: new ( self . borrow ( ) . clone ( ) )
402
370
}
403
371
}
372
+
373
+ #[ derive( Debug ) ]
374
+ pub struct RwLock < T > ( InnerRwLock < T > ) ;
375
+
376
+ impl < T > RwLock < T > {
377
+ #[ inline( always) ]
378
+ pub fn new ( inner : T ) -> Self {
379
+ RwLock ( InnerRwLock :: new ( inner) )
380
+ }
381
+
382
+ #[ inline( always) ]
383
+ pub fn into_inner ( self ) -> T {
384
+ self . 0 . into_inner ( )
385
+ }
386
+
387
+ #[ inline( always) ]
388
+ pub fn get_mut ( & mut self ) -> & mut T {
389
+ self . 0 . get_mut ( )
390
+ }
391
+
392
+ #[ cfg( not( parallel_queries) ) ]
393
+ #[ inline( always) ]
394
+ pub fn read ( & self ) -> ReadGuard < T > {
395
+ self . 0 . borrow ( )
396
+ }
397
+
398
+ #[ cfg( parallel_queries) ]
399
+ #[ inline( always) ]
400
+ pub fn read ( & self ) -> ReadGuard < T > {
401
+ if ERROR_CHECKING {
402
+ self . 0 . try_read ( ) . expect ( "lock was already held" )
403
+ } else {
404
+ self . 0 . read ( )
405
+ }
406
+ }
407
+
408
+ #[ inline( always) ]
409
+ pub fn with_read_lock < F : FnOnce ( & T ) -> R , R > ( & self , f : F ) -> R {
410
+ f ( & * self . read ( ) )
411
+ }
412
+
413
+ #[ cfg( not( parallel_queries) ) ]
414
+ #[ inline( always) ]
415
+ pub fn write ( & self ) -> WriteGuard < T > {
416
+ self . 0 . borrow_mut ( )
417
+ }
418
+
419
+ #[ cfg( parallel_queries) ]
420
+ #[ inline( always) ]
421
+ pub fn write ( & self ) -> WriteGuard < T > {
422
+ if ERROR_CHECKING {
423
+ self . 0 . try_write ( ) . expect ( "lock was already held" )
424
+ } else {
425
+ self . 0 . write ( )
426
+ }
427
+ }
428
+
429
+ #[ inline( always) ]
430
+ pub fn with_write_lock < F : FnOnce ( & mut T ) -> R , R > ( & self , f : F ) -> R {
431
+ f ( & mut * self . write ( ) )
432
+ }
433
+
434
+ #[ inline( always) ]
435
+ pub fn borrow ( & self ) -> ReadGuard < T > {
436
+ self . read ( )
437
+ }
438
+
439
+ #[ inline( always) ]
440
+ pub fn borrow_mut ( & self ) -> WriteGuard < T > {
441
+ self . write ( )
442
+ }
443
+ }
444
+
445
+ // FIXME: Probably a bad idea
446
+ impl < T : Clone > Clone for RwLock < T > {
447
+ #[ inline]
448
+ fn clone ( & self ) -> Self {
449
+ RwLock :: new ( self . borrow ( ) . clone ( ) )
450
+ }
451
+ }
0 commit comments