@@ -319,67 +319,49 @@ pub struct LittleLock {
319
319
priv l: Mutex ,
320
320
}
321
321
322
+ pub struct LittleGuard < ' a > {
323
+ priv l: & ' a mut Mutex ,
324
+ }
325
+
322
326
impl Drop for LittleLock {
323
327
fn drop ( & mut self ) {
324
- unsafe {
325
- self . l . destroy ( ) ;
326
- }
328
+ unsafe { self . l . destroy ( ) ; }
329
+ }
330
+ }
331
+
332
+ #[ unsafe_destructor]
333
+ impl < ' a > Drop for LittleGuard < ' a > {
334
+ fn drop ( & mut self ) {
335
+ unsafe { self . l . unlock ( ) ; }
327
336
}
328
337
}
329
338
330
339
impl LittleLock {
331
340
pub fn new ( ) -> LittleLock {
332
- unsafe {
333
- LittleLock {
334
- l : Mutex :: new ( )
335
- }
336
- }
341
+ unsafe { LittleLock { l : Mutex :: new ( ) } }
337
342
}
338
343
339
- pub unsafe fn lock < T > ( & self , f: || -> T ) -> T {
340
- let this = cast:: transmute_mut ( self ) ;
341
- do atomically {
342
- this. l . lock ( ) ;
343
- do ( || {
344
- f ( )
345
- } ) . finally {
346
- this. l . unlock ( ) ;
347
- }
348
- }
344
+ pub unsafe fn lock < ' a > ( & ' a mut self ) -> LittleGuard < ' a > {
345
+ self . l . lock ( ) ;
346
+ LittleGuard { l : & mut self . l }
349
347
}
350
348
351
- pub unsafe fn try_lock < T > ( & self , f: || -> T ) -> Option < T > {
352
- let this = cast:: transmute_mut ( self ) ;
353
- do atomically {
354
- if this. l . trylock ( ) {
355
- Some ( do ( || {
356
- f ( )
357
- } ) . finally {
358
- this. l . unlock ( ) ;
359
- } )
360
- } else {
361
- None
362
- }
349
+ pub unsafe fn try_lock < ' a > ( & ' a mut self ) -> Option < LittleGuard < ' a > > {
350
+ if self . l . trylock ( ) {
351
+ Some ( LittleGuard { l : & mut self . l } )
352
+ } else {
353
+ None
363
354
}
364
355
}
365
356
366
- pub unsafe fn signal ( & self ) {
367
- let this = cast:: transmute_mut ( self ) ;
368
- this. l . signal ( ) ;
357
+ pub unsafe fn signal ( & mut self ) {
358
+ self . l . signal ( ) ;
369
359
}
360
+ }
370
361
371
- pub unsafe fn lock_and_wait ( & self , f: || -> bool) {
372
- let this = cast:: transmute_mut ( self ) ;
373
- do atomically {
374
- this. l . lock ( ) ;
375
- do ( || {
376
- if f ( ) {
377
- this. l . wait ( ) ;
378
- }
379
- } ) . finally {
380
- this. l . unlock ( ) ;
381
- }
382
- }
362
+ impl < ' a > LittleGuard < ' a > {
363
+ pub unsafe fn wait ( & mut self ) {
364
+ self . l . wait ( ) ;
383
365
}
384
366
}
385
367
@@ -431,15 +413,14 @@ impl<T:Send> Exclusive<T> {
431
413
#[ inline]
432
414
pub unsafe fn with < U > ( & self , f: |x: & mut T | -> U ) -> U {
433
415
let rec = self . x . get ( ) ;
434
- do ( * rec) . lock . lock {
435
- if ( * rec) . failed {
436
- fail ! ( "Poisoned Exclusive::new - another task failed inside!" ) ;
437
- }
438
- ( * rec) . failed = true ;
439
- let result = f ( & mut ( * rec) . data ) ;
440
- ( * rec) . failed = false ;
441
- result
416
+ let _l = ( * rec) . lock . lock ( ) ;
417
+ if ( * rec) . failed {
418
+ fail ! ( "Poisoned Exclusive::new - another task failed inside!" ) ;
442
419
}
420
+ ( * rec) . failed = true ;
421
+ let result = f ( & mut ( * rec) . data ) ;
422
+ ( * rec) . failed = false ;
423
+ result
443
424
}
444
425
445
426
#[ inline]
@@ -452,28 +433,28 @@ impl<T:Send> Exclusive<T> {
452
433
#[ inline]
453
434
pub unsafe fn hold_and_signal ( & self , f: |x: & mut T |) {
454
435
let rec = self . x . get ( ) ;
455
- do ( * rec) . lock . lock {
456
- if ( * rec) . failed {
457
- fail ! ( "Poisoned Exclusive::new - another task failed inside!" ) ;
458
- }
459
- ( * rec) . failed = true ;
460
- f ( & mut ( * rec) . data ) ;
461
- ( * rec) . failed = false ;
462
- ( * rec) . lock . signal ( ) ;
436
+ let _l = ( * rec) . lock . lock ( ) ;
437
+ if ( * rec) . failed {
438
+ fail ! ( "Poisoned Exclusive::new - another task failed inside!" ) ;
463
439
}
440
+ ( * rec) . failed = true ;
441
+ f ( & mut ( * rec) . data ) ;
442
+ ( * rec) . failed = false ;
443
+ ( * rec) . lock . signal ( ) ;
464
444
}
465
445
466
446
#[ inline]
467
447
pub unsafe fn hold_and_wait ( & self , f: |x: & T | -> bool ) {
468
448
let rec = self . x . get ( ) ;
469
- do ( * rec) . lock . lock_and_wait {
470
- if ( * rec) . failed {
471
- fail ! ( "Poisoned Exclusive::new - another task failed inside!" ) ;
472
- }
473
- ( * rec) . failed = true ;
474
- let result = f ( & ( * rec) . data ) ;
475
- ( * rec) . failed = false ;
476
- result
449
+ let mut l = ( * rec) . lock . lock ( ) ;
450
+ if ( * rec) . failed {
451
+ fail ! ( "Poisoned Exclusive::new - another task failed inside!" ) ;
452
+ }
453
+ ( * rec) . failed = true ;
454
+ let result = f ( & ( * rec) . data ) ;
455
+ ( * rec) . failed = false ;
456
+ if result {
457
+ l. wait ( ) ;
477
458
}
478
459
}
479
460
0 commit comments