Skip to content

Commit 7158eef

Browse files
committed
use parking_lot
1 parent 0665376 commit 7158eef

2 files changed

Lines changed: 15 additions & 6 deletions

File tree

Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,8 @@ keywords = ["park", "notify", "thread", "wake", "condition"]
1515
categories = ["concurrency"]
1616
readme = "README.md"
1717

18+
[dependencies]
19+
parking_lot = "0.11.0"
20+
1821
[dev-dependencies]
1922
easy-parallel = "3.0.0"

src/lib.rs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,11 @@ use std::fmt;
3737
use std::marker::PhantomData;
3838
use std::sync::atomic::AtomicUsize;
3939
use std::sync::atomic::Ordering::SeqCst;
40-
use std::sync::{Arc, Condvar, Mutex};
40+
use std::sync::Arc;
4141
use std::time::{Duration, Instant};
4242

43+
use parking_lot::{Condvar, Mutex};
44+
4345
/// Creates a parker and an associated unparker.
4446
///
4547
/// # Examples
@@ -280,7 +282,7 @@ impl Inner {
280282
}
281283

282284
// Otherwise we need to coordinate going to sleep.
283-
let mut m = self.lock.lock().unwrap();
285+
let mut m = self.lock.lock();
284286

285287
match self.state.compare_exchange(EMPTY, PARKED, SeqCst, SeqCst) {
286288
Ok(_) => {}
@@ -302,9 +304,13 @@ impl Inner {
302304
None => {
303305
loop {
304306
// Block the current thread on the conditional variable.
305-
m = self.cvar.wait(m).unwrap();
307+
self.cvar.wait(&mut m);
306308

307-
if self.state.compare_exchange(NOTIFIED, EMPTY, SeqCst, SeqCst).is_ok() {
309+
if self
310+
.state
311+
.compare_exchange(NOTIFIED, EMPTY, SeqCst, SeqCst)
312+
.is_ok()
313+
{
308314
// got a notification
309315
return true;
310316
}
@@ -314,7 +320,7 @@ impl Inner {
314320
// Wait with a timeout, and if we spuriously wake up or otherwise wake up from a
315321
// notification we just want to unconditionally set `state` back to `EMPTY`, either
316322
// consuming a notification or un-flagging ourselves as parked.
317-
let (_m, _result) = self.cvar.wait_timeout(m, timeout).unwrap();
323+
let _result = self.cvar.wait_for(&mut m, timeout);
318324

319325
match self.state.swap(EMPTY, SeqCst) {
320326
NOTIFIED => true, // got a notification
@@ -345,7 +351,7 @@ impl Inner {
345351
//
346352
// Releasing `lock` before the call to `notify_one` means that when the parked thread wakes
347353
// it doesn't get woken only to have to wait for us to release `lock`.
348-
drop(self.lock.lock().unwrap());
354+
drop(self.lock.lock());
349355
self.cvar.notify_one();
350356
true
351357
}

0 commit comments

Comments
 (0)