Skip to content

Commit

Permalink
whole codebase is now ready for loom testing
Browse files Browse the repository at this point in the history
  • Loading branch information
glendc committed Sep 14, 2023
1 parent e82dd85 commit 10d96c8
Show file tree
Hide file tree
Showing 9 changed files with 48 additions and 55 deletions.
9 changes: 6 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,17 @@ keywords = ["io", "async", "non-blocking", "futures"]
license = "MIT OR Apache-2.0"
repository = "https://github.com/plabayo/tokio-graceful"

[target.'cfg(loom)'.dependencies]
loom = "0.7"

[dependencies]
pin-project-lite = "0.2.13"
slab = "0.4.9"
pin-project-lite = "0.2"
slab = "0.4"
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.5"
rand = "0.8"
tokio = { version = "1", features = ["net", "rt-multi-thread", "io-util", "test-util"] }
tracing-subscriber = { version = "0.3", features = ["env-filter"] }
2 changes: 1 addition & 1 deletion examples/waitgroup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ async fn main() {
// NOTE: you can also manually create
// a guard using `shutdown.guard()` and spawn
// you async tasks manually in case you do not wish to run these
// using `tokio::spawn`.
// using `tokio_graceful::sync::spawn` (Tokio by default).
let sleep = tokio::time::sleep(Duration::from_secs(countdown));
shutdown.spawn_task(async move {
sleep.await;
Expand Down
40 changes: 13 additions & 27 deletions src/guard.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
use std::{future::Future, mem::ManuallyDrop};

use tokio::task::JoinHandle;

use crate::{
sync::{Arc, AtomicUsize, Ordering},
sync::{Arc, AtomicUsize, JoinHandle, Ordering},
trigger::{Receiver, Sender},
};

Expand Down Expand Up @@ -58,80 +56,68 @@ impl ShutdownGuard {
self.0.cancelled().await
}

/// Returns a Tokio [`JoinHandle`] that can be awaited on
/// Returns a [`crate::sync::JoinHandle`] that can be awaited on
/// to wait for the spawned task to complete. See
/// [`tokio::spawn`] for more information.
///
/// [`JoinHandle`]: https://docs.rs/tokio/*/tokio/task/struct.JoinHandle.html
/// [`tokio::spawn`]: https://docs.rs/tokio/*/tokio/task/fn.spawn.html
/// [`crate::sync::spawn`] for more information.
pub fn spawn_task<T>(&self, task: T) -> JoinHandle<T::Output>
where
T: Future + Send + 'static,
T::Output: Send + 'static,
{
let guard = self.clone();
tokio::spawn(async move {
crate::sync::spawn(async move {
let output = task.await;
drop(guard);
output
})
}

/// Returns a Tokio [`JoinHandle`] that can be awaited on
/// Returns a Tokio [`crate::sync::JoinHandle`] that can be awaited on
/// to wait for the spawned task (future) to complete. See
/// [`tokio::spawn`] for more information.
/// [`crate::sync::spawn`] for more information.
///
/// In contrast to [`ShutdownGuard::spawn_task`] this method consumes the guard,
/// ensuring the guard is dropped once the task future is fulfilled.
///
/// [`JoinHandle`]: https://docs.rs/tokio/*/tokio/task/struct.JoinHandle.html
/// [`tokio::spawn`]: https://docs.rs/tokio/*/tokio/task/fn.spawn.html
/// [`ShutdownGuard::spawn_task`]: crate::ShutdownGuard::spawn_task
pub fn into_spawn_task<T>(self, task: T) -> JoinHandle<T::Output>
where
T: Future + Send + 'static,
T::Output: Send + 'static,
{
tokio::spawn(async move {
crate::sync::spawn(async move {
let output = task.await;
drop(self);
output
})
}

/// Returns a Tokio [`JoinHandle`] that can be awaited on
/// Returns a Tokio [`crate::sync::JoinHandle`] that can be awaited on
/// to wait for the spawned task (fn) to complete. See
/// [`tokio::spawn`] for more information.
///
/// [`JoinHandle`]: https://docs.rs/tokio/*/tokio/task/struct.JoinHandle.html
/// [`tokio::spawn`]: https://docs.rs/tokio/*/tokio/task/fn.spawn.html
/// [`crate::sync::spawn`] for more information.
pub fn spawn_task_fn<F, T>(&self, task: F) -> JoinHandle<T::Output>
where
F: FnOnce(ShutdownGuard) -> T + Send + 'static,
T: Future + Send + 'static,
T::Output: Send + 'static,
{
let guard = self.clone();
tokio::spawn(async move { task(guard).await })
crate::sync::spawn(async move { task(guard).await })
}

/// Returns a Tokio [`JoinHandle`] that can be awaited on
/// Returns a Tokio [`crate::sync::JoinHandle`] that can be awaited on
/// to wait for the spawned task (fn) to complete. See
/// [`tokio::spawn`] for more information.
/// [`crate::sync::spawn`] for more information.
///
/// In contrast to [`ShutdownGuard::spawn_task_fn`] this method consumes the guard,
/// ensuring the guard is dropped once the task future is fulfilled.
///
/// [`JoinHandle`]: https://docs.rs/tokio/*/tokio/task/struct.JoinHandle.html
/// [`tokio::spawn`]: https://docs.rs/tokio/*/tokio/task/fn.spawn.html
/// [`ShutdownGuard::spawn_task_fn`]: crate::ShutdownGuard::spawn_task_fn
pub fn into_spawn_task_fn<F, T>(self, task: F) -> JoinHandle<T::Output>
where
F: FnOnce(ShutdownGuard) -> T + Send + 'static,
T: Future + Send + 'static,
T::Output: Send + 'static,
{
tokio::spawn(async move { task(self).await })
crate::sync::spawn(async move { task(self).await })
}

/// Downgrades the guard to a [`WeakShutdownGuard`],
Expand Down
4 changes: 2 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ mod tests {
let shutdown = Shutdown::new(async {
rx.await.unwrap();
});
tokio::spawn(async move {
crate::sync::spawn(async move {
tx.send(()).unwrap();
});
shutdown.shutdown().await;
Expand All @@ -75,7 +75,7 @@ mod tests {
let shutdown = Shutdown::new(async {
rx.await.unwrap();
});
tokio::spawn(async move {
crate::sync::spawn(async move {
tx.send(()).unwrap();
});
shutdown
Expand Down
16 changes: 5 additions & 11 deletions src/shutdown.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ impl Shutdown {

let guard = ShutdownGuard::new(signal_rx, zero_tx, Arc::new(0usize.into()));

tokio::spawn(async move {
crate::sync::spawn(async move {
signal.await;
signal_tx.trigger();
});
Expand Down Expand Up @@ -81,12 +81,9 @@ impl Shutdown {
self.guard.clone_weak()
}

/// Returns a Tokio [`JoinHandle`] that can be awaited on
/// Returns a Tokio [`crate::sync::JoinHandle`] that can be awaited on
/// to wait for the spawned task to complete. See
/// [`tokio::spawn`] for more information.
///
/// [`JoinHandle`]: https://docs.rs/tokio/*/tokio/task/struct.JoinHandle.html
/// [`tokio::spawn`]: https://docs.rs/tokio/*/tokio/task/fn.spawn.html
/// [`crate::sync::spawn`] for more information.
#[inline]
pub fn spawn_task<T>(&self, task: T) -> tokio::task::JoinHandle<T::Output>
where
Expand All @@ -96,12 +93,9 @@ impl Shutdown {
self.guard.spawn_task(task)
}

/// Returns a Tokio [`JoinHandle`] that can be awaited on
/// Returns a Tokio [`crate::sync::JoinHandle`] that can be awaited on
/// to wait for the spawned task (fn) to complete. See
/// [`tokio::spawn`] for more information.
///
/// [`JoinHandle`]: https://docs.rs/tokio/*/tokio/task/struct.JoinHandle.html
/// [`tokio::spawn`]: https://docs.rs/tokio/*/tokio/task/fn.spawn.html
/// [`crate::sync::spawn`] for more information.
#[inline]
pub fn spawn_task_fn<T, F>(&self, task: F) -> tokio::task::JoinHandle<T::Output>
where
Expand Down
11 changes: 0 additions & 11 deletions src/sync.rs

This file was deleted.

6 changes: 6 additions & 0 deletions src/sync/default.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
pub use std::sync::{
atomic::{AtomicBool, AtomicUsize, Ordering},
Arc, Mutex,
};

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

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

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

0 comments on commit 10d96c8

Please sign in to comment.