Skip to content

Implement loom version of parking #8

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Oct 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ jobs:
if: startsWith(matrix.rust, 'nightly')
run: cargo check -Z features=dev_dep
- run: cargo test
- run: cargo test --test loom --features loom
env:
RUSTFLAGS: --cfg loom
LOOM_MAX_PREEMPTIONS: 2

msrv:
runs-on: ubuntu-latest
Expand Down
7 changes: 7 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,12 @@ keywords = ["park", "notify", "thread", "wake", "condition"]
categories = ["concurrency"]
exclude = ["/.*"]

# The `loom` feature, combined with the `loom` rustflag, enables a reimplementation
# of `parking` using `loom`. This feature is perma-unstable and should not be used
# in stable code.
[target.'cfg(loom)'.dependencies.loom]
version = "0.5"
optional = true

[dev-dependencies]
easy-parallel = "3.0.0"
47 changes: 34 additions & 13 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,23 @@
#![forbid(unsafe_code)]
#![warn(missing_docs, missing_debug_implementations, rust_2018_idioms)]

#[cfg(not(all(loom, feature = "loom")))]
use std::sync;

#[cfg(all(loom, feature = "loom"))]
use loom::sync;

use std::cell::Cell;
use std::fmt;
use std::marker::PhantomData;
use std::sync::atomic::AtomicUsize;
use std::sync::atomic::Ordering::SeqCst;
use std::sync::{Arc, Condvar, Mutex};
use std::time::{Duration, Instant};
use std::time::Duration;

#[cfg(not(all(loom, feature = "loom")))]
use std::time::Instant;

use sync::atomic::AtomicUsize;
use sync::atomic::Ordering::SeqCst;
use sync::{Arc, Condvar, Mutex};

/// Creates a parker and an associated unparker.
///
Expand Down Expand Up @@ -119,6 +129,7 @@ impl Parker {
/// // Wait for a notification, or time out after 500 ms.
/// p.park_timeout(Duration::from_millis(500));
/// ```
#[cfg(not(loom))]
pub fn park_timeout(&self, duration: Duration) -> bool {
self.unparker.inner.park(Some(duration))
}
Expand All @@ -138,6 +149,7 @@ impl Parker {
/// // Wait for a notification, or time out after 500 ms.
/// p.park_deadline(Instant::now() + Duration::from_millis(500));
/// ```
#[cfg(not(loom))]
pub fn park_deadline(&self, instant: Instant) -> bool {
self.unparker
.inner
Expand Down Expand Up @@ -315,15 +327,24 @@ impl Inner {
}
}
Some(timeout) => {
// Wait with a timeout, and if we spuriously wake up or otherwise wake up from a
// notification we just want to unconditionally set `state` back to `EMPTY`, either
// consuming a notification or un-flagging ourselves as parked.
let (_m, _result) = self.cvar.wait_timeout(m, timeout).unwrap();

match self.state.swap(EMPTY, SeqCst) {
NOTIFIED => true, // got a notification
PARKED => false, // no notification
n => panic!("inconsistent park_timeout state: {}", n),
#[cfg(not(loom))]
{
// Wait with a timeout, and if we spuriously wake up or otherwise wake up from a
// notification we just want to unconditionally set `state` back to `EMPTY`, either
// consuming a notification or un-flagging ourselves as parked.
let (_m, _result) = self.cvar.wait_timeout(m, timeout).unwrap();

match self.state.swap(EMPTY, SeqCst) {
NOTIFIED => true, // got a notification
PARKED => false, // no notification
n => panic!("inconsistent park_timeout state: {}", n),
}
}

#[cfg(loom)]
{
let _ = timeout;
panic!("park_timeout is not supported under loom");
}
}
}
Expand Down
14 changes: 14 additions & 0 deletions tests/loom.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#![cfg(loom)]

#[test]
fn smoke() {
loom::model(|| {
let (p, u) = parking::pair();

loom::thread::spawn(move || {
p.park();
});

u.unpark();
});
}