id stringlengths 22 133 | text stringlengths 40 40.2k | arch stringclasses 1
value | syntax stringclasses 1
value | kind stringclasses 4
values | repo stringclasses 27
values | path stringlengths 5 116 | license stringclasses 6
values | commit stringlengths 40 40 | source_host stringclasses 1
value | category stringclasses 16
values | source_url stringlengths 85 196 | line_start int64 1 4.28k | line_end int64 4 4.31k |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
tokio-rs/tokio:tokio/src/sync/watch.rs:28 | /// assert!(!state_rx.has_changed().unwrap());
/// assert_eq!(state_rx.borrow_and_update().counter, 2);
/// ```
pub fn send_if_modified<F>(&self, modify: F) -> bool
where
F: FnOnce(&mut T) -> bool,
{
{
// Acquire the write lock and update the value.
let mut lo... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | bd4ccae184b0359cb88f9ebc2ba157867e1eee0e | github | async-runtime | https://github.com/tokio-rs/tokio/blob/bd4ccae184b0359cb88f9ebc2ba157867e1eee0e/tokio/src/sync/watch.rs | 1,081 | 1,140 |
tokio-rs/tokio:tokio/src/sync/watch.rs:34 | /// assert_eq!(2, tx.receiver_count());
/// }
/// ```
pub fn receiver_count(&self) -> usize {
self.shared.ref_count_rx.load(Relaxed)
}
/// Returns `true` if senders belong to the same channel.
///
/// # Examples
///
/// ```
/// let (tx, rx) = tokio::sync::watch::chan... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | bd4ccae184b0359cb88f9ebc2ba157867e1eee0e | github | async-runtime | https://github.com/tokio-rs/tokio/blob/bd4ccae184b0359cb88f9ebc2ba157867e1eee0e/tokio/src/sync/watch.rs | 1,321 | 1,380 |
tokio-rs/tokio:tokio/src/sync/watch.rs:35 | }
}
#[cfg(all(test, loom))]
mod tests {
use futures::future::FutureExt;
use loom::thread;
// test for https://github.com/tokio-rs/tokio/issues/3168
#[test]
fn watch_spurious_wakeup() {
loom::model(|| {
let (send, mut recv) = crate::sync::watch::channel(0i32);
send.... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | bd4ccae184b0359cb88f9ebc2ba157867e1eee0e | github | async-runtime | https://github.com/tokio-rs/tokio/blob/bd4ccae184b0359cb88f9ebc2ba157867e1eee0e/tokio/src/sync/watch.rs | 1,361 | 1,420 |
tokio-rs/tokio:tokio/src/sync/watch.rs:36 | });
}
#[test]
fn watch_borrow() {
loom::model(|| {
let (send, mut recv) = crate::sync::watch::channel(0i32);
assert!(send.borrow().eq(&0));
assert!(recv.borrow().eq(&0));
send.send(1).unwrap();
assert!(send.borrow().eq(&1));
... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | bd4ccae184b0359cb88f9ebc2ba157867e1eee0e | github | async-runtime | https://github.com/tokio-rs/tokio/blob/bd4ccae184b0359cb88f9ebc2ba157867e1eee0e/tokio/src/sync/watch.rs | 1,401 | 1,443 |
tokio-rs/tokio:tokio/src/sync/watch.rs:21 | ///
/// // in place of changed we have use `wait_for`
/// // which would automatically check the current value
/// // and wait for changes until the closure returns true.
/// assert!(rx2.wait_for(|val| *val == "goodbye").await.is_ok());
/// assert_eq!(*rx2.borrow(), "goodbye");
... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | 3bf4f938543bfb4c7122b7f21e9154f4a3b27122 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/3bf4f938543bfb4c7122b7f21e9154f4a3b27122/tokio/src/sync/watch.rs | 801 | 860 |
tokio-rs/tokio:tokio/src/sync/watch.rs:28 | /// assert!(!state_rx.has_changed().unwrap());
/// assert_eq!(state_rx.borrow_and_update().counter, 2);
/// ```
pub fn send_if_modified<F>(&self, modify: F) -> bool
where
F: FnOnce(&mut T) -> bool,
{
{
// Acquire the write lock and update the value.
let mut lo... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | 3bf4f938543bfb4c7122b7f21e9154f4a3b27122 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/3bf4f938543bfb4c7122b7f21e9154f4a3b27122/tokio/src/sync/watch.rs | 1,081 | 1,140 |
tokio-rs/tokio:tokio/src/sync/watch.rs:33 | /// tx.send(5).unwrap();
/// let mut rx = tx.subscribe();
///
/// tokio::spawn(async move {
/// // by spawning and sleeping, the message is sent after `main`
/// // hits the call to `changed`.
/// # if false {
/// tokio::time::sleep(Duration::from_... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | 17555d71d96ec45d02785e316317489dc8bba13f | github | async-runtime | https://github.com/tokio-rs/tokio/blob/17555d71d96ec45d02785e316317489dc8bba13f/tokio/src/sync/watch.rs | 1,281 | 1,340 |
tokio-rs/tokio:tokio/src/sync/watch.rs:34 | /// assert_eq!(2, tx.receiver_count());
/// }
/// ```
pub fn receiver_count(&self) -> usize {
self.shared.ref_count_rx.load(Relaxed)
}
}
impl<T> Drop for Sender<T> {
fn drop(&mut self) {
if self.shared.ref_count_tx.fetch_sub(1, AcqRel) == 1 {
self.shared.state.set_cl... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | 17555d71d96ec45d02785e316317489dc8bba13f | github | async-runtime | https://github.com/tokio-rs/tokio/blob/17555d71d96ec45d02785e316317489dc8bba13f/tokio/src/sync/watch.rs | 1,321 | 1,380 |
tokio-rs/tokio:tokio/src/sync/watch.rs:35 | let send_thread = thread::spawn(move || {
send.send(2).unwrap();
send
});
recv.changed().now_or_never();
let send = send_thread.join().unwrap();
let recv_thread = thread::spawn(move || {
recv.changed().now_or_never();
... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | 17555d71d96ec45d02785e316317489dc8bba13f | github | async-runtime | https://github.com/tokio-rs/tokio/blob/17555d71d96ec45d02785e316317489dc8bba13f/tokio/src/sync/watch.rs | 1,361 | 1,420 |
tokio-rs/tokio:tokio/src/sync/watch.rs:36 | send
});
recv.changed().now_or_never();
let send = send_thread.join().unwrap();
let recv_thread = thread::spawn(move || {
recv.changed().now_or_never();
recv.changed().now_or_never();
recv
});
send... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | 17555d71d96ec45d02785e316317489dc8bba13f | github | async-runtime | https://github.com/tokio-rs/tokio/blob/17555d71d96ec45d02785e316317489dc8bba13f/tokio/src/sync/watch.rs | 1,401 | 1,427 |
tokio-rs/tokio:tokio/src/sync/watch.rs:4 | use std::ops;
use std::panic;
/// Receives values from the associated [`Sender`](struct@Sender).
///
/// Instances are created by the [`channel`](fn@channel) function.
///
/// To turn this receiver into a `Stream`, you can use the [`WatchStream`]
/// wrapper.
///
/// [`WatchStream`]: https://docs.rs/tokio-stream/0.1/t... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | 9c6ff3b7e5e86dd2e0104a8ac0a034a3695cfe84 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/9c6ff3b7e5e86dd2e0104a8ac0a034a3695cfe84/tokio/src/sync/watch.rs | 121 | 180 |
tokio-rs/tokio:tokio/src/sync/watch.rs:7 | /// The current version.
///
/// The lowest bit represents a "closed" state. The rest of the bits
/// represent the current version.
state: AtomicState,
/// Tracks the number of `Receiver` instances.
ref_count_rx: AtomicUsize,
/// Tracks the number of `Sender` instances.
ref_count_tx: ... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | 9c6ff3b7e5e86dd2e0104a8ac0a034a3695cfe84 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/9c6ff3b7e5e86dd2e0104a8ac0a034a3695cfe84/tokio/src/sync/watch.rs | 241 | 300 |
tokio-rs/tokio:tokio/src/sync/watch.rs:8 | pub struct SendError<T>(pub T);
// ===== impl SendError =====
impl<T> fmt::Debug for SendError<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("SendError").finish_non_exhaustive()
}
}
impl<T> fmt::Display for SendError<T> {
fn fmt(&se... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | 9c6ff3b7e5e86dd2e0104a8ac0a034a3695cfe84 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/9c6ff3b7e5e86dd2e0104a8ac0a034a3695cfe84/tokio/src/sync/watch.rs | 281 | 340 |
tokio-rs/tokio:tokio/src/sync/watch.rs:9 | //
// Some simple benchmarks show that randomness performs slightly better than
// circular access (probably due to contention on `next`), so we prefer to
// use randomness when Tokio is compiled with a random number generator.
//
// When the random number generator is not available, we fall back to... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | 9c6ff3b7e5e86dd2e0104a8ac0a034a3695cfe84 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/9c6ff3b7e5e86dd2e0104a8ac0a034a3695cfe84/tokio/src/sync/watch.rs | 321 | 380 |
tokio-rs/tokio:tokio/src/sync/watch.rs:10 | /// This function implements the case where randomness is available.
#[cfg(all(not(loom), feature = "sync", any(feature = "rt", feature = "macros")))]
pub(super) fn notified(&self) -> Notified<'_> {
let i = crate::runtime::context::thread_rng_n(8) as usize;
self.inner[i].notified... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | 9c6ff3b7e5e86dd2e0104a8ac0a034a3695cfe84 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/9c6ff3b7e5e86dd2e0104a8ac0a034a3695cfe84/tokio/src/sync/watch.rs | 361 | 420 |
tokio-rs/tokio:tokio/src/sync/watch.rs:11 | impl Version {
/// Decrements the version.
pub(super) fn decrement(&mut self) {
// Using a wrapping decrement here is required to ensure that the
// operation is consistent with `std::sync::atomic::AtomicUsize::fetch_add()`
// which wraps on overflow.
self... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | 9c6ff3b7e5e86dd2e0104a8ac0a034a3695cfe84 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/9c6ff3b7e5e86dd2e0104a8ac0a034a3695cfe84/tokio/src/sync/watch.rs | 401 | 460 |
tokio-rs/tokio:tokio/src/sync/watch.rs:12 | StateSnapshot(self.0.load(Ordering::Acquire))
}
/// Increment the version counter.
pub(super) fn increment_version_while_locked(&self) {
// Use `Release` ordering to ensure that the shared value
// has been written before updating the version. The shared
// v... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | 9c6ff3b7e5e86dd2e0104a8ac0a034a3695cfe84 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/9c6ff3b7e5e86dd2e0104a8ac0a034a3695cfe84/tokio/src/sync/watch.rs | 441 | 500 |
tokio-rs/tokio:tokio/src/sync/watch.rs:13 | /// println!("{}! ", *rx.borrow_and_update());
/// if rx.changed().await.is_err() {
/// break;
/// }
/// }
/// });
///
/// sleep(Duration::from_millis(100)).await;
/// tx.send("world")?;
/// # Ok(())
/// # }
/// ```
///
/// [`Sender`]: struct@Sender
/// [`Receiver`]: struct@Recei... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | 9c6ff3b7e5e86dd2e0104a8ac0a034a3695cfe84 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/9c6ff3b7e5e86dd2e0104a8ac0a034a3695cfe84/tokio/src/sync/watch.rs | 481 | 540 |
tokio-rs/tokio:tokio/src/sync/watch.rs:17 | ///
/// ```
/// use tokio::sync::watch;
///
/// #[tokio::main]
/// async fn main() {
/// let (tx, mut rx) = watch::channel("hello");
///
/// tx.send("goodbye").unwrap();
///
/// assert!(rx.has_changed().unwrap());
/// assert_eq!(*rx.borrow_and_update(), "goodb... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | 9c6ff3b7e5e86dd2e0104a8ac0a034a3695cfe84 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/9c6ff3b7e5e86dd2e0104a8ac0a034a3695cfe84/tokio/src/sync/watch.rs | 641 | 700 |
tokio-rs/tokio:tokio/src/sync/watch.rs:20 | /// when it returned `true` will be considered seen.
///
/// If the channel is closed, then `wait_for` will return a `RecvError`.
/// Once this happens, no more messages can ever be sent on the channel.
/// When an error is returned, it is guaranteed that the closure has been
/// called on the last ... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | 9c6ff3b7e5e86dd2e0104a8ac0a034a3695cfe84 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/9c6ff3b7e5e86dd2e0104a8ac0a034a3695cfe84/tokio/src/sync/watch.rs | 761 | 820 |
tokio-rs/tokio:tokio/src/sync/watch.rs:21 | /// }
/// ```
pub async fn wait_for(
&mut self,
mut f: impl FnMut(&T) -> bool,
) -> Result<Ref<'_, T>, error::RecvError> {
let mut closed = false;
loop {
{
let inner = self.shared.value.read().unwrap();
let new_version = self.share... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | 9c6ff3b7e5e86dd2e0104a8ac0a034a3695cfe84 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/9c6ff3b7e5e86dd2e0104a8ac0a034a3695cfe84/tokio/src/sync/watch.rs | 801 | 860 |
tokio-rs/tokio:tokio/src/sync/watch.rs:22 | closed = changed_impl(&self.shared, &mut self.version).await.is_err();
}
}
/// Returns `true` if receivers belong to the same channel.
///
/// # Examples
///
/// ```
/// let (tx, rx) = tokio::sync::watch::channel(true);
/// let rx2 = rx.clone();
/// assert!(rx.same_channel(&... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | 9c6ff3b7e5e86dd2e0104a8ac0a034a3695cfe84 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/9c6ff3b7e5e86dd2e0104a8ac0a034a3695cfe84/tokio/src/sync/watch.rs | 841 | 900 |
tokio-rs/tokio:tokio/src/sync/watch.rs:23 | if state.is_closed() {
// The sender has been dropped.
return Some(Err(error::RecvError(())));
}
None
}
async fn changed_impl<T>(
shared: &Shared<T>,
version: &mut Version,
) -> Result<(), error::RecvError> {
crate::trace::async_trace_leaf().await;
loop {
// In order t... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | 9c6ff3b7e5e86dd2e0104a8ac0a034a3695cfe84 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/9c6ff3b7e5e86dd2e0104a8ac0a034a3695cfe84/tokio/src/sync/watch.rs | 881 | 940 |
tokio-rs/tokio:tokio/src/sync/watch.rs:24 | fn drop(&mut self) {
// No synchronization necessary as this is only used as a counter and
// not memory access.
if 1 == self.shared.ref_count_rx.fetch_sub(1, Relaxed) {
// This is the last `Receiver` handle, tasks waiting on `Sender::closed()`
self.shared.notify_tx.notif... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | 9c6ff3b7e5e86dd2e0104a8ac0a034a3695cfe84 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/9c6ff3b7e5e86dd2e0104a8ac0a034a3695cfe84/tokio/src/sync/watch.rs | 921 | 980 |
tokio-rs/tokio:tokio/src/sync/watch.rs:25 | /// To always make a new value available for future receivers, even if no
/// receiver currently exists, one of the other send methods
/// ([`send_if_modified`], [`send_modify`], or [`send_replace`]) can be
/// used instead.
///
/// [`subscribe`]: Sender::subscribe
/// [`SendError`]: error::Send... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | 9c6ff3b7e5e86dd2e0104a8ac0a034a3695cfe84 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/9c6ff3b7e5e86dd2e0104a8ac0a034a3695cfe84/tokio/src/sync/watch.rs | 961 | 1,020 |
tokio-rs/tokio:tokio/src/sync/watch.rs:27 | ///
/// # Panics
///
/// This function panics when the invocation of the `modify` closure panics.
/// No receivers are notified when panicking. All changes of the watched
/// value applied by the closure before panicking will be visible in
/// subsequent calls to `borrow`.
///
/// # Exam... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | 9c6ff3b7e5e86dd2e0104a8ac0a034a3695cfe84 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/9c6ff3b7e5e86dd2e0104a8ac0a034a3695cfe84/tokio/src/sync/watch.rs | 1,041 | 1,100 |
tokio-rs/tokio:tokio/src/sync/watch.rs:28 | {
{
// Acquire the write lock and update the value.
let mut lock = self.shared.value.write().unwrap();
// Update the value and catch possible panic inside func.
let result = panic::catch_unwind(panic::AssertUnwindSafe(|| modify(&mut lock)));
match res... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | 9c6ff3b7e5e86dd2e0104a8ac0a034a3695cfe84 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/9c6ff3b7e5e86dd2e0104a8ac0a034a3695cfe84/tokio/src/sync/watch.rs | 1,081 | 1,140 |
tokio-rs/tokio:tokio/src/sync/watch.rs:33 | /// # if false {
/// tokio::time::sleep(Duration::from_millis(10)).await;
/// # }
/// tx.send(100).unwrap();
/// });
///
/// rx.changed().await.unwrap();
/// assert_eq!(100, *rx.borrow());
/// }
/// ```
pub fn subscribe(&self) -> Receiv... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | 9c6ff3b7e5e86dd2e0104a8ac0a034a3695cfe84 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/9c6ff3b7e5e86dd2e0104a8ac0a034a3695cfe84/tokio/src/sync/watch.rs | 1,281 | 1,340 |
tokio-rs/tokio:tokio/src/sync/watch.rs:34 | }
impl<T> Drop for Sender<T> {
fn drop(&mut self) {
if self.shared.ref_count_tx.fetch_sub(1, AcqRel) == 1 {
self.shared.state.set_closed();
self.shared.notify_rx.notify_waiters();
}
}
}
// ===== impl Ref =====
impl<T> ops::Deref for Ref<'_, T> {
type Target = T;
... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | 9c6ff3b7e5e86dd2e0104a8ac0a034a3695cfe84 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/9c6ff3b7e5e86dd2e0104a8ac0a034a3695cfe84/tokio/src/sync/watch.rs | 1,321 | 1,380 |
tokio-rs/tokio:tokio/src/sync/watch.rs:35 | let send = send_thread.join().unwrap();
let recv_thread = thread::spawn(move || {
recv.changed().now_or_never();
recv.changed().now_or_never();
recv
});
send.send(3).unwrap();
let mut recv = recv_thread.join().unwrap();
... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | 9c6ff3b7e5e86dd2e0104a8ac0a034a3695cfe84 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/9c6ff3b7e5e86dd2e0104a8ac0a034a3695cfe84/tokio/src/sync/watch.rs | 1,361 | 1,420 |
tokio-rs/tokio:tokio/src/sync/watch.rs:36 | let recv_thread = thread::spawn(move || {
recv.changed().now_or_never();
recv.changed().now_or_never();
recv
});
send.send(3).unwrap();
let recv = recv_thread.join().unwrap();
assert!(recv.borrow().eq(&3));
ass... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | 9c6ff3b7e5e86dd2e0104a8ac0a034a3695cfe84 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/9c6ff3b7e5e86dd2e0104a8ac0a034a3695cfe84/tokio/src/sync/watch.rs | 1,401 | 1,421 |
tokio-rs/tokio:tokio/src/sync/watch.rs:33 | /// rx.changed().await.unwrap();
/// assert_eq!(100, *rx.borrow());
/// }
/// ```
pub fn subscribe(&self) -> Receiver<T> {
let shared = self.shared.clone();
let version = shared.state.load().version();
// The CLOSED bit in the state tracks only whether the sender is
... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | 3141ed62287043b5400e24ac930b002dc7d11d92 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/3141ed62287043b5400e24ac930b002dc7d11d92/tokio/src/sync/watch.rs | 1,281 | 1,340 |
tokio-rs/tokio:tokio/src/sync/watch.rs:34 | self.shared.notify_rx.notify_waiters();
}
}
}
// ===== impl Ref =====
impl<T> ops::Deref for Ref<'_, T> {
type Target = T;
fn deref(&self) -> &T {
self.inner.deref()
}
}
#[cfg(all(test, loom))]
mod tests {
use futures::future::FutureExt;
use loom::thread;
// test for htt... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | 3141ed62287043b5400e24ac930b002dc7d11d92 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/3141ed62287043b5400e24ac930b002dc7d11d92/tokio/src/sync/watch.rs | 1,321 | 1,380 |
tokio-rs/tokio:tokio/src/sync/watch.rs:35 | });
send.send(3).unwrap();
let mut recv = recv_thread.join().unwrap();
let send_thread = thread::spawn(move || {
send.send(2).unwrap();
});
recv.changed().now_or_never();
send_thread.join().unwrap();
});
}
#[tes... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | 3141ed62287043b5400e24ac930b002dc7d11d92 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/3141ed62287043b5400e24ac930b002dc7d11d92/tokio/src/sync/watch.rs | 1,361 | 1,415 |
tokio-rs/tokio:tokio/src/sync/watch.rs:36 | send.send(3).unwrap();
let recv = recv_thread.join().unwrap();
assert!(recv.borrow().eq(&3));
assert!(send.borrow().eq(&3));
send.send(2).unwrap();
thread::spawn(move || {
assert!(recv.borrow().eq(&2));
});
assert!(se... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | 3141ed62287043b5400e24ac930b002dc7d11d92 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/3141ed62287043b5400e24ac930b002dc7d11d92/tokio/src/sync/watch.rs | 1,401 | 1,415 |
tokio-rs/tokio:tokio/src/sync/watch.rs:3 | //! sleep(Duration::from_millis(100)).await;
//! tx.send("world")?;
//! # Ok(())
//! # }
//! ```
//!
//! # Closing
//!
//! [`Sender::is_closed`] and [`Sender::closed`] allow the producer to detect
//! when all [`Receiver`] handles have been dropped. This indicates that there
//! is no further interest in the values bei... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | 02b779e315c5c5f0dbbc8b56fc711cc8e665ee1e | github | async-runtime | https://github.com/tokio-rs/tokio/blob/02b779e315c5c5f0dbbc8b56fc711cc8e665ee1e/tokio/src/sync/watch.rs | 81 | 140 |
tokio-rs/tokio:tokio/src/sync/watch.rs:4 | use std::ops;
use std::panic;
/// Receives values from the associated [`Sender`](struct@Sender).
///
/// Instances are created by the [`channel`](fn@channel) function.
///
/// To turn this receiver into a `Stream`, you can use the [`WatchStream`]
/// wrapper.
///
/// [`WatchStream`]: https://docs.rs/tokio-stream/0.1/t... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | 02b779e315c5c5f0dbbc8b56fc711cc8e665ee1e | github | async-runtime | https://github.com/tokio-rs/tokio/blob/02b779e315c5c5f0dbbc8b56fc711cc8e665ee1e/tokio/src/sync/watch.rs | 121 | 180 |
tokio-rs/tokio:tokio/src/sync/watch.rs:6 | /// // Drop the sender immediately, just for testing purposes.
/// drop(tx);
///
/// // Even if the sender has already been dropped...
/// assert!(rx.has_changed().is_err());
/// // ...the modified value is still readable and detected as changed.
/// assert_eq!(*rx.borrow... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | 02b779e315c5c5f0dbbc8b56fc711cc8e665ee1e | github | async-runtime | https://github.com/tokio-rs/tokio/blob/02b779e315c5c5f0dbbc8b56fc711cc8e665ee1e/tokio/src/sync/watch.rs | 201 | 260 |
tokio-rs/tokio:tokio/src/sync/watch.rs:7 | /// Notifies waiting receivers that the value changed.
notify_rx: big_notify::BigNotify,
/// Notifies any task listening for `Receiver` dropped events.
notify_tx: Notify,
}
impl<T: fmt::Debug> fmt::Debug for Shared<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let state = sel... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | 02b779e315c5c5f0dbbc8b56fc711cc8e665ee1e | github | async-runtime | https://github.com/tokio-rs/tokio/blob/02b779e315c5c5f0dbbc8b56fc711cc8e665ee1e/tokio/src/sync/watch.rs | 241 | 300 |
tokio-rs/tokio:tokio/src/sync/watch.rs:8 | }
}
impl<T> Error for SendError<T> {}
/// Error produced when receiving a change notification.
#[derive(Debug, Clone)]
pub struct RecvError(pub(super) ());
// ===== impl RecvError =====
impl fmt::Display for RecvError {
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | 02b779e315c5c5f0dbbc8b56fc711cc8e665ee1e | github | async-runtime | https://github.com/tokio-rs/tokio/blob/02b779e315c5c5f0dbbc8b56fc711cc8e665ee1e/tokio/src/sync/watch.rs | 281 | 340 |
tokio-rs/tokio:tokio/src/sync/watch.rs:9 | impl BigNotify {
pub(super) fn new() -> Self {
Self {
#[cfg(not(all(
not(loom),
feature = "sync",
any(feature = "rt", feature = "macros")
)))]
next: std::sync::atomic::AtomicUsize::new(0),
... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | 02b779e315c5c5f0dbbc8b56fc711cc8e665ee1e | github | async-runtime | https://github.com/tokio-rs/tokio/blob/02b779e315c5c5f0dbbc8b56fc711cc8e665ee1e/tokio/src/sync/watch.rs | 321 | 380 |
tokio-rs/tokio:tokio/src/sync/watch.rs:10 | const CLOSED_BIT: usize = 1;
// Using 2 as the step size preserves the `CLOSED_BIT`.
const STEP_SIZE: usize = 2;
/// The version part of the state. The lowest bit is always zero.
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub(super) struct Version(usize);
/// Snapshot of the state. The firs... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | 02b779e315c5c5f0dbbc8b56fc711cc8e665ee1e | github | async-runtime | https://github.com/tokio-rs/tokio/blob/02b779e315c5c5f0dbbc8b56fc711cc8e665ee1e/tokio/src/sync/watch.rs | 361 | 420 |
tokio-rs/tokio:tokio/src/sync/watch.rs:11 | /// Extract the version from the state.
pub(super) fn version(self) -> Version {
Version(self.0 & !CLOSED_BIT)
}
/// Is the closed bit set?
pub(super) fn is_closed(self) -> bool {
(self.0 & CLOSED_BIT) == CLOSED_BIT
}
}
impl AtomicState {
... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | 02b779e315c5c5f0dbbc8b56fc711cc8e665ee1e | github | async-runtime | https://github.com/tokio-rs/tokio/blob/02b779e315c5c5f0dbbc8b56fc711cc8e665ee1e/tokio/src/sync/watch.rs | 401 | 460 |
tokio-rs/tokio:tokio/src/sync/watch.rs:12 | pub(super) fn set_closed(&self) {
self.0.fetch_or(CLOSED_BIT, Ordering::Release);
}
}
}
/// Creates a new watch channel, returning the "send" and "receive" handles.
///
/// All values sent by [`Sender`] will become visible to the [`Receiver`] handles.
/// Only the last value sent is made availa... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | 02b779e315c5c5f0dbbc8b56fc711cc8e665ee1e | github | async-runtime | https://github.com/tokio-rs/tokio/blob/02b779e315c5c5f0dbbc8b56fc711cc8e665ee1e/tokio/src/sync/watch.rs | 441 | 500 |
tokio-rs/tokio:tokio/src/sync/watch.rs:13 | /// [`Sender`]: struct@Sender
/// [`Receiver`]: struct@Receiver
pub fn channel<T>(init: T) -> (Sender<T>, Receiver<T>) {
let shared = Arc::new(Shared {
value: RwLock::new(init),
state: AtomicState::new(),
ref_count_rx: AtomicUsize::new(1),
notify_rx: big_notify::BigNotify::new(),
... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | 02b779e315c5c5f0dbbc8b56fc711cc8e665ee1e | github | async-runtime | https://github.com/tokio-rs/tokio/blob/02b779e315c5c5f0dbbc8b56fc711cc8e665ee1e/tokio/src/sync/watch.rs | 481 | 540 |
tokio-rs/tokio:tokio/src/sync/watch.rs:16 | ///
/// [`changed`]: Receiver::changed
/// [`borrow`]: Receiver::borrow
pub fn borrow_and_update(&mut self) -> Ref<'_, T> {
let inner = self.shared.value.read().unwrap();
// After obtaining a read-lock no concurrent writes could occur
// and the loaded version matches that of the bo... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | 02b779e315c5c5f0dbbc8b56fc711cc8e665ee1e | github | async-runtime | https://github.com/tokio-rs/tokio/blob/02b779e315c5c5f0dbbc8b56fc711cc8e665ee1e/tokio/src/sync/watch.rs | 601 | 660 |
tokio-rs/tokio:tokio/src/sync/watch.rs:17 | /// assert!(!rx.has_changed().unwrap());
///
/// drop(tx);
/// // The `tx` handle has been dropped
/// assert!(rx.has_changed().is_err());
/// }
/// ```
pub fn has_changed(&self) -> Result<bool, error::RecvError> {
// Load the version from the state
let state ... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | 02b779e315c5c5f0dbbc8b56fc711cc8e665ee1e | github | async-runtime | https://github.com/tokio-rs/tokio/blob/02b779e315c5c5f0dbbc8b56fc711cc8e665ee1e/tokio/src/sync/watch.rs | 641 | 700 |
tokio-rs/tokio:tokio/src/sync/watch.rs:20 | ///
/// [`Receiver::changed()`]: crate::sync::watch::Receiver::changed
///
/// # Examples
///
/// ```
/// use tokio::sync::watch;
///
/// #[tokio::main]
///
/// async fn main() {
/// let (tx, _rx) = watch::channel("hello");
///
/// tx.send("goodbye").unwrap();... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | 02b779e315c5c5f0dbbc8b56fc711cc8e665ee1e | github | async-runtime | https://github.com/tokio-rs/tokio/blob/02b779e315c5c5f0dbbc8b56fc711cc8e665ee1e/tokio/src/sync/watch.rs | 761 | 820 |
tokio-rs/tokio:tokio/src/sync/watch.rs:21 | if !closed || has_changed {
let result = panic::catch_unwind(panic::AssertUnwindSafe(|| f(&inner)));
match result {
Ok(true) => {
return Ok(Ref { inner, has_changed });
}
Ok(false)... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | 02b779e315c5c5f0dbbc8b56fc711cc8e665ee1e | github | async-runtime | https://github.com/tokio-rs/tokio/blob/02b779e315c5c5f0dbbc8b56fc711cc8e665ee1e/tokio/src/sync/watch.rs | 801 | 860 |
tokio-rs/tokio:tokio/src/sync/watch.rs:22 | /// assert!(!rx3.same_channel(&rx2));
/// ```
pub fn same_channel(&self, other: &Self) -> bool {
Arc::ptr_eq(&self.shared, &other.shared)
}
cfg_process_driver! {
pub(crate) fn try_has_changed(&mut self) -> Option<Result<(), error::RecvError>> {
maybe_changed(&self.shared, &m... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | 02b779e315c5c5f0dbbc8b56fc711cc8e665ee1e | github | async-runtime | https://github.com/tokio-rs/tokio/blob/02b779e315c5c5f0dbbc8b56fc711cc8e665ee1e/tokio/src/sync/watch.rs | 841 | 900 |
tokio-rs/tokio:tokio/src/sync/watch.rs:23 | loop {
// In order to avoid a race condition, we first request a notification,
// **then** check the current value's version. If a new version exists,
// the notification request is dropped.
let notified = shared.notify_rx.notified();
if let Some(ret) = maybe_changed(shared, ver... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | 02b779e315c5c5f0dbbc8b56fc711cc8e665ee1e | github | async-runtime | https://github.com/tokio-rs/tokio/blob/02b779e315c5c5f0dbbc8b56fc711cc8e665ee1e/tokio/src/sync/watch.rs | 881 | 940 |
tokio-rs/tokio:tokio/src/sync/watch.rs:27 | /// counter: usize,
/// }
/// let (state_tx, mut state_rx) = watch::channel(State { counter: 1 });
/// let inc_counter_if_odd = |state: &mut State| {
/// if state.counter % 2 == 1 {
/// state.counter += 1;
/// return true;
/// }
/// false
/// };
//... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | 02b779e315c5c5f0dbbc8b56fc711cc8e665ee1e | github | async-runtime | https://github.com/tokio-rs/tokio/blob/02b779e315c5c5f0dbbc8b56fc711cc8e665ee1e/tokio/src/sync/watch.rs | 1,041 | 1,100 |
tokio-rs/tokio:tokio/src/sync/watch.rs:28 | }
Err(panicked) => {
// Drop the lock to avoid poisoning it.
drop(lock);
// Forward the panic to the caller.
panic::resume_unwind(panicked);
// Unreachable
}
};
se... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | 02b779e315c5c5f0dbbc8b56fc711cc8e665ee1e | github | async-runtime | https://github.com/tokio-rs/tokio/blob/02b779e315c5c5f0dbbc8b56fc711cc8e665ee1e/tokio/src/sync/watch.rs | 1,081 | 1,140 |
tokio-rs/tokio:tokio/src/sync/watch.rs:30 | /// let (tx, rx) = tokio::sync::watch::channel(());
/// assert!(!tx.is_closed());
///
/// drop(rx);
/// assert!(tx.is_closed());
/// ```
pub fn is_closed(&self) -> bool {
self.receiver_count() == 0
}
/// Completes when all receivers have dropped.
///
/// This allows the ... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | 02b779e315c5c5f0dbbc8b56fc711cc8e665ee1e | github | async-runtime | https://github.com/tokio-rs/tokio/blob/02b779e315c5c5f0dbbc8b56fc711cc8e665ee1e/tokio/src/sync/watch.rs | 1,161 | 1,220 |
tokio-rs/tokio:tokio/src/sync/watch.rs:33 | ///
/// # Examples
///
/// ```
/// use tokio::sync::watch;
///
/// #[tokio::main]
/// async fn main() {
/// let (tx, rx1) = watch::channel("hello");
///
/// assert_eq!(1, tx.receiver_count());
///
/// let mut _rx2 = rx1.clone();
///
/// assert_eq!(... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | 02b779e315c5c5f0dbbc8b56fc711cc8e665ee1e | github | async-runtime | https://github.com/tokio-rs/tokio/blob/02b779e315c5c5f0dbbc8b56fc711cc8e665ee1e/tokio/src/sync/watch.rs | 1,281 | 1,340 |
tokio-rs/tokio:tokio/src/sync/watch.rs:34 | mod tests {
use futures::future::FutureExt;
use loom::thread;
// test for https://github.com/tokio-rs/tokio/issues/3168
#[test]
fn watch_spurious_wakeup() {
loom::model(|| {
let (send, mut recv) = crate::sync::watch::channel(0i32);
send.send(1).unwrap();
... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | 02b779e315c5c5f0dbbc8b56fc711cc8e665ee1e | github | async-runtime | https://github.com/tokio-rs/tokio/blob/02b779e315c5c5f0dbbc8b56fc711cc8e665ee1e/tokio/src/sync/watch.rs | 1,321 | 1,380 |
tokio-rs/tokio:tokio/src/sync/watch.rs:35 | fn watch_borrow() {
loom::model(|| {
let (send, mut recv) = crate::sync::watch::channel(0i32);
assert!(send.borrow().eq(&0));
assert!(recv.borrow().eq(&0));
send.send(1).unwrap();
assert!(send.borrow().eq(&1));
let send_thread = thread::... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | 02b779e315c5c5f0dbbc8b56fc711cc8e665ee1e | github | async-runtime | https://github.com/tokio-rs/tokio/blob/02b779e315c5c5f0dbbc8b56fc711cc8e665ee1e/tokio/src/sync/watch.rs | 1,361 | 1,399 |
tokio-rs/tokio:tokio/src/sync/watch.rs:20 | /// let (tx, _rx) = watch::channel("hello");
///
/// tx.send("goodbye").unwrap();
///
/// // here we subscribe to a second receiver
/// // now in case of using `changed` we would have
/// // to first check the current value and then wait
/// // for changes or else `ch... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | 6871084629ad95c37c7136d865890ab2e371ea12 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/6871084629ad95c37c7136d865890ab2e371ea12/tokio/src/sync/watch.rs | 761 | 820 |
tokio-rs/tokio:tokio/src/sync/watch.rs:21 | // Drop the read-lock to avoid poisoning it.
drop(inner);
// Forward the panic to the caller.
panic::resume_unwind(panicked);
// Unreachable
}
};
}
... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | 6871084629ad95c37c7136d865890ab2e371ea12 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/6871084629ad95c37c7136d865890ab2e371ea12/tokio/src/sync/watch.rs | 801 | 860 |
tokio-rs/tokio:tokio/src/sync/watch.rs:22 | }
fn maybe_changed<T>(
shared: &Shared<T>,
version: &mut Version,
) -> Option<Result<(), error::RecvError>> {
// Load the version from the state
let state = shared.state.load();
let new_version = state.version();
if *version != new_version {
// Observe the new version and return
... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | 6871084629ad95c37c7136d865890ab2e371ea12 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/6871084629ad95c37c7136d865890ab2e371ea12/tokio/src/sync/watch.rs | 841 | 900 |
tokio-rs/tokio:tokio/src/sync/watch.rs:23 | notified.await;
// loop around again in case the wake-up was spurious
}
}
impl<T> Clone for Receiver<T> {
fn clone(&self) -> Self {
let version = self.version;
let shared = self.shared.clone();
Self::from_shared(version, shared)
}
}
impl<T> Drop for Receiver<T> {
fn dr... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | 6871084629ad95c37c7136d865890ab2e371ea12 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/6871084629ad95c37c7136d865890ab2e371ea12/tokio/src/sync/watch.rs | 881 | 940 |
tokio-rs/tokio:tokio/src/sync/watch.rs:27 | /// assert_eq!(state_rx.borrow().counter, 1);
///
/// assert!(!state_rx.has_changed().unwrap());
/// assert!(state_tx.send_if_modified(inc_counter_if_odd));
/// assert!(state_rx.has_changed().unwrap());
/// assert_eq!(state_rx.borrow_and_update().counter, 2);
///
/// assert!(!state_rx.has_ch... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | 6871084629ad95c37c7136d865890ab2e371ea12 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/6871084629ad95c37c7136d865890ab2e371ea12/tokio/src/sync/watch.rs | 1,041 | 1,100 |
tokio-rs/tokio:tokio/src/sync/watch.rs:32 | /// #[tokio::main]
/// async fn main() {
/// let (tx, _rx) = watch::channel(0u64);
/// tx.send(5).unwrap();
/// let mut rx = tx.subscribe();
///
/// tokio::spawn(async move {
/// // by spawning and sleeping, the message is sent after `main`
/// // hits the... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | 6871084629ad95c37c7136d865890ab2e371ea12 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/6871084629ad95c37c7136d865890ab2e371ea12/tokio/src/sync/watch.rs | 1,241 | 1,300 |
tokio-rs/tokio:tokio/src/sync/watch.rs:33 | ///
/// let mut _rx2 = rx1.clone();
///
/// assert_eq!(2, tx.receiver_count());
/// }
/// ```
pub fn receiver_count(&self) -> usize {
self.shared.ref_count_rx.load(Relaxed)
}
}
impl<T> Drop for Sender<T> {
fn drop(&mut self) {
self.shared.state.set_closed();
... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | 6871084629ad95c37c7136d865890ab2e371ea12 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/6871084629ad95c37c7136d865890ab2e371ea12/tokio/src/sync/watch.rs | 1,281 | 1,340 |
tokio-rs/tokio:tokio/src/sync/watch.rs:35 | send.send(2).unwrap();
send
});
recv.changed().now_or_never();
let send = send_thread.join().unwrap();
let recv_thread = thread::spawn(move || {
recv.changed().now_or_never();
recv.changed().now_or_never();
... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | 6871084629ad95c37c7136d865890ab2e371ea12 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/6871084629ad95c37c7136d865890ab2e371ea12/tokio/src/sync/watch.rs | 1,361 | 1,388 |
tokio-rs/tokio:tokio/src/sync/watch.rs:8 | }
}
impl<T> Error for SendError<T> {}
/// Error produced when receiving a change notification.
#[derive(Debug, Clone)]
pub struct RecvError(pub(super) ());
// ===== impl RecvError =====
impl fmt::Display for RecvError {
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | 453c72070961d155a07fd54b0db1439ecd33f4ef | github | async-runtime | https://github.com/tokio-rs/tokio/blob/453c72070961d155a07fd54b0db1439ecd33f4ef/tokio/src/sync/watch.rs | 281 | 340 |
tokio-rs/tokio:tokio/src/sync/watch.rs:9 | impl BigNotify {
pub(super) fn new() -> Self {
Self {
#[cfg(not(all(
not(loom),
feature = "sync",
any(feature = "rt", feature = "macros")
)))]
next: AtomicUsize::new(0),
inner:... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | 453c72070961d155a07fd54b0db1439ecd33f4ef | github | async-runtime | https://github.com/tokio-rs/tokio/blob/453c72070961d155a07fd54b0db1439ecd33f4ef/tokio/src/sync/watch.rs | 321 | 380 |
tokio-rs/tokio:tokio/src/sync/watch.rs:9 | impl BigNotify {
pub(super) fn new() -> Self {
Self {
#[cfg(not(all(
not(loom),
feature = "sync",
any(feature = "rt", feature = "macros")
)))]
next: AtomicUsize::new(0),
inner:... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | e76c06ba3853548096533fcb569374414e07a86b | github | async-runtime | https://github.com/tokio-rs/tokio/blob/e76c06ba3853548096533fcb569374414e07a86b/tokio/src/sync/watch.rs | 321 | 380 |
tokio-rs/tokio:tokio/src/sync/watch.rs:10 | const CLOSED_BIT: usize = 1;
// Using 2 as the step size preserves the `CLOSED_BIT`.
const STEP_SIZE: usize = 2;
/// The version part of the state. The lowest bit is always zero.
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub(super) struct Version(usize);
/// Snapshot of the state. The firs... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | e76c06ba3853548096533fcb569374414e07a86b | github | async-runtime | https://github.com/tokio-rs/tokio/blob/e76c06ba3853548096533fcb569374414e07a86b/tokio/src/sync/watch.rs | 361 | 420 |
tokio-rs/tokio:tokio/src/sync/watch.rs:11 | /// Is the closed bit set?
pub(super) fn is_closed(self) -> bool {
(self.0 & CLOSED_BIT) == CLOSED_BIT
}
}
impl AtomicState {
/// Create a new `AtomicState` that is not closed and which has the
/// version set to `Version::initial()`.
pub(super) fn new() -> S... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | e76c06ba3853548096533fcb569374414e07a86b | github | async-runtime | https://github.com/tokio-rs/tokio/blob/e76c06ba3853548096533fcb569374414e07a86b/tokio/src/sync/watch.rs | 401 | 460 |
tokio-rs/tokio:tokio/src/sync/watch.rs:12 | /// ```
/// use tokio::sync::watch;
/// use tokio::time::{Duration, sleep};
///
/// # async fn dox() -> Result<(), Box<dyn std::error::Error>> {
/// let (tx, mut rx) = watch::channel("hello");
///
/// tokio::spawn(async move {
/// // Use the equivalent of a "do-while" loop so the initial value is
/// // process... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | e76c06ba3853548096533fcb569374414e07a86b | github | async-runtime | https://github.com/tokio-rs/tokio/blob/e76c06ba3853548096533fcb569374414e07a86b/tokio/src/sync/watch.rs | 441 | 500 |
tokio-rs/tokio:tokio/src/sync/watch.rs:19 | ///
/// If the channel is closed, then `wait_for` will return a `RecvError`.
/// Once this happens, no more messages can ever be sent on the channel.
/// When an error is returned, it is guaranteed that the closure has been
/// called on the last value, and that it returned `false` for that value.
/... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | e76c06ba3853548096533fcb569374414e07a86b | github | async-runtime | https://github.com/tokio-rs/tokio/blob/e76c06ba3853548096533fcb569374414e07a86b/tokio/src/sync/watch.rs | 721 | 780 |
tokio-rs/tokio:tokio/src/sync/watch.rs:20 | /// ```
pub async fn wait_for(
&mut self,
mut f: impl FnMut(&T) -> bool,
) -> Result<Ref<'_, T>, error::RecvError> {
let mut closed = false;
loop {
{
let inner = self.shared.value.read().unwrap();
let new_version = self.shared.state.lo... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | e76c06ba3853548096533fcb569374414e07a86b | github | async-runtime | https://github.com/tokio-rs/tokio/blob/e76c06ba3853548096533fcb569374414e07a86b/tokio/src/sync/watch.rs | 761 | 820 |
tokio-rs/tokio:tokio/src/sync/watch.rs:21 | }
}
/// Returns `true` if receivers belong to the same channel.
///
/// # Examples
///
/// ```
/// let (tx, rx) = tokio::sync::watch::channel(true);
/// let rx2 = rx.clone();
/// assert!(rx.same_channel(&rx2));
///
/// let (tx3, rx3) = tokio::sync::watch::channel(true);
... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | e76c06ba3853548096533fcb569374414e07a86b | github | async-runtime | https://github.com/tokio-rs/tokio/blob/e76c06ba3853548096533fcb569374414e07a86b/tokio/src/sync/watch.rs | 801 | 860 |
tokio-rs/tokio:tokio/src/sync/watch.rs:22 | if state.is_closed() {
// The sender has been dropped.
return Some(Err(error::RecvError(())));
}
None
}
async fn changed_impl<T>(
shared: &Shared<T>,
version: &mut Version,
) -> Result<(), error::RecvError> {
crate::trace::async_trace_leaf().await;
loop {
// In order t... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | e76c06ba3853548096533fcb569374414e07a86b | github | async-runtime | https://github.com/tokio-rs/tokio/blob/e76c06ba3853548096533fcb569374414e07a86b/tokio/src/sync/watch.rs | 841 | 900 |
tokio-rs/tokio:tokio/src/sync/watch.rs:23 | // No synchronization necessary as this is only used as a counter and
// not memory access.
if 1 == self.shared.ref_count_rx.fetch_sub(1, Relaxed) {
// This is the last `Receiver` handle, tasks waiting on `Sender::closed()`
self.shared.notify_tx.notify_waiters();
}
}
... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | e76c06ba3853548096533fcb569374414e07a86b | github | async-runtime | https://github.com/tokio-rs/tokio/blob/e76c06ba3853548096533fcb569374414e07a86b/tokio/src/sync/watch.rs | 881 | 940 |
tokio-rs/tokio:tokio/src/sync/watch.rs:24 | /// receiver currently exists, one of the other send methods
/// ([`send_if_modified`], [`send_modify`], or [`send_replace`]) can be
/// used instead.
///
/// [`subscribe`]: Sender::subscribe
/// [`SendError`]: error::SendError
/// [`send_if_modified`]: Sender::send_if_modified
/// [`send_mo... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | e76c06ba3853548096533fcb569374414e07a86b | github | async-runtime | https://github.com/tokio-rs/tokio/blob/e76c06ba3853548096533fcb569374414e07a86b/tokio/src/sync/watch.rs | 921 | 980 |
tokio-rs/tokio:tokio/src/sync/watch.rs:26 | /// # Panics
///
/// This function panics when the invocation of the `modify` closure panics.
/// No receivers are notified when panicking. All changes of the watched
/// value applied by the closure before panicking will be visible in
/// subsequent calls to `borrow`.
///
/// # Examples
... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | e76c06ba3853548096533fcb569374414e07a86b | github | async-runtime | https://github.com/tokio-rs/tokio/blob/e76c06ba3853548096533fcb569374414e07a86b/tokio/src/sync/watch.rs | 1,001 | 1,060 |
tokio-rs/tokio:tokio/src/sync/watch.rs:27 | {
// Acquire the write lock and update the value.
let mut lock = self.shared.value.write().unwrap();
// Update the value and catch possible panic inside func.
let result = panic::catch_unwind(panic::AssertUnwindSafe(|| modify(&mut lock)));
match result {
... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | e76c06ba3853548096533fcb569374414e07a86b | github | async-runtime | https://github.com/tokio-rs/tokio/blob/e76c06ba3853548096533fcb569374414e07a86b/tokio/src/sync/watch.rs | 1,041 | 1,100 |
tokio-rs/tokio:tokio/src/sync/watch.rs:32 | /// assert_eq!(100, *rx.borrow());
/// }
/// ```
pub fn subscribe(&self) -> Receiver<T> {
let shared = self.shared.clone();
let version = shared.state.load().version();
// The CLOSED bit in the state tracks only whether the sender is
// dropped, so we do not need to unse... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | e76c06ba3853548096533fcb569374414e07a86b | github | async-runtime | https://github.com/tokio-rs/tokio/blob/e76c06ba3853548096533fcb569374414e07a86b/tokio/src/sync/watch.rs | 1,241 | 1,300 |
tokio-rs/tokio:tokio/src/sync/watch.rs:33 | }
// ===== impl Ref =====
impl<T> ops::Deref for Ref<'_, T> {
type Target = T;
fn deref(&self) -> &T {
self.inner.deref()
}
}
#[cfg(all(test, loom))]
mod tests {
use futures::future::FutureExt;
use loom::thread;
// test for https://github.com/tokio-rs/tokio/issues/3168
#[test]
... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | e76c06ba3853548096533fcb569374414e07a86b | github | async-runtime | https://github.com/tokio-rs/tokio/blob/e76c06ba3853548096533fcb569374414e07a86b/tokio/src/sync/watch.rs | 1,281 | 1,340 |
tokio-rs/tokio:tokio/src/sync/watch.rs:34 | let mut recv = recv_thread.join().unwrap();
let send_thread = thread::spawn(move || {
send.send(2).unwrap();
});
recv.changed().now_or_never();
send_thread.join().unwrap();
});
}
#[test]
fn watch_borrow() {
loom::model(|| {
... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | e76c06ba3853548096533fcb569374414e07a86b | github | async-runtime | https://github.com/tokio-rs/tokio/blob/e76c06ba3853548096533fcb569374414e07a86b/tokio/src/sync/watch.rs | 1,321 | 1,372 |
tokio-rs/tokio:tokio/src/sync/watch.rs:35 | assert!(recv.borrow().eq(&3));
assert!(send.borrow().eq(&3));
send.send(2).unwrap();
thread::spawn(move || {
assert!(recv.borrow().eq(&2));
});
assert!(send.borrow().eq(&2));
});
}
} | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | e76c06ba3853548096533fcb569374414e07a86b | github | async-runtime | https://github.com/tokio-rs/tokio/blob/e76c06ba3853548096533fcb569374414e07a86b/tokio/src/sync/watch.rs | 1,361 | 1,372 |
tokio-rs/tokio:tokio/src/sync/watch.rs:19 | ///
/// If the channel is closed, then `wait_for` will return a `RecvError`.
/// Once this happens, no more messages can ever be sent on the channel.
/// When an error is returned, it is guaranteed that the closure has been
/// called on the last value, and that it returned `false` for that value.
/... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | 9bc782acfcebf198eda21be63d5b315bf40b565b | github | async-runtime | https://github.com/tokio-rs/tokio/blob/9bc782acfcebf198eda21be63d5b315bf40b565b/tokio/src/sync/watch.rs | 721 | 780 |
tokio-rs/tokio:tokio/src/sync/watch.rs:20 | /// ```
pub async fn wait_for(
&mut self,
mut f: impl FnMut(&T) -> bool,
) -> Result<Ref<'_, T>, error::RecvError> {
let mut closed = false;
loop {
{
let inner = self.shared.value.read().unwrap();
let new_version = self.shared.state.lo... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | 9bc782acfcebf198eda21be63d5b315bf40b565b | github | async-runtime | https://github.com/tokio-rs/tokio/blob/9bc782acfcebf198eda21be63d5b315bf40b565b/tokio/src/sync/watch.rs | 761 | 820 |
tokio-rs/tokio:tokio/src/sync/watch.rs:21 | pub fn same_channel(&self, other: &Self) -> bool {
Arc::ptr_eq(&self.shared, &other.shared)
}
cfg_process_driver! {
pub(crate) fn try_has_changed(&mut self) -> Option<Result<(), error::RecvError>> {
maybe_changed(&self.shared, &mut self.version)
}
}
}
fn maybe_changed<T... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | 9bc782acfcebf198eda21be63d5b315bf40b565b | github | async-runtime | https://github.com/tokio-rs/tokio/blob/9bc782acfcebf198eda21be63d5b315bf40b565b/tokio/src/sync/watch.rs | 801 | 860 |
tokio-rs/tokio:tokio/src/sync/watch.rs:22 | // In order to avoid a race condition, we first request a notification,
// **then** check the current value's version. If a new version exists,
// the notification request is dropped.
let notified = shared.notify_rx.notified();
if let Some(ret) = maybe_changed(shared, version) {
... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | 9bc782acfcebf198eda21be63d5b315bf40b565b | github | async-runtime | https://github.com/tokio-rs/tokio/blob/9bc782acfcebf198eda21be63d5b315bf40b565b/tokio/src/sync/watch.rs | 841 | 900 |
tokio-rs/tokio:tokio/src/sync/watch.rs:26 | /// let (state_tx, mut state_rx) = watch::channel(State { counter: 1 });
/// let inc_counter_if_odd = |state: &mut State| {
/// if state.counter % 2 == 1 {
/// state.counter += 1;
/// return true;
/// }
/// false
/// };
///
/// assert_eq!(state_rx.borrow()... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | 9bc782acfcebf198eda21be63d5b315bf40b565b | github | async-runtime | https://github.com/tokio-rs/tokio/blob/9bc782acfcebf198eda21be63d5b315bf40b565b/tokio/src/sync/watch.rs | 1,001 | 1,060 |
tokio-rs/tokio:tokio/src/sync/watch.rs:27 | // Drop the lock to avoid poisoning it.
drop(lock);
// Forward the panic to the caller.
panic::resume_unwind(panicked);
// Unreachable
}
};
self.shared.state.increment_version();
// Rele... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | 9bc782acfcebf198eda21be63d5b315bf40b565b | github | async-runtime | https://github.com/tokio-rs/tokio/blob/9bc782acfcebf198eda21be63d5b315bf40b565b/tokio/src/sync/watch.rs | 1,041 | 1,100 |
tokio-rs/tokio:tokio/src/sync/watch.rs:29 | ///
/// drop(rx);
/// assert!(tx.is_closed());
/// ```
pub fn is_closed(&self) -> bool {
self.receiver_count() == 0
}
/// Completes when all receivers have dropped.
///
/// This allows the producer to get notified when interest in the produced
/// values is canceled and imme... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | 9bc782acfcebf198eda21be63d5b315bf40b565b | github | async-runtime | https://github.com/tokio-rs/tokio/blob/9bc782acfcebf198eda21be63d5b315bf40b565b/tokio/src/sync/watch.rs | 1,121 | 1,180 |
tokio-rs/tokio:tokio/src/sync/watch.rs:32 | ///
/// ```
/// use tokio::sync::watch;
///
/// #[tokio::main]
/// async fn main() {
/// let (tx, rx1) = watch::channel("hello");
///
/// assert_eq!(1, tx.receiver_count());
///
/// let mut _rx2 = rx1.clone();
///
/// assert_eq!(2, tx.receiver_count());
... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | 9bc782acfcebf198eda21be63d5b315bf40b565b | github | async-runtime | https://github.com/tokio-rs/tokio/blob/9bc782acfcebf198eda21be63d5b315bf40b565b/tokio/src/sync/watch.rs | 1,241 | 1,300 |
tokio-rs/tokio:tokio/src/sync/watch.rs:21 | pub fn same_channel(&self, other: &Self) -> bool {
Arc::ptr_eq(&self.shared, &other.shared)
}
cfg_process_driver! {
pub(crate) fn try_has_changed(&mut self) -> Option<Result<(), error::RecvError>> {
maybe_changed(&self.shared, &mut self.version)
}
}
}
fn maybe_changed<T... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | ad7f988da377c365cacb5ca24d044a9be5de5889 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/ad7f988da377c365cacb5ca24d044a9be5de5889/tokio/src/sync/watch.rs | 801 | 860 |
tokio-rs/tokio:tokio/src/sync/watch.rs:9 | impl BigNotify {
pub(super) fn new() -> Self {
Self {
#[cfg(not(all(
not(loom),
feature = "sync",
any(feature = "rt", feature = "macros")
)))]
next: AtomicUsize::new(0),
inner:... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | 9d51b76d017cfef12e053760fa31f0845c214e3a | github | async-runtime | https://github.com/tokio-rs/tokio/blob/9d51b76d017cfef12e053760fa31f0845c214e3a/tokio/src/sync/watch.rs | 321 | 380 |
tokio-rs/tokio:tokio/src/sync/watch.rs:10 | const CLOSED: usize = 1;
/// The version part of the state. The lowest bit is always zero.
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub(super) struct Version(usize);
/// Snapshot of the state. The first bit is used as the CLOSED bit.
/// The remaining bits are used as the version.
///
... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | 9d51b76d017cfef12e053760fa31f0845c214e3a | github | async-runtime | https://github.com/tokio-rs/tokio/blob/9d51b76d017cfef12e053760fa31f0845c214e3a/tokio/src/sync/watch.rs | 361 | 420 |
tokio-rs/tokio:tokio/src/sync/watch.rs:11 | }
/// Is the closed bit set?
pub(super) fn is_closed(self) -> bool {
(self.0 & CLOSED) == CLOSED
}
}
impl AtomicState {
/// Create a new `AtomicState` that is not closed and which has the
/// version set to `Version::initial()`.
pub(super) fn new() -... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | 9d51b76d017cfef12e053760fa31f0845c214e3a | github | async-runtime | https://github.com/tokio-rs/tokio/blob/9d51b76d017cfef12e053760fa31f0845c214e3a/tokio/src/sync/watch.rs | 401 | 460 |
tokio-rs/tokio:tokio/src/sync/watch.rs:12 | ///
/// The following example prints `hello! world! `.
///
/// ```
/// use tokio::sync::watch;
/// use tokio::time::{Duration, sleep};
///
/// # async fn dox() -> Result<(), Box<dyn std::error::Error>> {
/// let (tx, mut rx) = watch::channel("hello");
///
/// tokio::spawn(async move {
/// // Use the equivalent of a... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | 9d51b76d017cfef12e053760fa31f0845c214e3a | github | async-runtime | https://github.com/tokio-rs/tokio/blob/9d51b76d017cfef12e053760fa31f0845c214e3a/tokio/src/sync/watch.rs | 441 | 500 |
tokio-rs/tokio:tokio/src/sync/watch.rs:16 | Ref { inner, has_changed }
}
/// Checks if this channel contains a message that this receiver has not yet
/// seen. The new value is not marked as seen.
///
/// Although this method is called `has_changed`, it does not check new
/// messages for equality, so this call will return true even if t... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | 9d51b76d017cfef12e053760fa31f0845c214e3a | github | async-runtime | https://github.com/tokio-rs/tokio/blob/9d51b76d017cfef12e053760fa31f0845c214e3a/tokio/src/sync/watch.rs | 601 | 660 |
tokio-rs/tokio:tokio/src/sync/watch.rs:19 | ///
/// When this function returns, the value that was passed to the closure
/// when it returned `true` will be considered seen.
///
/// If the channel is closed, then `wait_for` will return a `RecvError`.
/// Once this happens, no more messages can ever be sent on the channel.
/// When an erro... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | 9d51b76d017cfef12e053760fa31f0845c214e3a | github | async-runtime | https://github.com/tokio-rs/tokio/blob/9d51b76d017cfef12e053760fa31f0845c214e3a/tokio/src/sync/watch.rs | 721 | 780 |
tokio-rs/tokio:tokio/src/sync/watch.rs:20 | /// assert!(rx2.wait_for(|val| *val == "goodbye").await.is_ok());
/// assert_eq!(*rx2.borrow(), "goodbye");
/// }
/// ```
pub async fn wait_for(
&mut self,
mut f: impl FnMut(&T) -> bool,
) -> Result<Ref<'_, T>, error::RecvError> {
let mut closed = false;
loop ... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | 9d51b76d017cfef12e053760fa31f0845c214e3a | github | async-runtime | https://github.com/tokio-rs/tokio/blob/9d51b76d017cfef12e053760fa31f0845c214e3a/tokio/src/sync/watch.rs | 761 | 820 |
tokio-rs/tokio:tokio/src/sync/watch.rs:21 | /// let (tx3, rx3) = tokio::sync::watch::channel(true);
/// assert!(!rx3.same_channel(&rx2));
/// ```
pub fn same_channel(&self, other: &Self) -> bool {
Arc::ptr_eq(&self.shared, &other.shared)
}
cfg_process_driver! {
pub(crate) fn try_has_changed(&mut self) -> Option<Result<(), err... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | 9d51b76d017cfef12e053760fa31f0845c214e3a | github | async-runtime | https://github.com/tokio-rs/tokio/blob/9d51b76d017cfef12e053760fa31f0845c214e3a/tokio/src/sync/watch.rs | 801 | 860 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.