@@ -59,32 +59,6 @@ impl WaitTimeoutResult {
59
59
/// - Can be statically constructed (requires the `const_fn` nightly feature).
60
60
/// - Does not require any drop glue when dropped.
61
61
/// - Inline fast path for the uncontended case.
62
- ///
63
- /// # Examples
64
- ///
65
- /// ```
66
- /// use parking_lot::{Mutex, Condvar};
67
- /// use sync::Arc;
68
- /// use thread;
69
- ///
70
- /// let pair = Arc::new((Mutex::new(false), Condvar::new()));
71
- /// let pair2 = pair.clone();
72
- ///
73
- /// // Inside of our lock, spawn a new thread, and then wait for it to start
74
- /// thread::spawn(move|| {
75
- /// let &(ref lock, ref cvar) = &*pair2;
76
- /// let mut started = lock.lock();
77
- /// *started = true;
78
- /// cvar.notify_one();
79
- /// });
80
- ///
81
- /// // wait for the thread to start up
82
- /// let &(ref lock, ref cvar) = &*pair;
83
- /// let mut started = lock.lock();
84
- /// while !*started {
85
- /// cvar.wait(&mut started);
86
- /// }
87
- /// ```
88
62
pub struct Condvar {
89
63
state : AtomicPtr < RawMutex > ,
90
64
}
@@ -119,20 +93,6 @@ impl Condvar {
119
93
/// `notify_one` are not buffered in any way.
120
94
///
121
95
/// To wake up all threads, see `notify_all()`.
122
- ///
123
- /// # Examples
124
- ///
125
- /// ```
126
- /// use parking_lot::Condvar;
127
- ///
128
- /// let condvar = Condvar::new();
129
- ///
130
- /// // do something with condvar, share it with other threads
131
- ///
132
- /// if !condvar.notify_one() {
133
- /// println!("Nobody was listening for this.");
134
- /// }
135
- /// ```
136
96
#[ inline]
137
97
pub fn notify_one ( & self ) -> bool {
138
98
// Nothing to do if there are no waiting threads
@@ -421,7 +381,7 @@ mod tests {
421
381
422
382
#[ test]
423
383
fn notify_one ( ) {
424
- let m = Arc :: new ( Mutex :: INIT ) ;
384
+ let m = Arc :: new ( RawMutex :: INIT ) ;
425
385
let m2 = m. clone ( ) ;
426
386
let c = Arc :: new ( Condvar :: new ( ) ) ;
427
387
let c2 = c. clone ( ) ;
@@ -433,155 +393,87 @@ mod tests {
433
393
m2. unlock ( ) ;
434
394
} ) ;
435
395
c. wait ( & m) ;
436
- }
437
-
438
- #[ test]
439
- fn notify_all ( ) {
440
- const N : usize = 10 ;
441
-
442
- let data = Arc :: new ( ( Mutex :: new ( 0 ) , Condvar :: new ( ) ) ) ;
443
- let ( tx, rx) = channel ( ) ;
444
- for _ in 0 ..N {
445
- let data = data. clone ( ) ;
446
- let tx = tx. clone ( ) ;
447
- thread:: spawn ( move || {
448
- let & ( ref lock, ref cond) = & * data;
449
- let mut cnt = lock. lock ( ) ;
450
- * cnt += 1 ;
451
- if * cnt == N {
452
- tx. send ( ( ) ) . unwrap ( ) ;
453
- }
454
- while * cnt != 0 {
455
- cond. wait ( & mut cnt) ;
456
- }
457
- tx. send ( ( ) ) . unwrap ( ) ;
458
- } ) ;
459
- }
460
- drop ( tx) ;
461
-
462
- let & ( ref lock, ref cond) = & * data;
463
- rx. recv ( ) . unwrap ( ) ;
464
- let mut cnt = lock. lock ( ) ;
465
- * cnt = 0 ;
466
- cond. notify_all ( ) ;
467
- drop ( cnt) ;
468
-
469
- for _ in 0 ..N {
470
- rx. recv ( ) . unwrap ( ) ;
471
- }
396
+ m. unlock ( ) ;
472
397
}
473
398
474
399
#[ test]
475
400
fn notify_one_return_true ( ) {
476
- let m = Arc :: new ( Mutex :: new ( ( ) ) ) ;
401
+ let m = Arc :: new ( RawMutex :: INIT ) ;
477
402
let m2 = m. clone ( ) ;
478
403
let c = Arc :: new ( Condvar :: new ( ) ) ;
479
404
let c2 = c. clone ( ) ;
480
405
481
- let mut g = m. lock ( ) ;
406
+ m. lock ( ) ;
482
407
let _t = thread:: spawn ( move || {
483
- let _g = m2. lock ( ) ;
408
+ m2. lock ( ) ;
484
409
assert ! ( c2. notify_one( ) ) ;
410
+ m2. unlock ( ) ;
485
411
} ) ;
486
- c. wait ( & mut g) ;
412
+ c. wait ( & m) ;
413
+ m. unlock ( ) ;
487
414
}
488
415
489
416
#[ test]
490
417
fn notify_one_return_false ( ) {
491
- let m = Arc :: new ( Mutex :: new ( ( ) ) ) ;
418
+ let m = Arc :: new ( RawMutex :: INIT ) ;
492
419
let c = Arc :: new ( Condvar :: new ( ) ) ;
493
420
494
421
let _t = thread:: spawn ( move || {
495
- let _g = m. lock ( ) ;
422
+ m. lock ( ) ;
496
423
assert ! ( !c. notify_one( ) ) ;
424
+ m. unlock ( ) ;
497
425
} ) ;
498
426
}
499
427
500
- #[ test]
501
- fn notify_all_return ( ) {
502
- const N : usize = 10 ;
503
-
504
- let data = Arc :: new ( ( Mutex :: new ( 0 ) , Condvar :: new ( ) ) ) ;
505
- let ( tx, rx) = channel ( ) ;
506
- for _ in 0 ..N {
507
- let data = data. clone ( ) ;
508
- let tx = tx. clone ( ) ;
509
- thread:: spawn ( move || {
510
- let & ( ref lock, ref cond) = & * data;
511
- let mut cnt = lock. lock ( ) ;
512
- * cnt += 1 ;
513
- if * cnt == N {
514
- tx. send ( ( ) ) . unwrap ( ) ;
515
- }
516
- while * cnt != 0 {
517
- cond. wait ( & mut cnt) ;
518
- }
519
- tx. send ( ( ) ) . unwrap ( ) ;
520
- } ) ;
521
- }
522
- drop ( tx) ;
523
-
524
- let & ( ref lock, ref cond) = & * data;
525
- rx. recv ( ) . unwrap ( ) ;
526
- let mut cnt = lock. lock ( ) ;
527
- * cnt = 0 ;
528
- assert_eq ! ( cond. notify_all( ) , N ) ;
529
- drop ( cnt) ;
530
-
531
- for _ in 0 ..N {
532
- rx. recv ( ) . unwrap ( ) ;
533
- }
534
-
535
- assert_eq ! ( cond. notify_all( ) , 0 ) ;
536
- }
537
-
538
428
#[ test]
539
429
fn wait_for ( ) {
540
- let m = Arc :: new ( Mutex :: new ( ( ) ) ) ;
430
+ let m = Arc :: new ( RawMutex :: INIT ) ;
541
431
let m2 = m. clone ( ) ;
542
432
let c = Arc :: new ( Condvar :: new ( ) ) ;
543
433
let c2 = c. clone ( ) ;
544
434
545
- let mut g = m. lock ( ) ;
546
- let no_timeout = c. wait_for ( & mut g , Duration :: from_millis ( 1 ) ) ;
435
+ m. lock ( ) ;
436
+ let no_timeout = c. wait_for ( & m , Duration :: from_millis ( 1 ) ) ;
547
437
assert ! ( no_timeout. timed_out( ) ) ;
548
438
let _t = thread:: spawn ( move || {
549
- let _g = m2. lock ( ) ;
439
+ m2. lock ( ) ;
550
440
c2. notify_one ( ) ;
441
+ m2. unlock ( ) ;
551
442
} ) ;
552
- let timeout_res = c. wait_for ( & mut g , Duration :: from_millis ( u32:: max_value ( ) as u64 ) ) ;
443
+ let timeout_res = c. wait_for ( & m , Duration :: from_millis ( u32:: max_value ( ) as u64 ) ) ;
553
444
assert ! ( !timeout_res. timed_out( ) ) ;
554
- drop ( g ) ;
445
+ m . unlock ( ) ;
555
446
}
556
447
557
448
#[ test]
558
449
fn wait_until ( ) {
559
- let m = Arc :: new ( Mutex :: new ( ( ) ) ) ;
450
+ let m = Arc :: new ( RawMutex :: INIT ) ;
560
451
let m2 = m. clone ( ) ;
561
452
let c = Arc :: new ( Condvar :: new ( ) ) ;
562
453
let c2 = c. clone ( ) ;
563
454
564
- let mut g = m. lock ( ) ;
565
- let no_timeout = c. wait_until ( & mut g , Instant :: now ( ) + Duration :: from_millis ( 1 ) ) ;
455
+ m. lock ( ) ;
456
+ let no_timeout = c. wait_until ( & m , Instant :: now ( ) + Duration :: from_millis ( 1 ) ) ;
566
457
assert ! ( no_timeout. timed_out( ) ) ;
567
458
let _t = thread:: spawn ( move || {
568
- let _g = m2. lock ( ) ;
459
+ m2. lock ( ) ;
569
460
c2. notify_one ( ) ;
461
+ m2. unlock ( ) ;
570
462
} ) ;
571
463
let timeout_res = c. wait_until (
572
- & mut g ,
464
+ & m ,
573
465
Instant :: now ( ) + Duration :: from_millis ( u32:: max_value ( ) as u64 ) ,
574
466
) ;
575
467
assert ! ( !timeout_res. timed_out( ) ) ;
576
- drop ( g ) ;
468
+ m . unlock ( ) ;
577
469
}
578
470
579
471
#[ test]
580
472
#[ should_panic]
581
473
fn two_mutexes ( ) {
582
- let m = Arc :: new ( Mutex :: new ( ( ) ) ) ;
474
+ let m = Arc :: new ( RawMutex :: INIT ) ;
583
475
let m2 = m. clone ( ) ;
584
- let m3 = Arc :: new ( Mutex :: new ( ( ) ) ) ;
476
+ let m3 = Arc :: new ( RawMutex :: INIT ) ;
585
477
let c = Arc :: new ( Condvar :: new ( ) ) ;
586
478
let c2 = c. clone ( ) ;
587
479
@@ -594,36 +486,43 @@ mod tests {
594
486
}
595
487
596
488
let ( tx, rx) = channel ( ) ;
597
- let g = m. lock ( ) ;
489
+ m. lock ( ) ;
598
490
let _t = thread:: spawn ( move || {
599
- let mut g = m2. lock ( ) ;
491
+ m2. lock ( ) ;
600
492
tx. send ( ( ) ) . unwrap ( ) ;
601
- c2. wait ( & mut g) ;
493
+ c2. wait ( & m2) ;
494
+ m2. unlock ( ) ;
602
495
} ) ;
603
- drop ( g ) ;
496
+ m . unlock ( ) ;
604
497
rx. recv ( ) . unwrap ( ) ;
605
- let _g = m. lock ( ) ;
498
+ m. lock ( ) ;
606
499
let _guard = PanicGuard ( & * c) ;
607
- let _ = c. wait ( & mut m3. lock ( ) ) ;
500
+ m3. lock ( ) ;
501
+ let _ = c. wait ( & m3) ;
502
+ m3. unlock ( ) ;
503
+ m. unlock ( ) ;
608
504
}
609
505
610
506
#[ test]
611
507
fn two_mutexes_disjoint ( ) {
612
- let m = Arc :: new ( Mutex :: new ( ( ) ) ) ;
508
+ let m = Arc :: new ( RawMutex :: INIT ) ;
613
509
let m2 = m. clone ( ) ;
614
- let m3 = Arc :: new ( Mutex :: new ( ( ) ) ) ;
510
+ let m3 = Arc :: new ( RawMutex :: INIT ) ;
615
511
let c = Arc :: new ( Condvar :: new ( ) ) ;
616
512
let c2 = c. clone ( ) ;
617
513
618
- let mut g = m. lock ( ) ;
514
+ m. lock ( ) ;
619
515
let _t = thread:: spawn ( move || {
620
- let _g = m2. lock ( ) ;
516
+ m2. lock ( ) ;
621
517
c2. notify_one ( ) ;
518
+ m2. unlock ( ) ;
622
519
} ) ;
623
- c. wait ( & mut g ) ;
624
- drop ( g ) ;
520
+ c. wait ( & m ) ;
521
+ m . unlock ( ) ;
625
522
626
- let _ = c. wait_for ( & mut m3. lock ( ) , Duration :: from_millis ( 1 ) ) ;
523
+ m3. lock ( ) ;
524
+ let _ = c. wait_for ( & m3, Duration :: from_millis ( 1 ) ) ;
525
+ m3. unlock ( ) ;
627
526
}
628
527
629
528
#[ test]
0 commit comments