Skip to content

Commit 2df24c7

Browse files
committed
Fix condvar tests
1 parent c0bd074 commit 2df24c7

File tree

1 file changed

+47
-148
lines changed

1 file changed

+47
-148
lines changed

src/libstd/sys_common/parking_lot/condvar.rs

Lines changed: 47 additions & 148 deletions
Original file line numberDiff line numberDiff line change
@@ -59,32 +59,6 @@ impl WaitTimeoutResult {
5959
/// - Can be statically constructed (requires the `const_fn` nightly feature).
6060
/// - Does not require any drop glue when dropped.
6161
/// - 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-
/// ```
8862
pub struct Condvar {
8963
state: AtomicPtr<RawMutex>,
9064
}
@@ -119,20 +93,6 @@ impl Condvar {
11993
/// `notify_one` are not buffered in any way.
12094
///
12195
/// 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-
/// ```
13696
#[inline]
13797
pub fn notify_one(&self) -> bool {
13898
// Nothing to do if there are no waiting threads
@@ -421,7 +381,7 @@ mod tests {
421381

422382
#[test]
423383
fn notify_one() {
424-
let m = Arc::new(Mutex::INIT);
384+
let m = Arc::new(RawMutex::INIT);
425385
let m2 = m.clone();
426386
let c = Arc::new(Condvar::new());
427387
let c2 = c.clone();
@@ -433,155 +393,87 @@ mod tests {
433393
m2.unlock();
434394
});
435395
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();
472397
}
473398

474399
#[test]
475400
fn notify_one_return_true() {
476-
let m = Arc::new(Mutex::new(()));
401+
let m = Arc::new(RawMutex::INIT);
477402
let m2 = m.clone();
478403
let c = Arc::new(Condvar::new());
479404
let c2 = c.clone();
480405

481-
let mut g = m.lock();
406+
m.lock();
482407
let _t = thread::spawn(move || {
483-
let _g = m2.lock();
408+
m2.lock();
484409
assert!(c2.notify_one());
410+
m2.unlock();
485411
});
486-
c.wait(&mut g);
412+
c.wait(&m);
413+
m.unlock();
487414
}
488415

489416
#[test]
490417
fn notify_one_return_false() {
491-
let m = Arc::new(Mutex::new(()));
418+
let m = Arc::new(RawMutex::INIT);
492419
let c = Arc::new(Condvar::new());
493420

494421
let _t = thread::spawn(move || {
495-
let _g = m.lock();
422+
m.lock();
496423
assert!(!c.notify_one());
424+
m.unlock();
497425
});
498426
}
499427

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-
538428
#[test]
539429
fn wait_for() {
540-
let m = Arc::new(Mutex::new(()));
430+
let m = Arc::new(RawMutex::INIT);
541431
let m2 = m.clone();
542432
let c = Arc::new(Condvar::new());
543433
let c2 = c.clone();
544434

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));
547437
assert!(no_timeout.timed_out());
548438
let _t = thread::spawn(move || {
549-
let _g = m2.lock();
439+
m2.lock();
550440
c2.notify_one();
441+
m2.unlock();
551442
});
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));
553444
assert!(!timeout_res.timed_out());
554-
drop(g);
445+
m.unlock();
555446
}
556447

557448
#[test]
558449
fn wait_until() {
559-
let m = Arc::new(Mutex::new(()));
450+
let m = Arc::new(RawMutex::INIT);
560451
let m2 = m.clone();
561452
let c = Arc::new(Condvar::new());
562453
let c2 = c.clone();
563454

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));
566457
assert!(no_timeout.timed_out());
567458
let _t = thread::spawn(move || {
568-
let _g = m2.lock();
459+
m2.lock();
569460
c2.notify_one();
461+
m2.unlock();
570462
});
571463
let timeout_res = c.wait_until(
572-
&mut g,
464+
&m,
573465
Instant::now() + Duration::from_millis(u32::max_value() as u64),
574466
);
575467
assert!(!timeout_res.timed_out());
576-
drop(g);
468+
m.unlock();
577469
}
578470

579471
#[test]
580472
#[should_panic]
581473
fn two_mutexes() {
582-
let m = Arc::new(Mutex::new(()));
474+
let m = Arc::new(RawMutex::INIT);
583475
let m2 = m.clone();
584-
let m3 = Arc::new(Mutex::new(()));
476+
let m3 = Arc::new(RawMutex::INIT);
585477
let c = Arc::new(Condvar::new());
586478
let c2 = c.clone();
587479

@@ -594,36 +486,43 @@ mod tests {
594486
}
595487

596488
let (tx, rx) = channel();
597-
let g = m.lock();
489+
m.lock();
598490
let _t = thread::spawn(move || {
599-
let mut g = m2.lock();
491+
m2.lock();
600492
tx.send(()).unwrap();
601-
c2.wait(&mut g);
493+
c2.wait(&m2);
494+
m2.unlock();
602495
});
603-
drop(g);
496+
m.unlock();
604497
rx.recv().unwrap();
605-
let _g = m.lock();
498+
m.lock();
606499
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();
608504
}
609505

610506
#[test]
611507
fn two_mutexes_disjoint() {
612-
let m = Arc::new(Mutex::new(()));
508+
let m = Arc::new(RawMutex::INIT);
613509
let m2 = m.clone();
614-
let m3 = Arc::new(Mutex::new(()));
510+
let m3 = Arc::new(RawMutex::INIT);
615511
let c = Arc::new(Condvar::new());
616512
let c2 = c.clone();
617513

618-
let mut g = m.lock();
514+
m.lock();
619515
let _t = thread::spawn(move || {
620-
let _g = m2.lock();
516+
m2.lock();
621517
c2.notify_one();
518+
m2.unlock();
622519
});
623-
c.wait(&mut g);
624-
drop(g);
520+
c.wait(&m);
521+
m.unlock();
625522

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();
627526
}
628527

629528
#[test]

0 commit comments

Comments
 (0)