Skip to content

Commit

Permalink
prepare trigger channel code for loom test
Browse files Browse the repository at this point in the history
and ensure rest of code can still just run fine as-is
  • Loading branch information
glendc committed Sep 14, 2023
1 parent 10d96c8 commit ffc7f90
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 11 deletions.
19 changes: 19 additions & 0 deletions .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,25 @@ jobs:
command: test
args: --all-features --examples

test-loom:
needs: check
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions-rs/toolchain@v1
with:
toolchain: ${{env.RUST_TOOLCHAIN}}
override: true
profile: minimal
- uses: Swatinem/rust-cache@v1
- name: Run tests
uses: actions-rs/cargo@v1
with:
command: test
args: test_loom --release
env:
RUSTFLAGS: --cfg loom

cargo-hack:
needs: check
runs-on: ubuntu-latest
Expand Down
4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ tokio = { version = "1", features = ["rt", "signal", "sync", "macros", "time"] }
tracing = "0.1"

[dev-dependencies]
hyper = { version = "0.14", features = [ "server", "tcp", "http1", "http2" ] }
rand = "0.8"
tokio = { version = "1", features = ["net", "rt-multi-thread", "io-util", "test-util"] }
tracing-subscriber = { version = "0.3", features = ["env-filter"] }

[target.'cfg(not(loom))'.dev-dependencies]
hyper = { version = "0.14", features = [ "server", "tcp", "http1", "http2" ] }
4 changes: 3 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ mod guard;
pub use guard::{ShutdownGuard, WeakShutdownGuard};

mod shutdown;
pub use shutdown::{default_signal, Shutdown};
#[cfg(not(loom))]
pub use shutdown::default_signal;
pub use shutdown::Shutdown;

pub(crate) mod sync;
pub(crate) mod trigger;
Expand Down
10 changes: 7 additions & 3 deletions src/shutdown.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::{future::Future, time};

use crate::{
sync::Arc,
sync::{Arc, JoinHandle},
trigger::{trigger, Receiver},
ShutdownGuard, WeakShutdownGuard,
};
Expand All @@ -13,6 +13,8 @@ use crate::{
/// create a [`Shutdown`] with [`Shutdown::default`], which uses the default
/// signal handler to trigger shutdown. See [`default_signal`] for more info.
///
/// > (NOTE: that these defaults are not available when compiling with --cfg loom)
///
/// See the [README] for more info on how to use this crate.
///
/// [`Future`]: std::future::Future
Expand Down Expand Up @@ -85,7 +87,7 @@ impl Shutdown {
/// to wait for the spawned task to complete. See
/// [`crate::sync::spawn`] for more information.
#[inline]
pub fn spawn_task<T>(&self, task: T) -> tokio::task::JoinHandle<T::Output>
pub fn spawn_task<T>(&self, task: T) -> JoinHandle<T::Output>
where
T: Future + Send + 'static,
T::Output: Send + 'static,
Expand All @@ -97,7 +99,7 @@ impl Shutdown {
/// to wait for the spawned task (fn) to complete. See
/// [`crate::sync::spawn`] for more information.
#[inline]
pub fn spawn_task_fn<T, F>(&self, task: F) -> tokio::task::JoinHandle<T::Output>
pub fn spawn_task_fn<T, F>(&self, task: F) -> JoinHandle<T::Output>
where
T: Future + Send + 'static,
T::Output: Send + 'static,
Expand Down Expand Up @@ -182,6 +184,7 @@ impl Shutdown {
///
/// [`Future`]: std::future::Future
/// [`tokio::time::sleep`]: https://docs.rs/tokio/*/tokio/time/fn.sleep.html
#[cfg(not(loom))]
pub async fn default_signal() {
let ctrl_c = tokio::signal::ctrl_c();
let signal = async {
Expand All @@ -196,6 +199,7 @@ pub async fn default_signal() {
}
}

#[cfg(not(loom))]
impl Default for Shutdown {
fn default() -> Self {
Self::new(default_signal())
Expand Down
2 changes: 0 additions & 2 deletions src/sync/default.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,3 @@ pub use std::sync::{
atomic::{AtomicBool, AtomicUsize, Ordering},
Arc, Mutex,
};

pub use tokio::task::{spawn, JoinHandle};
4 changes: 1 addition & 3 deletions src/sync/loom.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
pub use loom::sync::{
atomic::{AtomicBool, AtomicUsize, Ordering},
Arc, Mutex, Ordering,
Arc, Mutex,
};

pub use loom::task::{spawn, JoinHandle};
4 changes: 3 additions & 1 deletion src/sync/mod.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
#[cfg(loom)]
mod loom;
#[cfg(loom)]
pub use loom::*;
pub use self::loom::*;

#[cfg(not(loom))]
mod default;
#[cfg(not(loom))]
pub use default::*;

pub use tokio::task::{spawn, JoinHandle};
8 changes: 8 additions & 0 deletions src/trigger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -213,3 +213,11 @@ pub fn trigger() -> (Sender, Receiver) {

(sender, receiver)
}

#[cfg(all(test, loom))]
mod loom_tests {
use super::*;

#[test]
fn test_loom_concurrent() {}
}

0 comments on commit ffc7f90

Please sign in to comment.