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:22 | crate::trace::async_trace_leaf().await;
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();
... | 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 | 841 | 900 |
tokio-rs/tokio:tokio/src/sync/watch.rs:24 | return Err(error::SendError(value));
}
self.send_replace(value);
Ok(())
}
/// Modifies the watched value **unconditionally** in-place,
/// notifying all receivers.
///
/// This can be useful for modifying the watched value, without
/// having to allocate a new instance.... | 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 | 921 | 980 |
tokio-rs/tokio:tokio/src/sync/watch.rs:26 | /// struct State {
/// 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;
/// }
/// ... | 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 | 1,001 | 1,060 |
tokio-rs/tokio:tokio/src/sync/watch.rs:27 | // Continue if modified
}
Err(panicked) => {
// Drop the lock to avoid poisoning it.
drop(lock);
// Forward the panic to the caller.
panic::resume_unwind(panicked);
// Unreachable
... | 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 | 1,041 | 1,100 |
tokio-rs/tokio:tokio/src/sync/watch.rs:29 | /// ```
/// 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... | 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 | 1,121 | 1,180 |
tokio-rs/tokio:tokio/src/sync/watch.rs:32 | /// Returns the number of receivers that currently exist.
///
/// # Examples
///
/// ```
/// use tokio::sync::watch;
///
/// #[tokio::main]
/// async fn main() {
/// let (tx, rx1) = watch::channel("hello");
///
/// assert_eq!(1, tx.receiver_count());
///
/// ... | 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 | 1,241 | 1,300 |
tokio-rs/tokio:tokio/src/sync/watch.rs:33 | #[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.send(... | 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 | 1,281 | 1,340 |
tokio-rs/tokio:tokio/src/sync/watch.rs:34 | #[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));
let send_threa... | 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 | 1,321 | 1,360 |
tokio-rs/tokio:tokio/src/sync/watch.rs:22 | crate::trace::async_trace_leaf().await;
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();
... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | 804511822b4be12a958d01fe1156b0a71bc8e6f7 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/804511822b4be12a958d01fe1156b0a71bc8e6f7/tokio/src/sync/watch.rs | 841 | 900 |
tokio-rs/tokio:tokio/src/sync/watch.rs:26 | /// 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 | 804511822b4be12a958d01fe1156b0a71bc8e6f7 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/804511822b4be12a958d01fe1156b0a71bc8e6f7/tokio/src/sync/watch.rs | 1,001 | 1,060 |
tokio-rs/tokio:tokio/src/sync/watch.rs:31 | /// # 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 | 804511822b4be12a958d01fe1156b0a71bc8e6f7 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/804511822b4be12a958d01fe1156b0a71bc8e6f7/tokio/src/sync/watch.rs | 1,201 | 1,260 |
tokio-rs/tokio:tokio/src/sync/watch.rs:32 | }
impl<T> Drop for Sender<T> {
fn drop(&mut self) {
self.shared.state.set_closed();
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, loo... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | 804511822b4be12a958d01fe1156b0a71bc8e6f7 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/804511822b4be12a958d01fe1156b0a71bc8e6f7/tokio/src/sync/watch.rs | 1,241 | 1,300 |
tokio-rs/tokio:tokio/src/sync/watch.rs:33 | 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();
let send_thread = thread::spawn(move || {
... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | 804511822b4be12a958d01fe1156b0a71bc8e6f7 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/804511822b4be12a958d01fe1156b0a71bc8e6f7/tokio/src/sync/watch.rs | 1,281 | 1,339 |
tokio-rs/tokio:tokio/src/sync/watch.rs:34 | recv.changed().now_or_never();
recv
});
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 || {
... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | 804511822b4be12a958d01fe1156b0a71bc8e6f7 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/804511822b4be12a958d01fe1156b0a71bc8e6f7/tokio/src/sync/watch.rs | 1,321 | 1,339 |
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 | 61042b4d90fa737dcf922e01466ded812c0f1a03 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/61042b4d90fa737dcf922e01466ded812c0f1a03/tokio/src/sync/watch.rs | 361 | 420 |
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 | 61042b4d90fa737dcf922e01466ded812c0f1a03 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/61042b4d90fa737dcf922e01466ded812c0f1a03/tokio/src/sync/watch.rs | 601 | 660 |
tokio-rs/tokio:tokio/src/sync/watch.rs:19 | /// called on the last value, and that it returned `false` for that value.
/// (If the closure returned `true`, then the last value would have been
/// returned instead of the error.)
///
/// Like the `borrow` method, the returned borrow holds a read lock on the
/// inner value. This means that long... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | 61042b4d90fa737dcf922e01466ded812c0f1a03 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/61042b4d90fa737dcf922e01466ded812c0f1a03/tokio/src/sync/watch.rs | 721 | 780 |
tokio-rs/tokio:tokio/src/sync/watch.rs:20 | ) -> Result<Ref<'_, T>, error::RecvError> {
let mut closed = false;
loop {
{
let inner = self.shared.value.read().unwrap();
let new_version = self.shared.state.load().version();
let has_changed = self.version != new_version;
se... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | 61042b4d90fa737dcf922e01466ded812c0f1a03 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/61042b4d90fa737dcf922e01466ded812c0f1a03/tokio/src/sync/watch.rs | 761 | 820 |
tokio-rs/tokio:tokio/src/sync/watch.rs:21 | 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>(
shared: &Shared<T>,
version: &mut Version,
) -> Option<Result<(), error::RecvError>> {
// Load t... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | 61042b4d90fa737dcf922e01466ded812c0f1a03 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/61042b4d90fa737dcf922e01466ded812c0f1a03/tokio/src/sync/watch.rs | 801 | 860 |
tokio-rs/tokio:tokio/src/sync/watch.rs:22 | if let Some(ret) = maybe_changed(shared, version) {
return ret;
}
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()... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | 61042b4d90fa737dcf922e01466ded812c0f1a03 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/61042b4d90fa737dcf922e01466ded812c0f1a03/tokio/src/sync/watch.rs | 841 | 900 |
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 | cb1e10b7452e56ca964bcd1cfdd78997791f37d6 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/cb1e10b7452e56ca964bcd1cfdd78997791f37d6/tokio/src/sync/watch.rs | 361 | 420 |
tokio-rs/tokio:tokio/src/sync/watch.rs:11 | /// version set to `Version::initial()`.
pub(super) fn new() -> Self {
AtomicState(AtomicUsize::new(0))
}
/// Load the current value of the state.
pub(super) fn load(&self) -> StateSnapshot {
StateSnapshot(self.0.load(SeqCst))
}
/// Increment the... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | cb1e10b7452e56ca964bcd1cfdd78997791f37d6 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/cb1e10b7452e56ca964bcd1cfdd78997791f37d6/tokio/src/sync/watch.rs | 401 | 460 |
tokio-rs/tokio:tokio/src/sync/watch.rs:12 | /// tokio::spawn(async move {
/// // Use the equivalent of a "do-while" loop so the initial value is
/// // processed before awaiting the `changed()` future.
/// loop {
/// println!("{}! ", *rx.borrow_and_update());
/// if rx.changed().await.is_err() {
/// break;
/// }
//... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | cb1e10b7452e56ca964bcd1cfdd78997791f37d6 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/cb1e10b7452e56ca964bcd1cfdd78997791f37d6/tokio/src/sync/watch.rs | 441 | 500 |
tokio-rs/tokio:tokio/src/sync/watch.rs:19 | /// use tokio::sync::watch;
///
/// #[tokio::main]
///
/// async fn main() {
/// 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
... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | cb1e10b7452e56ca964bcd1cfdd78997791f37d6 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/cb1e10b7452e56ca964bcd1cfdd78997791f37d6/tokio/src/sync/watch.rs | 721 | 780 |
tokio-rs/tokio:tokio/src/sync/watch.rs:20 | if closed {
return Err(error::RecvError(()));
}
// Wait for the value to change.
closed = changed_impl(&self.shared, &mut self.version).await.is_err();
}
}
/// Returns `true` if receivers belong to the same channel.
///
/// # Examples
///... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | cb1e10b7452e56ca964bcd1cfdd78997791f37d6 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/cb1e10b7452e56ca964bcd1cfdd78997791f37d6/tokio/src/sync/watch.rs | 761 | 820 |
tokio-rs/tokio:tokio/src/sync/watch.rs:21 | if *version != new_version {
// Observe the new version and return
*version = new_version;
return Some(Ok(()));
}
if state.is_closed() {
// All receivers have dropped.
return Some(Err(error::RecvError(())));
}
None
}
async fn changed_impl<T>(
shared: &Share... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | cb1e10b7452e56ca964bcd1cfdd78997791f37d6 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/cb1e10b7452e56ca964bcd1cfdd78997791f37d6/tokio/src/sync/watch.rs | 801 | 860 |
tokio-rs/tokio:tokio/src/sync/watch.rs:22 | Self::from_shared(version, shared)
}
}
impl<T> Drop for Receiver<T> {
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` hand... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | cb1e10b7452e56ca964bcd1cfdd78997791f37d6 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/cb1e10b7452e56ca964bcd1cfdd78997791f37d6/tokio/src/sync/watch.rs | 841 | 900 |
tokio-rs/tokio:tokio/src/sync/watch.rs:2 | //!
//! The value in the channel will not be dropped until the sender and all receivers
//! have been dropped.
//!
//! # Thread safety
//!
//! Both [`Sender`] and [`Receiver`] are thread safe. They can be moved to other
//! threads and can be used in a concurrent environment. Clones of [`Receiver`]
//! handles may be m... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | d8847cf89171e8b7872195724830a276c421f63e | github | async-runtime | https://github.com/tokio-rs/tokio/blob/d8847cf89171e8b7872195724830a276c421f63e/tokio/src/sync/watch.rs | 41 | 100 |
tokio-rs/tokio:tokio/src/sync/watch.rs:5 | /// }
///
/// // Now the value has already been marked as seen and could
/// // never be modified again (after the sender has been dropped).
/// assert!(!rx.borrow().has_changed());
/// }
/// ```
pub fn has_changed(&self) -> bool {
self.has_changed
}
}
struct Sha... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | d8847cf89171e8b7872195724830a276c421f63e | github | async-runtime | https://github.com/tokio-rs/tokio/blob/d8847cf89171e8b7872195724830a276c421f63e/tokio/src/sync/watch.rs | 161 | 220 |
tokio-rs/tokio:tokio/src/sync/watch.rs:6 | .finish()
}
}
pub mod error {
//! Watch error types.
use std::error::Error;
use std::fmt;
/// Error produced when sending a value fails.
#[derive(PartialEq, Eq, Clone, Copy)]
pub struct SendError<T>(pub T);
// ===== impl SendError =====
impl<T> fmt::Debug for SendError<T> {
... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | d8847cf89171e8b7872195724830a276c421f63e | github | async-runtime | https://github.com/tokio-rs/tokio/blob/d8847cf89171e8b7872195724830a276c421f63e/tokio/src/sync/watch.rs | 201 | 260 |
tokio-rs/tokio:tokio/src/sync/watch.rs:7 | }
impl Error for RecvError {}
}
mod big_notify {
use super::*;
use crate::sync::notify::Notified;
// To avoid contention on the lock inside the `Notify`, we store multiple
// copies of it. Then, we use either circular access or randomness to spread
// out threads over different `Notify` objec... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | d8847cf89171e8b7872195724830a276c421f63e | github | async-runtime | https://github.com/tokio-rs/tokio/blob/d8847cf89171e8b7872195724830a276c421f63e/tokio/src/sync/watch.rs | 241 | 300 |
tokio-rs/tokio:tokio/src/sync/watch.rs:8 | for notify in &self.inner {
notify.notify_waiters();
}
}
/// This function implements the case where randomness is not available.
#[cfg(not(all(not(loom), feature = "sync", any(feature = "rt", feature = "macros"))))]
pub(super) fn notified(&self) -> Notified<... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | d8847cf89171e8b7872195724830a276c421f63e | github | async-runtime | https://github.com/tokio-rs/tokio/blob/d8847cf89171e8b7872195724830a276c421f63e/tokio/src/sync/watch.rs | 281 | 340 |
tokio-rs/tokio:tokio/src/sync/watch.rs:9 | /// The state stored in an atomic integer.
#[derive(Debug)]
pub(super) struct AtomicState(AtomicUsize);
impl Version {
/// Get the initial version when creating the channel.
pub(super) fn initial() -> Self {
Version(0)
}
}
impl StateSnapshot {
/// Extrac... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | d8847cf89171e8b7872195724830a276c421f63e | github | async-runtime | https://github.com/tokio-rs/tokio/blob/d8847cf89171e8b7872195724830a276c421f63e/tokio/src/sync/watch.rs | 321 | 380 |
tokio-rs/tokio:tokio/src/sync/watch.rs:10 | /// Set the closed bit in the state.
pub(super) fn set_closed(&self) {
self.0.fetch_or(CLOSED, SeqCst);
}
}
}
/// 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 ... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | d8847cf89171e8b7872195724830a276c421f63e | github | async-runtime | https://github.com/tokio-rs/tokio/blob/d8847cf89171e8b7872195724830a276c421f63e/tokio/src/sync/watch.rs | 361 | 420 |
tokio-rs/tokio:tokio/src/sync/watch.rs:11 | notify_rx: big_notify::BigNotify::new(),
notify_tx: Notify::new(),
});
let tx = Sender {
shared: shared.clone(),
};
let rx = Receiver {
shared,
version: Version::initial(),
};
(tx, rx)
}
impl<T> Receiver<T> {
fn from_shared(version: Version, shared: Arc<Sh... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | d8847cf89171e8b7872195724830a276c421f63e | github | async-runtime | https://github.com/tokio-rs/tokio/blob/d8847cf89171e8b7872195724830a276c421f63e/tokio/src/sync/watch.rs | 401 | 460 |
tokio-rs/tokio:tokio/src/sync/watch.rs:17 | /// [`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 | d8847cf89171e8b7872195724830a276c421f63e | github | async-runtime | https://github.com/tokio-rs/tokio/blob/d8847cf89171e8b7872195724830a276c421f63e/tokio/src/sync/watch.rs | 641 | 700 |
tokio-rs/tokio:tokio/src/sync/watch.rs:18 | if (!closed || has_changed) && f(&inner) {
return Ok(Ref { inner, has_changed });
}
}
if closed {
return Err(error::RecvError(()));
}
// Wait for the value to change.
closed = changed_impl(&self.shared, &mu... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | d8847cf89171e8b7872195724830a276c421f63e | github | async-runtime | https://github.com/tokio-rs/tokio/blob/d8847cf89171e8b7872195724830a276c421f63e/tokio/src/sync/watch.rs | 681 | 740 |
tokio-rs/tokio:tokio/src/sync/watch.rs:19 | ) -> 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
*version = new_version;
return Some(Ok(()));
}
if state.... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | d8847cf89171e8b7872195724830a276c421f63e | github | async-runtime | https://github.com/tokio-rs/tokio/blob/d8847cf89171e8b7872195724830a276c421f63e/tokio/src/sync/watch.rs | 721 | 780 |
tokio-rs/tokio:tokio/src/sync/watch.rs:20 | 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 drop(&mut self) {
// No synchronization necessary as this is only used as a count... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | d8847cf89171e8b7872195724830a276c421f63e | github | async-runtime | https://github.com/tokio-rs/tokio/blob/d8847cf89171e8b7872195724830a276c421f63e/tokio/src/sync/watch.rs | 761 | 820 |
tokio-rs/tokio:tokio/src/sync/watch.rs:21 | // This is pretty much only useful as a hint anyway, so synchronization isn't critical.
if 0 == self.receiver_count() {
return Err(error::SendError(value));
}
self.send_replace(value);
Ok(())
}
/// Modifies the watched value **unconditionally** in-place,
/// not... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | d8847cf89171e8b7872195724830a276c421f63e | github | async-runtime | https://github.com/tokio-rs/tokio/blob/d8847cf89171e8b7872195724830a276c421f63e/tokio/src/sync/watch.rs | 801 | 860 |
tokio-rs/tokio:tokio/src/sync/watch.rs:23 | /// use tokio::sync::watch;
///
/// struct State {
/// 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;
/// ... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | d8847cf89171e8b7872195724830a276c421f63e | github | async-runtime | https://github.com/tokio-rs/tokio/blob/d8847cf89171e8b7872195724830a276c421f63e/tokio/src/sync/watch.rs | 881 | 940 |
tokio-rs/tokio:tokio/src/sync/watch.rs:24 | return false;
}
// Continue if modified
}
Err(panicked) => {
// Drop the lock to avoid poisoning it.
drop(lock);
// Forward the panic to the caller.
panic::resume_unwin... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | d8847cf89171e8b7872195724830a276c421f63e | github | async-runtime | https://github.com/tokio-rs/tokio/blob/d8847cf89171e8b7872195724830a276c421f63e/tokio/src/sync/watch.rs | 921 | 980 |
tokio-rs/tokio:tokio/src/sync/watch.rs:26 | /// # Examples
///
/// ```
/// 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 dr... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | d8847cf89171e8b7872195724830a276c421f63e | github | async-runtime | https://github.com/tokio-rs/tokio/blob/d8847cf89171e8b7872195724830a276c421f63e/tokio/src/sync/watch.rs | 1,001 | 1,060 |
tokio-rs/tokio:tokio/src/sync/watch.rs:29 | }
/// Returns the number of receivers that currently exist.
///
/// # Examples
///
/// ```
/// use tokio::sync::watch;
///
/// #[tokio::main]
/// async fn main() {
/// let (tx, rx1) = watch::channel("hello");
///
/// assert_eq!(1, tx.receiver_count());
///
... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | d8847cf89171e8b7872195724830a276c421f63e | github | async-runtime | https://github.com/tokio-rs/tokio/blob/d8847cf89171e8b7872195724830a276c421f63e/tokio/src/sync/watch.rs | 1,121 | 1,180 |
tokio-rs/tokio:tokio/src/sync/watch.rs:30 | }
#[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.se... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | d8847cf89171e8b7872195724830a276c421f63e | github | async-runtime | https://github.com/tokio-rs/tokio/blob/d8847cf89171e8b7872195724830a276c421f63e/tokio/src/sync/watch.rs | 1,161 | 1,220 |
tokio-rs/tokio:tokio/src/sync/watch.rs:31 | }
#[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));
let sen... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | d8847cf89171e8b7872195724830a276c421f63e | github | async-runtime | https://github.com/tokio-rs/tokio/blob/d8847cf89171e8b7872195724830a276c421f63e/tokio/src/sync/watch.rs | 1,201 | 1,242 |
tokio-rs/tokio:tokio/src/sync/watch.rs:5 | /// }
///
/// // Now the value has already been marked as seen and could
/// // never be modified again (after the sender has been dropped).
/// assert!(!rx.borrow().has_changed());
/// }
/// ```
pub fn has_changed(&self) -> bool {
self.has_changed
}
}
struct Sha... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | 7a99f87df203d589d9a8ad042a64b105a954f9fb | github | async-runtime | https://github.com/tokio-rs/tokio/blob/7a99f87df203d589d9a8ad042a64b105a954f9fb/tokio/src/sync/watch.rs | 161 | 220 |
tokio-rs/tokio:tokio/src/sync/watch.rs:6 | .finish()
}
}
pub mod error {
//! Watch error types.
use std::fmt;
/// Error produced when sending a value fails.
#[derive(PartialEq, Eq, Clone, Copy)]
pub struct SendError<T>(pub T);
// ===== impl SendError =====
impl<T> fmt::Debug for SendError<T> {
fn fmt(&self, f: &mut f... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | 7a99f87df203d589d9a8ad042a64b105a954f9fb | github | async-runtime | https://github.com/tokio-rs/tokio/blob/7a99f87df203d589d9a8ad042a64b105a954f9fb/tokio/src/sync/watch.rs | 201 | 260 |
tokio-rs/tokio:tokio/src/sync/watch.rs:7 | impl std::error::Error for RecvError {}
}
mod big_notify {
use super::*;
use crate::sync::notify::Notified;
// To avoid contention on the lock inside the `Notify`, we store multiple
// copies of it. Then, we use either circular access or randomness to spread
// out threads over different `Notify` ... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | 7a99f87df203d589d9a8ad042a64b105a954f9fb | github | async-runtime | https://github.com/tokio-rs/tokio/blob/7a99f87df203d589d9a8ad042a64b105a954f9fb/tokio/src/sync/watch.rs | 241 | 300 |
tokio-rs/tokio:tokio/src/sync/watch.rs:8 | notify.notify_waiters();
}
}
/// This function implements the case where randomness is not available.
#[cfg(not(all(not(loom), feature = "sync", any(feature = "rt", feature = "macros"))))]
pub(super) fn notified(&self) -> Notified<'_> {
let i = self.next.fetch_ad... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | 7a99f87df203d589d9a8ad042a64b105a954f9fb | github | async-runtime | https://github.com/tokio-rs/tokio/blob/7a99f87df203d589d9a8ad042a64b105a954f9fb/tokio/src/sync/watch.rs | 281 | 340 |
tokio-rs/tokio:tokio/src/sync/watch.rs:9 | #[derive(Debug)]
pub(super) struct AtomicState(AtomicUsize);
impl Version {
/// Get the initial version when creating the channel.
pub(super) fn initial() -> Self {
Version(0)
}
}
impl StateSnapshot {
/// Extract the version from the state.
pub(super... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | 7a99f87df203d589d9a8ad042a64b105a954f9fb | github | async-runtime | https://github.com/tokio-rs/tokio/blob/7a99f87df203d589d9a8ad042a64b105a954f9fb/tokio/src/sync/watch.rs | 321 | 380 |
tokio-rs/tokio:tokio/src/sync/watch.rs:10 | /// Set the closed bit in the state.
pub(super) fn set_closed(&self) {
self.0.fetch_or(CLOSED, SeqCst);
}
}
}
/// 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 ... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | 7a99f87df203d589d9a8ad042a64b105a954f9fb | github | async-runtime | https://github.com/tokio-rs/tokio/blob/7a99f87df203d589d9a8ad042a64b105a954f9fb/tokio/src/sync/watch.rs | 361 | 420 |
tokio-rs/tokio:tokio/src/sync/watch.rs:11 | notify_tx: Notify::new(),
});
let tx = Sender {
shared: shared.clone(),
};
let rx = Receiver {
shared,
version: Version::initial(),
};
(tx, rx)
}
impl<T> Receiver<T> {
fn from_shared(version: Version, shared: Arc<Shared<T>>) -> Self {
// No synchronization... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | 7a99f87df203d589d9a8ad042a64b105a954f9fb | github | async-runtime | https://github.com/tokio-rs/tokio/blob/7a99f87df203d589d9a8ad042a64b105a954f9fb/tokio/src/sync/watch.rs | 401 | 460 |
tokio-rs/tokio:tokio/src/sync/watch.rs:17 | ///
/// # Examples
///
/// ```
/// use tokio::sync::watch;
///
/// #[tokio::main]
///
/// async fn main() {
/// let (tx, _rx) = watch::channel("hello");
///
/// tx.send("goodbye").unwrap();
///
/// // here we subscribe to a second receiver
/// // n... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | 7a99f87df203d589d9a8ad042a64b105a954f9fb | github | async-runtime | https://github.com/tokio-rs/tokio/blob/7a99f87df203d589d9a8ad042a64b105a954f9fb/tokio/src/sync/watch.rs | 641 | 700 |
tokio-rs/tokio:tokio/src/sync/watch.rs:18 | return Ok(Ref { inner, has_changed });
}
}
if closed {
return Err(error::RecvError(()));
}
// Wait for the value to change.
closed = changed_impl(&self.shared, &mut self.version).await.is_err();
}
}
/// Return... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | 7a99f87df203d589d9a8ad042a64b105a954f9fb | github | async-runtime | https://github.com/tokio-rs/tokio/blob/7a99f87df203d589d9a8ad042a64b105a954f9fb/tokio/src/sync/watch.rs | 681 | 740 |
tokio-rs/tokio:tokio/src/sync/watch.rs:19 | // 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
*version = new_version;
return Some(Ok(()));
}
if state.is_closed() {
// All receivers have drop... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | 7a99f87df203d589d9a8ad042a64b105a954f9fb | github | async-runtime | https://github.com/tokio-rs/tokio/blob/7a99f87df203d589d9a8ad042a64b105a954f9fb/tokio/src/sync/watch.rs | 721 | 780 |
tokio-rs/tokio:tokio/src/sync/watch.rs:20 | 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 drop(&mut self) {
// No synchronization necessary as this is only used as a counter and
// not memory access.... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | 7a99f87df203d589d9a8ad042a64b105a954f9fb | github | async-runtime | https://github.com/tokio-rs/tokio/blob/7a99f87df203d589d9a8ad042a64b105a954f9fb/tokio/src/sync/watch.rs | 761 | 820 |
tokio-rs/tokio:tokio/src/sync/watch.rs:21 | if 0 == self.receiver_count() {
return Err(error::SendError(value));
}
self.send_replace(value);
Ok(())
}
/// Modifies the watched value **unconditionally** in-place,
/// notifying all receivers.
///
/// This can be useful for modifying the watched value, withou... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | 7a99f87df203d589d9a8ad042a64b105a954f9fb | github | async-runtime | https://github.com/tokio-rs/tokio/blob/7a99f87df203d589d9a8ad042a64b105a954f9fb/tokio/src/sync/watch.rs | 801 | 860 |
tokio-rs/tokio:tokio/src/sync/watch.rs:23 | ///
/// struct State {
/// 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;
/// }
... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | 7a99f87df203d589d9a8ad042a64b105a954f9fb | github | async-runtime | https://github.com/tokio-rs/tokio/blob/7a99f87df203d589d9a8ad042a64b105a954f9fb/tokio/src/sync/watch.rs | 881 | 940 |
tokio-rs/tokio:tokio/src/sync/watch.rs:24 | }
// Continue if modified
}
Err(panicked) => {
// Drop the lock to avoid poisoning it.
drop(lock);
// Forward the panic to the caller.
panic::resume_unwind(panicked);
/... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | 7a99f87df203d589d9a8ad042a64b105a954f9fb | github | async-runtime | https://github.com/tokio-rs/tokio/blob/7a99f87df203d589d9a8ad042a64b105a954f9fb/tokio/src/sync/watch.rs | 921 | 980 |
tokio-rs/tokio:tokio/src/sync/watch.rs:26 | ///
/// ```
/// 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.
///
... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | 7a99f87df203d589d9a8ad042a64b105a954f9fb | github | async-runtime | https://github.com/tokio-rs/tokio/blob/7a99f87df203d589d9a8ad042a64b105a954f9fb/tokio/src/sync/watch.rs | 1,001 | 1,060 |
tokio-rs/tokio:tokio/src/sync/watch.rs:30 | #[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.send(... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | 7a99f87df203d589d9a8ad042a64b105a954f9fb | github | async-runtime | https://github.com/tokio-rs/tokio/blob/7a99f87df203d589d9a8ad042a64b105a954f9fb/tokio/src/sync/watch.rs | 1,161 | 1,220 |
tokio-rs/tokio:tokio/src/sync/watch.rs:19 | // 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
*version = new_version;
return Some(Ok(()));
}
if state.is_closed() {
// All receivers have drop... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | 61b68a8abc7e6824f31eaa51e4b41e00a5471d20 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/61b68a8abc7e6824f31eaa51e4b41e00a5471d20/tokio/src/sync/watch.rs | 721 | 780 |
tokio-rs/tokio:tokio/src/sync/watch.rs:20 | let shared = self.shared.clone();
Self::from_shared(version, shared)
}
}
impl<T> Drop for Receiver<T> {
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) {
... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | 61b68a8abc7e6824f31eaa51e4b41e00a5471d20 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/61b68a8abc7e6824f31eaa51e4b41e00a5471d20/tokio/src/sync/watch.rs | 761 | 820 |
tokio-rs/tokio:tokio/src/sync/watch.rs:23 | /// 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 | 61b68a8abc7e6824f31eaa51e4b41e00a5471d20 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/61b68a8abc7e6824f31eaa51e4b41e00a5471d20/tokio/src/sync/watch.rs | 881 | 940 |
tokio-rs/tokio:tokio/src/sync/watch.rs:24 | }
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 | 61b68a8abc7e6824f31eaa51e4b41e00a5471d20 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/61b68a8abc7e6824f31eaa51e4b41e00a5471d20/tokio/src/sync/watch.rs | 921 | 980 |
tokio-rs/tokio:tokio/src/sync/watch.rs:5 | /// }
///
/// // Now the value has already been marked as seen and could
/// // never be modified again (after the sender has been dropped).
/// assert!(!rx.borrow().has_changed());
/// }
/// ```
pub fn has_changed(&self) -> bool {
self.has_changed
}
}
struct Sha... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | c1778eda38d5df316bc8356f2a7aa9f2d2e9abf0 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/c1778eda38d5df316bc8356f2a7aa9f2d2e9abf0/tokio/src/sync/watch.rs | 161 | 220 |
tokio-rs/tokio:tokio/src/sync/watch.rs:6 | .finish()
}
}
pub mod error {
//! Watch error types.
use std::fmt;
/// Error produced when sending a value fails.
#[derive(Debug)]
pub struct SendError<T>(pub T);
// ===== impl SendError =====
impl<T: fmt::Debug> fmt::Display for SendError<T> {
fn fmt(&self, fmt: &mut fmt::F... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | c1778eda38d5df316bc8356f2a7aa9f2d2e9abf0 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/c1778eda38d5df316bc8356f2a7aa9f2d2e9abf0/tokio/src/sync/watch.rs | 201 | 260 |
tokio-rs/tokio:tokio/src/sync/watch.rs:7 | use crate::sync::notify::Notified;
// To avoid contention on the lock inside the `Notify`, we store multiple
// copies of it. Then, we use either circular access or randomness to spread
// out threads over different `Notify` objects.
//
// Some simple benchmarks show that randomness performs slight... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | c1778eda38d5df316bc8356f2a7aa9f2d2e9abf0 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/c1778eda38d5df316bc8356f2a7aa9f2d2e9abf0/tokio/src/sync/watch.rs | 241 | 300 |
tokio-rs/tokio:tokio/src/sync/watch.rs:8 | pub(super) fn notified(&self) -> Notified<'_> {
let i = self.next.fetch_add(1, Relaxed) % 8;
self.inner[i].notified()
}
/// This function implements the case where randomness is available.
#[cfg(all(not(loom), feature = "sync", any(feature = "rt", feature = "macros")))]
... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | c1778eda38d5df316bc8356f2a7aa9f2d2e9abf0 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/c1778eda38d5df316bc8356f2a7aa9f2d2e9abf0/tokio/src/sync/watch.rs | 281 | 340 |
tokio-rs/tokio:tokio/src/sync/watch.rs:9 | Version(0)
}
}
impl StateSnapshot {
/// Extract the version from the state.
pub(super) fn version(self) -> Version {
Version(self.0 & !CLOSED)
}
/// Is the closed bit set?
pub(super) fn is_closed(self) -> bool {
(self.0 & CLOSED) == CLOSE... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | c1778eda38d5df316bc8356f2a7aa9f2d2e9abf0 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/c1778eda38d5df316bc8356f2a7aa9f2d2e9abf0/tokio/src/sync/watch.rs | 321 | 380 |
tokio-rs/tokio:tokio/src/sync/watch.rs:10 | /// 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 available to the [`Receiver`] half. All
/// intermediate values are dropped.
///
/// # Examples
///
/// ```
/// use tok... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | c1778eda38d5df316bc8356f2a7aa9f2d2e9abf0 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/c1778eda38d5df316bc8356f2a7aa9f2d2e9abf0/tokio/src/sync/watch.rs | 361 | 420 |
tokio-rs/tokio:tokio/src/sync/watch.rs:11 | let rx = Receiver {
shared,
version: Version::initial(),
};
(tx, rx)
}
impl<T> Receiver<T> {
fn from_shared(version: Version, shared: Arc<Shared<T>>) -> Self {
// No synchronization necessary as this is only used as a counter and
// not memory access.
shared.ref_cou... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | c1778eda38d5df316bc8356f2a7aa9f2d2e9abf0 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/c1778eda38d5df316bc8356f2a7aa9f2d2e9abf0/tokio/src/sync/watch.rs | 401 | 460 |
tokio-rs/tokio:tokio/src/sync/watch.rs:17 | /// #[tokio::main]
///
/// async fn main() {
/// 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 v... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | c1778eda38d5df316bc8356f2a7aa9f2d2e9abf0 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/c1778eda38d5df316bc8356f2a7aa9f2d2e9abf0/tokio/src/sync/watch.rs | 641 | 700 |
tokio-rs/tokio:tokio/src/sync/watch.rs:18 | }
// Wait for the value to change.
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);
... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | c1778eda38d5df316bc8356f2a7aa9f2d2e9abf0 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/c1778eda38d5df316bc8356f2a7aa9f2d2e9abf0/tokio/src/sync/watch.rs | 681 | 740 |
tokio-rs/tokio:tokio/src/sync/watch.rs:19 | *version = new_version;
return Some(Ok(()));
}
if state.is_closed() {
// All receivers have dropped.
return Some(Err(error::RecvError(())));
}
None
}
async fn changed_impl<T>(
shared: &Shared<T>,
version: &mut Version,
) -> Result<(), error::RecvError> {
loop {
... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | c1778eda38d5df316bc8356f2a7aa9f2d2e9abf0 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/c1778eda38d5df316bc8356f2a7aa9f2d2e9abf0/tokio/src/sync/watch.rs | 721 | 780 |
tokio-rs/tokio:tokio/src/sync/watch.rs:20 | impl<T> Drop for Receiver<T> {
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()`
... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | c1778eda38d5df316bc8356f2a7aa9f2d2e9abf0 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/c1778eda38d5df316bc8356f2a7aa9f2d2e9abf0/tokio/src/sync/watch.rs | 761 | 820 |
tokio-rs/tokio:tokio/src/sync/watch.rs:23 | /// return true;
/// }
/// false
/// };
///
/// 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!(st... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | c1778eda38d5df316bc8356f2a7aa9f2d2e9abf0 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/c1778eda38d5df316bc8356f2a7aa9f2d2e9abf0/tokio/src/sync/watch.rs | 881 | 940 |
tokio-rs/tokio:tokio/src/sync/watch.rs:24 | // Unreachable
}
};
self.shared.state.increment_version();
// Release the write lock.
//
// Incrementing the version counter while holding the lock ensures
// that receivers are able to figure out the version number of the
... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | c1778eda38d5df316bc8356f2a7aa9f2d2e9abf0 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/c1778eda38d5df316bc8356f2a7aa9f2d2e9abf0/tokio/src/sync/watch.rs | 921 | 980 |
tokio-rs/tokio:tokio/src/sync/watch.rs:28 | /// use tokio::sync::watch;
/// use tokio::time::Duration;
///
/// #[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... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | c1778eda38d5df316bc8356f2a7aa9f2d2e9abf0 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/c1778eda38d5df316bc8356f2a7aa9f2d2e9abf0/tokio/src/sync/watch.rs | 1,081 | 1,140 |
tokio-rs/tokio:tokio/src/sync/watch.rs:29 | /// let (tx, rx1) = watch::channel("hello");
///
/// assert_eq!(1, tx.receiver_count());
///
/// let mut _rx2 = rx1.clone();
///
/// assert_eq!(2, tx.receiver_count());
/// }
/// ```
pub fn receiver_count(&self) -> usize {
self.shared.ref_count_rx.load(Relaxed... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | c1778eda38d5df316bc8356f2a7aa9f2d2e9abf0 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/c1778eda38d5df316bc8356f2a7aa9f2d2e9abf0/tokio/src/sync/watch.rs | 1,121 | 1,180 |
tokio-rs/tokio:tokio/src/sync/watch.rs:30 | let (send, mut recv) = crate::sync::watch::channel(0i32);
send.send(1).unwrap();
let send_thread = thread::spawn(move || {
send.send(2).unwrap();
send
});
recv.changed().now_or_never();
let send = send_thread.join().unwrap()... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | c1778eda38d5df316bc8356f2a7aa9f2d2e9abf0 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/c1778eda38d5df316bc8356f2a7aa9f2d2e9abf0/tokio/src/sync/watch.rs | 1,161 | 1,220 |
tokio-rs/tokio:tokio/src/sync/watch.rs:31 | assert!(send.borrow().eq(&1));
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 || {
... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | c1778eda38d5df316bc8356f2a7aa9f2d2e9abf0 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/c1778eda38d5df316bc8356f2a7aa9f2d2e9abf0/tokio/src/sync/watch.rs | 1,201 | 1,231 |
tokio-rs/tokio:tokio/src/sync/watch.rs:16 | // the notification request is dropped.
let notified = self.shared.notify_rx.notified();
if let Some(ret) = maybe_changed(&self.shared, &mut self.version) {
return ret;
}
notified.await;
// loop around again in case the wake-up was spurious
... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | d63d6590787c91ddb942e7f0d743b8c9433adcc0 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/d63d6590787c91ddb942e7f0d743b8c9433adcc0/tokio/src/sync/watch.rs | 601 | 660 |
tokio-rs/tokio:tokio/src/sync/watch.rs:17 | let state = shared.state.load();
let new_version = state.version();
if *version != new_version {
// Observe the new version and return
*version = new_version;
return Some(Ok(()));
}
if state.is_closed() {
// All receivers have dropped.
return Some(Err(error::Rec... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | d63d6590787c91ddb942e7f0d743b8c9433adcc0 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/d63d6590787c91ddb942e7f0d743b8c9433adcc0/tokio/src/sync/watch.rs | 641 | 700 |
tokio-rs/tokio:tokio/src/sync/watch.rs:26 | /// # }
/// tx.send(100).unwrap();
/// });
///
/// 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().versio... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | d63d6590787c91ddb942e7f0d743b8c9433adcc0 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/d63d6590787c91ddb942e7f0d743b8c9433adcc0/tokio/src/sync/watch.rs | 1,001 | 1,060 |
tokio-rs/tokio:tokio/src/sync/watch.rs:27 | impl<T> Drop for Sender<T> {
fn drop(&mut self) {
self.shared.state.set_closed();
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))... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | d63d6590787c91ddb942e7f0d743b8c9433adcc0 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/d63d6590787c91ddb942e7f0d743b8c9433adcc0/tokio/src/sync/watch.rs | 1,041 | 1,100 |
tokio-rs/tokio:tokio/src/sync/watch.rs:28 | recv.changed().now_or_never();
recv
});
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();
... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | d63d6590787c91ddb942e7f0d743b8c9433adcc0 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/d63d6590787c91ddb942e7f0d743b8c9433adcc0/tokio/src/sync/watch.rs | 1,081 | 1,137 |
tokio-rs/tokio:tokio/src/sync/watch.rs:29 | });
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));
});
... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | d63d6590787c91ddb942e7f0d743b8c9433adcc0 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/d63d6590787c91ddb942e7f0d743b8c9433adcc0/tokio/src/sync/watch.rs | 1,121 | 1,137 |
tokio-rs/tokio:tokio/src/sync/watch.rs:2 | //!
//! # Thread safety
//!
//! Both [`Sender`] and [`Receiver`] are thread safe. They can be moved to other
//! threads and can be used in a concurrent environment. Clones of [`Receiver`]
//! handles may be moved to separate threads and also used concurrently.
//!
//! [`Sender`]: crate::sync::watch::Sender
//! [`Recei... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | b921fe45ac9fc49e18bc6e834065b61d246f56e0 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/b921fe45ac9fc49e18bc6e834065b61d246f56e0/tokio/src/sync/watch.rs | 41 | 100 |
tokio-rs/tokio:tokio/src/sync/watch.rs:5 | /// // never be modified again (after the sender has been dropped).
/// assert!(!rx.borrow().has_changed());
/// }
/// ```
pub fn has_changed(&self) -> bool {
self.has_changed
}
}
struct Shared<T> {
/// The most recent value.
value: RwLock<T>,
/// The current version.
... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | b921fe45ac9fc49e18bc6e834065b61d246f56e0 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/b921fe45ac9fc49e18bc6e834065b61d246f56e0/tokio/src/sync/watch.rs | 161 | 220 |
tokio-rs/tokio:tokio/src/sync/watch.rs:6 | pub mod error {
//! Watch error types.
use std::fmt;
/// Error produced when sending a value fails.
#[derive(Debug)]
pub struct SendError<T>(pub T);
// ===== impl SendError =====
impl<T: fmt::Debug> fmt::Display for SendError<T> {
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fm... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | b921fe45ac9fc49e18bc6e834065b61d246f56e0 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/b921fe45ac9fc49e18bc6e834065b61d246f56e0/tokio/src/sync/watch.rs | 201 | 260 |
tokio-rs/tokio:tokio/src/sync/watch.rs:7 | // copies of it. Then, we use either circular access or randomness to spread
// out threads over different `Notify` objects.
//
// Some simple benchmarks show that randomness performs slightly better than
// circular access (probably due to contention on `next`), so we prefer to
// use randomness wh... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | b921fe45ac9fc49e18bc6e834065b61d246f56e0 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/b921fe45ac9fc49e18bc6e834065b61d246f56e0/tokio/src/sync/watch.rs | 241 | 300 |
tokio-rs/tokio:tokio/src/sync/watch.rs:8 | }
/// 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[... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | b921fe45ac9fc49e18bc6e834065b61d246f56e0 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/b921fe45ac9fc49e18bc6e834065b61d246f56e0/tokio/src/sync/watch.rs | 281 | 340 |
tokio-rs/tokio:tokio/src/sync/watch.rs:9 | impl StateSnapshot {
/// Extract the version from the state.
pub(super) fn version(self) -> Version {
Version(self.0 & !CLOSED)
}
/// Is the closed bit set?
pub(super) fn is_closed(self) -> bool {
(self.0 & CLOSED) == CLOSED
}
}
impl Atom... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | b921fe45ac9fc49e18bc6e834065b61d246f56e0 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/b921fe45ac9fc49e18bc6e834065b61d246f56e0/tokio/src/sync/watch.rs | 321 | 380 |
tokio-rs/tokio:tokio/src/sync/watch.rs:10 | /// All values sent by [`Sender`] will become visible to the [`Receiver`] handles.
/// Only the last value sent is made available to the [`Receiver`] half. All
/// intermediate values are dropped.
///
/// # Examples
///
/// ```
/// use tokio::sync::watch;
///
/// # async fn dox() -> Result<(), Box<dyn std::error::Error... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | b921fe45ac9fc49e18bc6e834065b61d246f56e0 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/b921fe45ac9fc49e18bc6e834065b61d246f56e0/tokio/src/sync/watch.rs | 361 | 420 |
tokio-rs/tokio:tokio/src/sync/watch.rs:16 | if let Some(ret) = maybe_changed(&self.shared, &mut self.version) {
return ret;
}
notified.await;
// loop around again in case the wake-up was spurious
}
}
/// Returns `true` if receivers belong to the same channel.
///
/// # Examples
///... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | b921fe45ac9fc49e18bc6e834065b61d246f56e0 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/b921fe45ac9fc49e18bc6e834065b61d246f56e0/tokio/src/sync/watch.rs | 601 | 660 |
tokio-rs/tokio:tokio/src/sync/watch.rs:17 | if *version != new_version {
// Observe the new version and return
*version = new_version;
return Some(Ok(()));
}
if state.is_closed() {
// All receivers have dropped.
return Some(Err(error::RecvError(())));
}
None
}
impl<T> Clone for Receiver<T> {
fn clone... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | b921fe45ac9fc49e18bc6e834065b61d246f56e0 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/b921fe45ac9fc49e18bc6e834065b61d246f56e0/tokio/src/sync/watch.rs | 641 | 700 |
tokio-rs/tokio:tokio/src/sync/watch.rs:18 | /// isn't made available for future receivers (but returned with the
/// [`SendError`]).
///
/// 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
... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | b921fe45ac9fc49e18bc6e834065b61d246f56e0 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/b921fe45ac9fc49e18bc6e834065b61d246f56e0/tokio/src/sync/watch.rs | 681 | 740 |
tokio-rs/tokio:tokio/src/sync/watch.rs:20 | ///
/// Returns the result of the closure, i.e. `true` if the value has
/// been modified and `false` otherwise.
///
/// # Panics
///
/// This function panics when the invocation of the `modify` closure panics.
/// No receivers are notified when panicking. All changes of the watched
/// ... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | b921fe45ac9fc49e18bc6e834065b61d246f56e0 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/b921fe45ac9fc49e18bc6e834065b61d246f56e0/tokio/src/sync/watch.rs | 761 | 820 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.