@@ -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
@@ -384,6 +347,11 @@ impl<T> Lock<T> {
384
347
self . 0 . borrow_mut ( )
385
348
}
386
349
350
+ #[ inline( always) ]
351
+ pub fn with_lock < F : FnOnce ( & mut T ) -> R , R > ( & self , f : F ) -> R {
352
+ f ( & mut * self . lock ( ) )
353
+ }
354
+
387
355
#[ inline( always) ]
388
356
pub fn borrow ( & self ) -> LockGuard < T > {
389
357
self . lock ( )
@@ -402,3 +370,83 @@ impl<T: Clone> Clone for Lock<T> {
402
370
Lock :: new ( self . borrow ( ) . clone ( ) )
403
371
}
404
372
}
373
+
374
+ #[ derive( Debug ) ]
375
+ pub struct RwLock < T > ( InnerRwLock < T > ) ;
376
+
377
+ impl < T > RwLock < T > {
378
+ #[ inline( always) ]
379
+ pub fn new ( inner : T ) -> Self {
380
+ RwLock ( InnerRwLock :: new ( inner) )
381
+ }
382
+
383
+ #[ inline( always) ]
384
+ pub fn into_inner ( self ) -> T {
385
+ self . 0 . into_inner ( )
386
+ }
387
+
388
+ #[ inline( always) ]
389
+ pub fn get_mut ( & mut self ) -> & mut T {
390
+ self . 0 . get_mut ( )
391
+ }
392
+
393
+ #[ cfg( not( parallel_queries) ) ]
394
+ #[ inline( always) ]
395
+ pub fn read ( & self ) -> ReadGuard < T > {
396
+ self . 0 . borrow ( )
397
+ }
398
+
399
+ #[ cfg( parallel_queries) ]
400
+ #[ inline( always) ]
401
+ pub fn read ( & self ) -> ReadGuard < T > {
402
+ if ERROR_CHECKING {
403
+ self . 0 . try_read ( ) . expect ( "lock was already held" )
404
+ } else {
405
+ self . 0 . read ( )
406
+ }
407
+ }
408
+
409
+ #[ inline( always) ]
410
+ pub fn with_read_lock < F : FnOnce ( & T ) -> R , R > ( & self , f : F ) -> R {
411
+ f ( & * self . read ( ) )
412
+ }
413
+
414
+ #[ cfg( not( parallel_queries) ) ]
415
+ #[ inline( always) ]
416
+ pub fn write ( & self ) -> WriteGuard < T > {
417
+ self . 0 . borrow_mut ( )
418
+ }
419
+
420
+ #[ cfg( parallel_queries) ]
421
+ #[ inline( always) ]
422
+ pub fn write ( & self ) -> WriteGuard < T > {
423
+ if ERROR_CHECKING {
424
+ self . 0 . try_write ( ) . expect ( "lock was already held" )
425
+ } else {
426
+ self . 0 . write ( )
427
+ }
428
+ }
429
+
430
+ #[ inline( always) ]
431
+ pub fn with_write_lock < F : FnOnce ( & mut T ) -> R , R > ( & self , f : F ) -> R {
432
+ f ( & mut * self . write ( ) )
433
+ }
434
+
435
+ #[ inline( always) ]
436
+ pub fn borrow ( & self ) -> ReadGuard < T > {
437
+ self . read ( )
438
+ }
439
+
440
+ #[ inline( always) ]
441
+ pub fn borrow_mut ( & self ) -> WriteGuard < T > {
442
+ self . write ( )
443
+ }
444
+ }
445
+
446
+ // FIXME: Probably a bad idea
447
+ impl < T : Clone > Clone for RwLock < T > {
448
+ #[ inline]
449
+ fn clone ( & self ) -> Self {
450
+ RwLock :: new ( self . borrow ( ) . clone ( ) )
451
+ }
452
+ }
0 commit comments