Skip to content

Commit

Permalink
sync: move CancellationToken to tokio-util (#2721)
Browse files Browse the repository at this point in the history
* sync: move CancellationToken to tokio-util

The `CancellationToken` utility is only available with the
`tokio_unstable` flag. This was done as the API is not final, but it
adds friction for users.

This patch moves `CancellationToken` to tokio-util where it is generally
available. The tokio-util crate does not have any constraints on
breaking change releases.

* fix clippy

* clippy again
  • Loading branch information
carllerche committed Aug 23, 2020
1 parent fde72bf commit 9d58b70
Show file tree
Hide file tree
Showing 15 changed files with 23 additions and 29 deletions.
1 change: 1 addition & 0 deletions tokio-util/Cargo.toml
Expand Up @@ -46,6 +46,7 @@ tokio = { version = "0.3.0", path = "../tokio", features = ["full"] }
tokio-test = { version = "0.3.0", path = "../tokio-test" }

futures = "0.3.0"
futures-test = "0.3.5"

[package.metadata.docs.rs]
all-features = true
Expand Down
4 changes: 4 additions & 0 deletions tokio-util/src/lib.rs
Expand Up @@ -24,6 +24,8 @@
#[macro_use]
mod cfg;

mod loom;

cfg_codec! {
pub mod codec;
}
Expand All @@ -35,3 +37,5 @@ cfg_udp! {
cfg_compat! {
pub mod compat;
}

pub mod sync;
1 change: 1 addition & 0 deletions tokio-util/src/loom.rs
@@ -0,0 +1 @@
pub(crate) use std::sync;
Expand Up @@ -3,7 +3,7 @@

use crate::loom::sync::atomic::AtomicUsize;
use crate::loom::sync::Mutex;
use crate::util::intrusive_double_linked_list::{LinkedList, ListNode};
use crate::sync::intrusive_double_linked_list::{LinkedList, ListNode};

use core::future::Future;
use core::pin::Pin;
Expand Down Expand Up @@ -129,6 +129,12 @@ impl Drop for CancellationToken {
}
}

impl Default for CancellationToken {
fn default() -> CancellationToken {
CancellationToken::new()
}
}

impl CancellationToken {
/// Creates a new CancellationToken in the non-cancelled state.
pub fn new() -> CancellationToken {
Expand Down
6 changes: 6 additions & 0 deletions tokio-util/src/sync/mod.rs
@@ -0,0 +1,6 @@
//! Synchronization primitives

mod cancellation_token;
pub use cancellation_token::{CancellationToken, WaitForCancellationFuture};

mod intrusive_double_linked_list;
1 change: 1 addition & 0 deletions tokio-util/src/sync/tests/mod.rs
@@ -0,0 +1 @@

@@ -1,7 +1,5 @@
#![cfg(tokio_unstable)]

use tokio::pin;
use tokio::sync::CancellationToken;
use tokio_util::sync::CancellationToken;

use core::future::Future;
use core::task::{Context, Poll};
Expand Down Expand Up @@ -186,8 +184,8 @@ fn drop_multiple_child_tokens() {
for drop_first_child_first in &[true, false] {
let token = CancellationToken::new();
let mut child_tokens = [None, None, None];
for i in 0..child_tokens.len() {
child_tokens[i] = Some(token.child_token());
for child in &mut child_tokens {
*child = Some(token.child_token());
}

assert!(!token.is_cancelled());
Expand Down
1 change: 0 additions & 1 deletion tokio/Cargo.toml
Expand Up @@ -125,7 +125,6 @@ optional = true
[dev-dependencies]
tokio-test = { version = "0.3.0", path = "../tokio-test" }
futures = { version = "0.3.0", features = ["async-await"] }
futures-test = "0.3.0"
proptest = "0.9.4"
tempfile = "3.1.0"

Expand Down
10 changes: 0 additions & 10 deletions tokio/src/macros/cfg.rs
Expand Up @@ -354,16 +354,6 @@ macro_rules! cfg_uds {
}
}

macro_rules! cfg_unstable {
($($item:item)*) => {
$(
#[cfg(tokio_unstable)]
#[cfg_attr(docsrs, doc(cfg(tokio_unstable)))]
$item
)*
}
}

macro_rules! cfg_trace {
($($item:item)*) => {
$(
Expand Down
5 changes: 0 additions & 5 deletions tokio/src/sync/mod.rs
Expand Up @@ -434,11 +434,6 @@ cfg_sync! {

pub mod broadcast;

cfg_unstable! {
mod cancellation_token;
pub use cancellation_token::{CancellationToken, WaitForCancellationFuture};
}

pub mod mpsc;

mod mutex;
Expand Down
2 changes: 0 additions & 2 deletions tokio/src/sync/tests/mod.rs
Expand Up @@ -7,8 +7,6 @@ cfg_not_loom! {
cfg_loom! {
mod loom_atomic_waker;
mod loom_broadcast;
#[cfg(tokio_unstable)]
mod loom_cancellation_token;
mod loom_list;
mod loom_mpsc;
mod loom_notify;
Expand Down
2 changes: 0 additions & 2 deletions tokio/src/util/mod.rs
Expand Up @@ -24,5 +24,3 @@ pub(crate) mod trace;
#[cfg(any(feature = "macros", feature = "stream"))]
#[cfg_attr(not(feature = "macros"), allow(unreachable_pub))]
pub use rand::thread_rng_n;

pub(crate) mod intrusive_double_linked_list;
3 changes: 0 additions & 3 deletions tokio/tests/async_send_sync.rs
Expand Up @@ -259,6 +259,3 @@ async_assert_fn!(tokio::time::timeout_at(Instant, BoxFutureSync<()>): Send & Syn
async_assert_fn!(tokio::time::timeout_at(Instant, BoxFutureSend<()>): Send & !Sync);
async_assert_fn!(tokio::time::timeout_at(Instant, BoxFuture<()>): !Send & !Sync);
async_assert_fn!(tokio::time::Interval::tick(_): Send & Sync);

#[cfg(tokio_unstable)]
assert_value!(tokio::sync::CancellationToken: Send & Sync);

0 comments on commit 9d58b70

Please sign in to comment.