diff options
author | Carl Lerche <me@carllerche.com> | 2019-11-16 23:35:17 -0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-11-16 23:35:17 -0800 |
commit | c147be04374534cf7062ea5e7062c4767ba0e2f7 (patch) | |
tree | f22b66d2bf53bf88b8828a9e9f71f305168d0c73 /tokio/src/sync | |
parent | b1d9e55487d8411da89788ce0fbb3eb99a1f3a40 (diff) |
make AtomicWaker private (#1782)
Diffstat (limited to 'tokio/src/sync')
-rw-r--r-- | tokio/src/sync/mod.rs | 2 | ||||
-rw-r--r-- | tokio/src/sync/task/atomic_waker.rs | 13 | ||||
-rw-r--r-- | tokio/src/sync/task/mod.rs | 2 | ||||
-rw-r--r-- | tokio/src/sync/tests/atomic_waker.rs | 34 | ||||
-rw-r--r-- | tokio/src/sync/tests/mod.rs | 12 |
5 files changed, 54 insertions, 9 deletions
diff --git a/tokio/src/sync/mod.rs b/tokio/src/sync/mod.rs index 25f58ffd..cf003816 100644 --- a/tokio/src/sync/mod.rs +++ b/tokio/src/sync/mod.rs @@ -47,7 +47,7 @@ pub mod oneshot; pub mod semaphore; mod task; -pub use task::AtomicWaker; +pub(crate) use task::AtomicWaker; pub mod watch; diff --git a/tokio/src/sync/task/atomic_waker.rs b/tokio/src/sync/task/atomic_waker.rs index 88d083d6..eaad17c1 100644 --- a/tokio/src/sync/task/atomic_waker.rs +++ b/tokio/src/sync/task/atomic_waker.rs @@ -20,7 +20,7 @@ use std::task::Waker; /// /// A single `AtomicWaker` may be reused for any number of calls to `register` or /// `wake`. -pub struct AtomicWaker { +pub(crate) struct AtomicWaker { state: AtomicUsize, waker: CausalCell<Option<Waker>>, } @@ -132,7 +132,7 @@ const WAKING: usize = 0b10; impl AtomicWaker { /// Create an `AtomicWaker` - pub fn new() -> AtomicWaker { + pub(crate) fn new() -> AtomicWaker { AtomicWaker { state: AtomicUsize::new(WAITING), waker: CausalCell::new(None), @@ -142,7 +142,8 @@ impl AtomicWaker { /// Registers the current waker to be notified on calls to `wake`. /// /// This is the same as calling `register_task` with `task::current()`. - pub fn register(&self, waker: Waker) { + #[cfg(feature = "io-driver")] + pub(crate) fn register(&self, waker: Waker) { self.do_register(waker); } @@ -161,7 +162,7 @@ impl AtomicWaker { /// idea. Concurrent calls to `register` will attempt to register different /// tasks to be woken. One of the callers will win and have its task set, /// but there is no guarantee as to which caller will succeed. - pub fn register_by_ref(&self, waker: &Waker) { + pub(crate) fn register_by_ref(&self, waker: &Waker) { self.do_register(waker); } @@ -238,7 +239,7 @@ impl AtomicWaker { /// Wakes the task that last called `register`. /// /// If `register` has not been called yet, then this does nothing. - pub fn wake(&self) { + pub(crate) fn wake(&self) { debug!(" + wake"); if let Some(waker) = self.take_waker() { waker.wake(); @@ -247,7 +248,7 @@ impl AtomicWaker { /// Attempts to take the `Waker` value out of the `AtomicWaker` with the /// intention that the caller will wake the task later. - pub fn take_waker(&self) -> Option<Waker> { + pub(crate) fn take_waker(&self) -> Option<Waker> { debug!(" + take_waker"); // AcqRel ordering is used in order to acquire the value of the `waker` // cell as well as to establish a `release` ordering with whatever diff --git a/tokio/src/sync/task/mod.rs b/tokio/src/sync/task/mod.rs index 446ff712..a6bc6ed0 100644 --- a/tokio/src/sync/task/mod.rs +++ b/tokio/src/sync/task/mod.rs @@ -1,4 +1,4 @@ //! Thread-safe task notification primitives. mod atomic_waker; -pub use self::atomic_waker::AtomicWaker; +pub(crate) use self::atomic_waker::AtomicWaker; diff --git a/tokio/src/sync/tests/atomic_waker.rs b/tokio/src/sync/tests/atomic_waker.rs new file mode 100644 index 00000000..c832d62e --- /dev/null +++ b/tokio/src/sync/tests/atomic_waker.rs @@ -0,0 +1,34 @@ +use crate::sync::AtomicWaker; +use tokio_test::task; + +use std::task::Waker; + +trait AssertSend: Send {} +trait AssertSync: Send {} + +impl AssertSend for AtomicWaker {} +impl AssertSync for AtomicWaker {} + +impl AssertSend for Waker {} +impl AssertSync for Waker {} + +#[test] +fn basic_usage() { + let mut waker = task::spawn(AtomicWaker::new()); + + waker.enter(|cx, waker| waker.register_by_ref(cx.waker())); + waker.wake(); + + assert!(waker.is_woken()); +} + +#[test] +fn wake_without_register() { + let mut waker = task::spawn(AtomicWaker::new()); + waker.wake(); + + // Registering should not result in a notification + waker.enter(|cx, waker| waker.register_by_ref(cx.waker())); + + assert!(!waker.is_woken()); +} diff --git a/tokio/src/sync/tests/mod.rs b/tokio/src/sync/tests/mod.rs index 8e627cb8..8da739f9 100644 --- a/tokio/src/sync/tests/mod.rs +++ b/tokio/src/sync/tests/mod.rs @@ -1,7 +1,17 @@ -#![cfg(loom)] +#[cfg(not(loom))] +mod atomic_waker; +#[cfg(loom)] mod loom_atomic_waker; + +#[cfg(loom)] mod loom_list; + +#[cfg(loom)] mod loom_mpsc; + +#[cfg(loom)] mod loom_oneshot; + +#[cfg(loom)] mod loom_semaphore; |