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:9 | /// Waits for a change notification, then marks the newest value as seen.
///
/// If the newest value in the channel has not yet been marked seen when
/// this method is called, the method marks that value seen and returns
/// immediately. If the newest value has already been marked seen, then the
/... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | 75c07770bfbfea4e5fd914af819c741ed9c3fc36 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/75c07770bfbfea4e5fd914af819c741ed9c3fc36/tokio/src/sync/watch.rs | 321 | 380 |
tokio-rs/tokio:tokio/src/sync/watch.rs:10 | 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 = self.shared.notify_rx.notified();
if let Some(ret) = ... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | 75c07770bfbfea4e5fd914af819c741ed9c3fc36 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/75c07770bfbfea4e5fd914af819c741ed9c3fc36/tokio/src/sync/watch.rs | 361 | 420 |
tokio-rs/tokio:tokio/src/sync/watch.rs:11 | None
}
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... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | 75c07770bfbfea4e5fd914af819c741ed9c3fc36 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/75c07770bfbfea4e5fd914af819c741ed9c3fc36/tokio/src/sync/watch.rs | 401 | 460 |
tokio-rs/tokio:tokio/src/sync/watch.rs:12 | /// the previous value in the channel.
///
/// This can be useful for reusing the buffers inside a watched value.
/// Additionally, this method permits sending values even when there are no
/// receivers.
///
/// # Examples
///
/// ```
/// use tokio::sync::watch;
///
/// let ... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | 75c07770bfbfea4e5fd914af819c741ed9c3fc36 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/75c07770bfbfea4e5fd914af819c741ed9c3fc36/tokio/src/sync/watch.rs | 441 | 500 |
tokio-rs/tokio:tokio/src/sync/watch.rs:16 | /// // by spawning and sleeping, the message is sent after `main`
/// // hits the call to `changed`.
/// # if false {
/// tokio::time::sleep(Duration::from_millis(10)).await;
/// # }
/// tx.send(100).unwrap();
/// });
///
/// rx.cha... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | 75c07770bfbfea4e5fd914af819c741ed9c3fc36 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/75c07770bfbfea4e5fd914af819c741ed9c3fc36/tokio/src/sync/watch.rs | 601 | 660 |
tokio-rs/tokio:tokio/src/sync/watch.rs:17 | self.shared.ref_count_rx.load(Relaxed)
}
}
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 {
se... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | 75c07770bfbfea4e5fd914af819c741ed9c3fc36 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/75c07770bfbfea4e5fd914af819c741ed9c3fc36/tokio/src/sync/watch.rs | 641 | 700 |
tokio-rs/tokio:tokio/src/sync/watch.rs:11 | None
}
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... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | 03969cdae7674681d1b10926e6a56fbb8908dbb8 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/03969cdae7674681d1b10926e6a56fbb8908dbb8/tokio/src/sync/watch.rs | 401 | 460 |
tokio-rs/tokio:tokio/src/sync/watch.rs:12 | ///
/// ```
/// use tokio::sync::watch;
///
/// let (tx, _rx) = watch::channel(1);
/// assert_eq!(tx.send_replace(2).unwrap(), 1);
/// assert_eq!(tx.send_replace(3).unwrap(), 2);
/// ```
pub fn send_replace(&self, value: T) -> Result<T, error::SendError<T>> {
// This is pretty mu... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | 03969cdae7674681d1b10926e6a56fbb8908dbb8 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/03969cdae7674681d1b10926e6a56fbb8908dbb8/tokio/src/sync/watch.rs | 441 | 500 |
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 | dee3236c97b5502290046b9d677e108089188a9e | github | async-runtime | https://github.com/tokio-rs/tokio/blob/dee3236c97b5502290046b9d677e108089188a9e/tokio/src/sync/watch.rs | 41 | 100 |
tokio-rs/tokio:tokio/src/sync/watch.rs:3 | /// Sends values to the associated [`Receiver`](struct@Receiver).
///
/// Instances are created by the [`channel`](fn@channel) function.
#[derive(Debug)]
pub struct Sender<T> {
shared: Arc<Shared<T>>,
}
/// Returns a reference to the inner value
///
/// Outstanding borrows hold a read lock on the inner value. This... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | dee3236c97b5502290046b9d677e108089188a9e | github | async-runtime | https://github.com/tokio-rs/tokio/blob/dee3236c97b5502290046b9d677e108089188a9e/tokio/src/sync/watch.rs | 81 | 140 |
tokio-rs/tokio:tokio/src/sync/watch.rs:4 | //! 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<'_>) -> fmt::Result {
... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | dee3236c97b5502290046b9d677e108089188a9e | github | async-runtime | https://github.com/tokio-rs/tokio/blob/dee3236c97b5502290046b9d677e108089188a9e/tokio/src/sync/watch.rs | 121 | 180 |
tokio-rs/tokio:tokio/src/sync/watch.rs:9 | /// Wait for a change notification, then mark the newest value as seen.
///
/// If the newest value in the channel has not yet been marked seen when
/// this method is called, the method marks that value seen and returns
/// immediately. If the newest value has already been marked seen, then the
///... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | dee3236c97b5502290046b9d677e108089188a9e | github | async-runtime | https://github.com/tokio-rs/tokio/blob/dee3236c97b5502290046b9d677e108089188a9e/tokio/src/sync/watch.rs | 321 | 380 |
tokio-rs/tokio:tokio/src/sync/watch.rs:16 | /// # 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 | dee3236c97b5502290046b9d677e108089188a9e | github | async-runtime | https://github.com/tokio-rs/tokio/blob/dee3236c97b5502290046b9d677e108089188a9e/tokio/src/sync/watch.rs | 601 | 660 |
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 | b99eedc2ea43ee7707b11cfcca5eec339c9ffd5b | github | async-runtime | https://github.com/tokio-rs/tokio/blob/b99eedc2ea43ee7707b11cfcca5eec339c9ffd5b/tokio/src/sync/watch.rs | 41 | 100 |
tokio-rs/tokio:tokio/src/sync/watch.rs:3 | ///
/// Instances are created by the [`channel`](fn@channel) function.
#[derive(Debug)]
pub struct Sender<T> {
shared: Arc<Shared<T>>,
}
/// Returns a reference to the inner value
///
/// Outstanding borrows hold a read lock on the inner value. This means that
/// long lived borrows could cause the produce half to... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | b99eedc2ea43ee7707b11cfcca5eec339c9ffd5b | github | async-runtime | https://github.com/tokio-rs/tokio/blob/b99eedc2ea43ee7707b11cfcca5eec339c9ffd5b/tokio/src/sync/watch.rs | 81 | 140 |
tokio-rs/tokio:tokio/src/sync/watch.rs:4 | 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<'_>) -> fmt::Result {
write!(fmt, "channel clo... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | b99eedc2ea43ee7707b11cfcca5eec339c9ffd5b | github | async-runtime | https://github.com/tokio-rs/tokio/blob/b99eedc2ea43ee7707b11cfcca5eec339c9ffd5b/tokio/src/sync/watch.rs | 121 | 180 |
tokio-rs/tokio:tokio/src/sync/watch.rs:5 | #[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.
///
/// The CLOSED bit tracks whether the Sender has been dropped. Dropping all
/// receivers does n... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | b99eedc2ea43ee7707b11cfcca5eec339c9ffd5b | github | async-runtime | https://github.com/tokio-rs/tokio/blob/b99eedc2ea43ee7707b11cfcca5eec339c9ffd5b/tokio/src/sync/watch.rs | 161 | 220 |
tokio-rs/tokio:tokio/src/sync/watch.rs:6 | /// Load the current value of the state.
pub(super) fn load(&self) -> StateSnapshot {
StateSnapshot(self.0.load(SeqCst))
}
/// Increment the version counter.
pub(super) fn increment_version(&self) {
// Increment by two to avoid touching the CLOSED bit.
... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | b99eedc2ea43ee7707b11cfcca5eec339c9ffd5b | github | async-runtime | https://github.com/tokio-rs/tokio/blob/b99eedc2ea43ee7707b11cfcca5eec339c9ffd5b/tokio/src/sync/watch.rs | 201 | 260 |
tokio-rs/tokio:tokio/src/sync/watch.rs:7 | /// # Ok(())
/// # }
/// ```
///
/// [`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: ... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | b99eedc2ea43ee7707b11cfcca5eec339c9ffd5b | github | async-runtime | https://github.com/tokio-rs/tokio/blob/b99eedc2ea43ee7707b11cfcca5eec339c9ffd5b/tokio/src/sync/watch.rs | 241 | 300 |
tokio-rs/tokio:tokio/src/sync/watch.rs:9 | ///
/// If the newest value in the channel has not yet been marked seen when
/// this method is called, the method marks that value seen and returns
/// immediately. If the newest value has already been marked seen, then the
/// method sleeps until a new message is sent by the [`Sender`] connected to
... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | b99eedc2ea43ee7707b11cfcca5eec339c9ffd5b | github | async-runtime | https://github.com/tokio-rs/tokio/blob/b99eedc2ea43ee7707b11cfcca5eec339c9ffd5b/tokio/src/sync/watch.rs | 321 | 380 |
tokio-rs/tokio:tokio/src/sync/watch.rs:10 | // 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 = self.shared.notify_rx.notified();
if let Some(ret) = maybe_changed(&self... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | b99eedc2ea43ee7707b11cfcca5eec339c9ffd5b | github | async-runtime | https://github.com/tokio-rs/tokio/blob/b99eedc2ea43ee7707b11cfcca5eec339c9ffd5b/tokio/src/sync/watch.rs | 361 | 420 |
tokio-rs/tokio:tokio/src/sync/watch.rs:11 | None
}
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... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | b99eedc2ea43ee7707b11cfcca5eec339c9ffd5b | github | async-runtime | https://github.com/tokio-rs/tokio/blob/b99eedc2ea43ee7707b11cfcca5eec339c9ffd5b/tokio/src/sync/watch.rs | 401 | 460 |
tokio-rs/tokio:tokio/src/sync/watch.rs:3 | ///
/// Instances are created by the [`channel`](fn@channel) function.
#[derive(Debug)]
pub struct Sender<T> {
shared: Arc<Shared<T>>,
}
/// Returns a reference to the inner value
///
/// Outstanding borrows hold a read lock on the inner value. This means that
/// long lived borrows could cause the produce half to... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | 8aa2bfe23e739b19a0787ab141d2965d9e891997 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/8aa2bfe23e739b19a0787ab141d2965d9e891997/tokio/src/sync/watch.rs | 81 | 140 |
tokio-rs/tokio:tokio/src/sync/watch.rs:4 | use std::fmt;
/// Error produced when sending a value fails.
#[derive(Debug)]
pub struct SendError<T> {
pub(crate) inner: T,
}
// ===== impl SendError =====
impl<T: fmt::Debug> fmt::Display for SendError<T> {
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | 8aa2bfe23e739b19a0787ab141d2965d9e891997 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/8aa2bfe23e739b19a0787ab141d2965d9e891997/tokio/src/sync/watch.rs | 121 | 180 |
tokio-rs/tokio:tokio/src/sync/watch.rs:6 | AtomicState(AtomicUsize::new(0))
}
/// Load the current value of the state.
pub(super) fn load(&self) -> StateSnapshot {
StateSnapshot(self.0.load(SeqCst))
}
/// Increment the version counter.
pub(super) fn increment_version(&self) {
// Increment... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | 8aa2bfe23e739b19a0787ab141d2965d9e891997 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/8aa2bfe23e739b19a0787ab141d2965d9e891997/tokio/src/sync/watch.rs | 201 | 260 |
tokio-rs/tokio:tokio/src/sync/watch.rs:7 | ///
/// tx.send("world")?;
/// # Ok(())
/// # }
/// ```
///
/// [`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: AtomicUsi... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | 8aa2bfe23e739b19a0787ab141d2965d9e891997 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/8aa2bfe23e739b19a0787ab141d2965d9e891997/tokio/src/sync/watch.rs | 241 | 300 |
tokio-rs/tokio:tokio/src/sync/watch.rs:10 | pub async fn changed(&mut self) -> Result<(), error::RecvError> {
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 not... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | 8aa2bfe23e739b19a0787ab141d2965d9e891997 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/8aa2bfe23e739b19a0787ab141d2965d9e891997/tokio/src/sync/watch.rs | 361 | 420 |
tokio-rs/tokio:tokio/src/sync/watch.rs:11 | }
None
}
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 ... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | 8aa2bfe23e739b19a0787ab141d2965d9e891997 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/8aa2bfe23e739b19a0787ab141d2965d9e891997/tokio/src/sync/watch.rs | 401 | 460 |
tokio-rs/tokio:tokio/src/sync/watch.rs:12 | 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
// value they are currently looking at.
drop... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | 8aa2bfe23e739b19a0787ab141d2965d9e891997 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/8aa2bfe23e739b19a0787ab141d2965d9e891997/tokio/src/sync/watch.rs | 441 | 500 |
tokio-rs/tokio:tokio/src/sync/watch.rs:4 | use std::fmt;
/// Error produced when sending a value fails.
#[derive(Debug)]
pub struct SendError<T> {
pub(crate) inner: T,
}
// ===== impl SendError =====
impl<T: fmt::Debug> fmt::Display for SendError<T> {
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | e66217575bdb91c9f180bb1a8f9d0008f3ffa35b | github | async-runtime | https://github.com/tokio-rs/tokio/blob/e66217575bdb91c9f180bb1a8f9d0008f3ffa35b/tokio/src/sync/watch.rs | 121 | 180 |
tokio-rs/tokio:tokio/src/sync/watch.rs:5 | /// 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.
#[derive(Copy, Clone, Debug)]
pub(... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | e66217575bdb91c9f180bb1a8f9d0008f3ffa35b | github | async-runtime | https://github.com/tokio-rs/tokio/blob/e66217575bdb91c9f180bb1a8f9d0008f3ffa35b/tokio/src/sync/watch.rs | 161 | 220 |
tokio-rs/tokio:tokio/src/sync/watch.rs:6 | /// Load the current value of the state.
pub(super) fn load(&self) -> StateSnapshot {
StateSnapshot(self.0.load(SeqCst))
}
/// Increment the version counter.
pub(super) fn increment_version(&self) {
// Increment by two to avoid touching the CLOSED bit.
... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | e66217575bdb91c9f180bb1a8f9d0008f3ffa35b | github | async-runtime | https://github.com/tokio-rs/tokio/blob/e66217575bdb91c9f180bb1a8f9d0008f3ffa35b/tokio/src/sync/watch.rs | 201 | 260 |
tokio-rs/tokio:tokio/src/sync/watch.rs:7 | /// # }
/// ```
///
/// [`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: Notify::new()... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | e66217575bdb91c9f180bb1a8f9d0008f3ffa35b | github | async-runtime | https://github.com/tokio-rs/tokio/blob/e66217575bdb91c9f180bb1a8f9d0008f3ffa35b/tokio/src/sync/watch.rs | 241 | 300 |
tokio-rs/tokio:tokio/src/sync/watch.rs:9 | /// If the newest value in the channel has not yet been marked seen when
/// this method is called, the method marks that value seen and returns
/// immediately. If the newest value has already been marked seen, then the
/// method sleeps until a new message is sent by the [`Sender`] connected to
/// th... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | e66217575bdb91c9f180bb1a8f9d0008f3ffa35b | github | async-runtime | https://github.com/tokio-rs/tokio/blob/e66217575bdb91c9f180bb1a8f9d0008f3ffa35b/tokio/src/sync/watch.rs | 321 | 380 |
tokio-rs/tokio:tokio/src/sync/watch.rs:10 | // **then** check the current value's version. If a new version exists,
// 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;
}
... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | e66217575bdb91c9f180bb1a8f9d0008f3ffa35b | github | async-runtime | https://github.com/tokio-rs/tokio/blob/e66217575bdb91c9f180bb1a8f9d0008f3ffa35b/tokio/src/sync/watch.rs | 361 | 420 |
tokio-rs/tokio:tokio/src/sync/watch.rs:11 | }
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 co... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | e66217575bdb91c9f180bb1a8f9d0008f3ffa35b | github | async-runtime | https://github.com/tokio-rs/tokio/blob/e66217575bdb91c9f180bb1a8f9d0008f3ffa35b/tokio/src/sync/watch.rs | 401 | 460 |
tokio-rs/tokio:tokio/src/sync/watch.rs:13 | /// assert!(!tx.is_closed());
///
/// drop(rx);
/// assert!(tx.is_closed());
/// ```
pub fn is_closed(&self) -> bool {
self.shared.ref_count_rx.load(Relaxed) == 0
}
/// Completes when all receivers have dropped.
///
/// This allows the producer to get notified when interest ... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | e66217575bdb91c9f180bb1a8f9d0008f3ffa35b | github | async-runtime | https://github.com/tokio-rs/tokio/blob/e66217575bdb91c9f180bb1a8f9d0008f3ffa35b/tokio/src/sync/watch.rs | 481 | 540 |
tokio-rs/tokio:tokio/src/sync/watch.rs:14 | if self.shared.ref_count_rx.load(Relaxed) == 0 {
return;
}
notified.await;
debug_assert_eq!(0, self.shared.ref_count_rx.load(Relaxed));
}
cfg_signal_internal! {
pub(crate) fn subscribe(&self) -> Receiver<T> {
let shared = self.shared.clone();
... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | e66217575bdb91c9f180bb1a8f9d0008f3ffa35b | github | async-runtime | https://github.com/tokio-rs/tokio/blob/e66217575bdb91c9f180bb1a8f9d0008f3ffa35b/tokio/src/sync/watch.rs | 521 | 580 |
tokio-rs/tokio:tokio/src/sync/watch.rs:15 | 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 | e66217575bdb91c9f180bb1a8f9d0008f3ffa35b | github | async-runtime | https://github.com/tokio-rs/tokio/blob/e66217575bdb91c9f180bb1a8f9d0008f3ffa35b/tokio/src/sync/watch.rs | 561 | 620 |
tokio-rs/tokio:tokio/src/sync/watch.rs:16 | 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 || {
send.send(2).unwrap();
});
... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | e66217575bdb91c9f180bb1a8f9d0008f3ffa35b | github | async-runtime | https://github.com/tokio-rs/tokio/blob/e66217575bdb91c9f180bb1a8f9d0008f3ffa35b/tokio/src/sync/watch.rs | 601 | 658 |
tokio-rs/tokio:tokio/src/sync/watch.rs:17 | 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 || {
assert!(recv.borrow().eq(&2));
... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | e66217575bdb91c9f180bb1a8f9d0008f3ffa35b | github | async-runtime | https://github.com/tokio-rs/tokio/blob/e66217575bdb91c9f180bb1a8f9d0008f3ffa35b/tokio/src/sync/watch.rs | 641 | 658 |
tokio-rs/tokio:tokio/src/sync/watch.rs:11 | }
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 co... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | be26ca762521a0f5db58fe3814d738719ac20b77 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/be26ca762521a0f5db58fe3814d738719ac20b77/tokio/src/sync/watch.rs | 401 | 460 |
tokio-rs/tokio:tokio/src/sync/watch.rs:13 | /// assert!(tx.is_closed());
/// ```
pub fn is_closed(&self) -> bool {
self.shared.ref_count_rx.load(Relaxed) == 0
}
/// Completes when all receivers have dropped.
///
/// This allows the producer to get notified when interest in the produced
/// values is canceled and immediately s... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | be26ca762521a0f5db58fe3814d738719ac20b77 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/be26ca762521a0f5db58fe3814d738719ac20b77/tokio/src/sync/watch.rs | 481 | 540 |
tokio-rs/tokio:tokio/src/sync/watch.rs:14 | }
notified.await;
debug_assert_eq!(0, self.shared.ref_count_rx.load(Relaxed));
}
cfg_signal_internal! {
pub(crate) fn subscribe(&self) -> Receiver<T> {
let shared = self.shared.clone();
let version = shared.state.load().version();
Receiver::from_sha... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | be26ca762521a0f5db58fe3814d738719ac20b77 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/be26ca762521a0f5db58fe3814d738719ac20b77/tokio/src/sync/watch.rs | 521 | 580 |
tokio-rs/tokio:tokio/src/sync/watch.rs:15 | 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))]
mod tests {
use futures::future::FutureExt;
use loom... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | be26ca762521a0f5db58fe3814d738719ac20b77 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/be26ca762521a0f5db58fe3814d738719ac20b77/tokio/src/sync/watch.rs | 561 | 620 |
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 | c8ecfc894d06db466fe14b90f8b7f64eb3a3d5ea | github | async-runtime | https://github.com/tokio-rs/tokio/blob/c8ecfc894d06db466fe14b90f8b7f64eb3a3d5ea/tokio/src/sync/watch.rs | 41 | 100 |
tokio-rs/tokio:tokio/src/sync/watch.rs:3 | ///
/// Instances are created by the [`channel`](fn@channel) function.
#[derive(Debug)]
pub struct Sender<T> {
shared: Arc<Shared<T>>,
}
/// Returns a reference to the inner value
///
/// Outstanding borrows hold a read lock on the inner value. This means that
/// long lived borrows could cause the produce half to... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | c8ecfc894d06db466fe14b90f8b7f64eb3a3d5ea | github | async-runtime | https://github.com/tokio-rs/tokio/blob/c8ecfc894d06db466fe14b90f8b7f64eb3a3d5ea/tokio/src/sync/watch.rs | 81 | 140 |
tokio-rs/tokio:tokio/src/sync/watch.rs:4 | use std::fmt;
/// Error produced when sending a value fails.
#[derive(Debug)]
pub struct SendError<T> {
pub(crate) inner: T,
}
// ===== impl SendError =====
impl<T: fmt::Debug> fmt::Display for SendError<T> {
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | c8ecfc894d06db466fe14b90f8b7f64eb3a3d5ea | github | async-runtime | https://github.com/tokio-rs/tokio/blob/c8ecfc894d06db466fe14b90f8b7f64eb3a3d5ea/tokio/src/sync/watch.rs | 121 | 180 |
tokio-rs/tokio:tokio/src/sync/watch.rs:5 | /// intermediate values are dropped.
///
/// # Examples
///
/// ```
/// use tokio::sync::watch;
///
/// # async fn dox() -> Result<(), Box<dyn std::error::Error>> {
/// let (tx, mut rx) = watch::channel("hello");
///
/// tokio::spawn(async move {
/// while rx.changed().await.is_ok() {
/// pr... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | c8ecfc894d06db466fe14b90f8b7f64eb3a3d5ea | github | async-runtime | https://github.com/tokio-rs/tokio/blob/c8ecfc894d06db466fe14b90f8b7f64eb3a3d5ea/tokio/src/sync/watch.rs | 161 | 220 |
tokio-rs/tokio:tokio/src/sync/watch.rs:6 | impl<T> Receiver<T> {
fn from_shared(version: usize, shared: Arc<Shared<T>>) -> Self {
// No synchronization necessary as this is only used as a counter and
// not memory access.
shared.ref_count_rx.fetch_add(1, Relaxed);
Self { shared, version }
}
/// Returns a reference t... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | c8ecfc894d06db466fe14b90f8b7f64eb3a3d5ea | github | async-runtime | https://github.com/tokio-rs/tokio/blob/c8ecfc894d06db466fe14b90f8b7f64eb3a3d5ea/tokio/src/sync/watch.rs | 201 | 260 |
tokio-rs/tokio:tokio/src/sync/watch.rs:8 | ///
/// tokio::spawn(async move {
/// tx.send("goodbye").unwrap();
/// });
///
/// assert!(rx.changed().await.is_ok());
/// assert_eq!(*rx.borrow(), "goodbye");
///
/// // The `tx` handle has been dropped
/// assert!(rx.changed().await.is_err());
/... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | c8ecfc894d06db466fe14b90f8b7f64eb3a3d5ea | github | async-runtime | https://github.com/tokio-rs/tokio/blob/c8ecfc894d06db466fe14b90f8b7f64eb3a3d5ea/tokio/src/sync/watch.rs | 281 | 340 |
tokio-rs/tokio:tokio/src/sync/watch.rs:9 | let state = shared.version.load(SeqCst);
let new_version = state & !CLOSED;
if *version != new_version {
// Observe the new version and return
*version = new_version;
return Some(Ok(()));
}
if CLOSED == state & CLOSED {
// All receivers have dropped.
return Some... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | c8ecfc894d06db466fe14b90f8b7f64eb3a3d5ea | github | async-runtime | https://github.com/tokio-rs/tokio/blob/c8ecfc894d06db466fe14b90f8b7f64eb3a3d5ea/tokio/src/sync/watch.rs | 321 | 380 |
tokio-rs/tokio:tokio/src/sync/watch.rs:10 | // This is pretty much only useful as a hint anyway, so synchronization isn't critical.
if 0 == self.shared.ref_count_rx.load(Relaxed) {
return Err(error::SendError { inner: value });
}
{
// Acquire the write lock and update the value.
let mut lock = self.sha... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | c8ecfc894d06db466fe14b90f8b7f64eb3a3d5ea | github | async-runtime | https://github.com/tokio-rs/tokio/blob/c8ecfc894d06db466fe14b90f8b7f64eb3a3d5ea/tokio/src/sync/watch.rs | 361 | 420 |
tokio-rs/tokio:tokio/src/sync/watch.rs:11 | /// ```
pub fn borrow(&self) -> Ref<'_, T> {
let inner = self.shared.value.read().unwrap();
Ref { inner }
}
/// Checks if the channel has been closed. This happens when all receivers
/// have dropped.
///
/// # Examples
///
/// ```
/// let (tx, rx) = tokio::sync::wat... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | c8ecfc894d06db466fe14b90f8b7f64eb3a3d5ea | github | async-runtime | https://github.com/tokio-rs/tokio/blob/c8ecfc894d06db466fe14b90f8b7f64eb3a3d5ea/tokio/src/sync/watch.rs | 401 | 460 |
tokio-rs/tokio:tokio/src/sync/watch.rs:12 | ///
/// tokio::spawn(async move {
/// // use `rx`
/// drop(rx);
/// });
///
/// // Waits for `rx` to drop
/// tx.closed().await;
/// println!("the `rx` handles dropped")
/// }
/// ```
pub async fn closed(&self) {
let notified = self... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | c8ecfc894d06db466fe14b90f8b7f64eb3a3d5ea | github | async-runtime | https://github.com/tokio-rs/tokio/blob/c8ecfc894d06db466fe14b90f8b7f64eb3a3d5ea/tokio/src/sync/watch.rs | 441 | 500 |
tokio-rs/tokio:tokio/src/sync/watch.rs:13 | /// 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 | c8ecfc894d06db466fe14b90f8b7f64eb3a3d5ea | github | async-runtime | https://github.com/tokio-rs/tokio/blob/c8ecfc894d06db466fe14b90f8b7f64eb3a3d5ea/tokio/src/sync/watch.rs | 481 | 540 |
tokio-rs/tokio:tokio/src/sync/watch.rs:6 | impl<T> Receiver<T> {
fn from_shared(version: usize, shared: Arc<Shared<T>>) -> Self {
// No synchronization necessary as this is only used as a counter and
// not memory access.
shared.ref_count_rx.fetch_add(1, Relaxed);
Self { shared, version }
}
/// Returns a reference t... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | b521cc26895c6331c8ced6be72f8b3d9947a9fb3 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/b521cc26895c6331c8ced6be72f8b3d9947a9fb3/tokio/src/sync/watch.rs | 201 | 260 |
tokio-rs/tokio:tokio/src/sync/watch.rs:7 | /// `borrow_and_update`.
///
/// Outstanding borrows hold a read lock. This means that long lived borrows
/// could cause the send half to block. It is recommended to keep the borrow
/// as short lived as possible.
///
/// [`changed`]: Receiver::changed
pub fn borrow_and_update(&mut self) ->... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | d1aa2df80ecc19d014c909813700e39c2197c96e | github | async-runtime | https://github.com/tokio-rs/tokio/blob/d1aa2df80ecc19d014c909813700e39c2197c96e/tokio/src/sync/watch.rs | 241 | 300 |
tokio-rs/tokio:tokio/src/sync/watch.rs:8 | ///
/// // The `tx` handle has been dropped
/// assert!(rx.changed().await.is_err());
/// }
/// ```
pub async fn changed(&mut self) -> Result<(), error::RecvError> {
loop {
// In order to avoid a race condition, we first request a notification,
// **then** che... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | d1aa2df80ecc19d014c909813700e39c2197c96e | github | async-runtime | https://github.com/tokio-rs/tokio/blob/d1aa2df80ecc19d014c909813700e39c2197c96e/tokio/src/sync/watch.rs | 281 | 340 |
tokio-rs/tokio:tokio/src/sync/watch.rs:9 | }
if CLOSED == state & CLOSED {
// All receivers have dropped.
return Some(Err(error::RecvError(())));
}
None
}
impl<T> Clone for Receiver<T> {
fn clone(&self) -> Self {
let version = self.version;
let shared = self.shared.clone();
Self::from_shared(version, s... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | d1aa2df80ecc19d014c909813700e39c2197c96e | github | async-runtime | https://github.com/tokio-rs/tokio/blob/d1aa2df80ecc19d014c909813700e39c2197c96e/tokio/src/sync/watch.rs | 321 | 380 |
tokio-rs/tokio:tokio/src/sync/watch.rs:10 | let mut lock = self.shared.value.write().unwrap();
*lock = value;
// Update the version. 2 is used so that the CLOSED bit is not set.
self.shared.version.fetch_add(2, SeqCst);
// Release the write lock.
//
// Incrementing the version counter whil... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | d1aa2df80ecc19d014c909813700e39c2197c96e | github | async-runtime | https://github.com/tokio-rs/tokio/blob/d1aa2df80ecc19d014c909813700e39c2197c96e/tokio/src/sync/watch.rs | 361 | 420 |
tokio-rs/tokio:tokio/src/sync/watch.rs:11 | /// have dropped.
///
/// # 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.shared.ref_count_rx.load(Relaxed) == 0
... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | d1aa2df80ecc19d014c909813700e39c2197c96e | github | async-runtime | https://github.com/tokio-rs/tokio/blob/d1aa2df80ecc19d014c909813700e39c2197c96e/tokio/src/sync/watch.rs | 401 | 460 |
tokio-rs/tokio:tokio/src/sync/watch.rs:12 | let notified = self.shared.notify_tx.notified();
if self.shared.ref_count_rx.load(Relaxed) == 0 {
return;
}
notified.await;
debug_assert_eq!(0, self.shared.ref_count_rx.load(Relaxed));
}
cfg_signal_internal! {
pub(crate) fn subscribe(&self) -> Receiver<T> {... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | d1aa2df80ecc19d014c909813700e39c2197c96e | github | async-runtime | https://github.com/tokio-rs/tokio/blob/d1aa2df80ecc19d014c909813700e39c2197c96e/tokio/src/sync/watch.rs | 441 | 500 |
tokio-rs/tokio:tokio/src/sync/watch.rs:13 | }
impl<T> Drop for Sender<T> {
fn drop(&mut self) {
self.shared.version.fetch_or(CLOSED, SeqCst);
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... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | d1aa2df80ecc19d014c909813700e39c2197c96e | github | async-runtime | https://github.com/tokio-rs/tokio/blob/d1aa2df80ecc19d014c909813700e39c2197c96e/tokio/src/sync/watch.rs | 481 | 540 |
tokio-rs/tokio:tokio/src/sync/watch.rs:5 | /// intermediate values are dropped.
///
/// # Examples
///
/// ```
/// use tokio::sync::watch;
///
/// # async fn dox() -> Result<(), Box<dyn std::error::Error>> {
/// let (tx, mut rx) = watch::channel("hello");
///
/// tokio::spawn(async move {
/// while rx.changed().await.is_ok() {
/// pr... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | 652f0ae7287794d7cd476d4965e143b2f3901bb4 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/652f0ae7287794d7cd476d4965e143b2f3901bb4/tokio/src/sync/watch.rs | 161 | 220 |
tokio-rs/tokio:tokio/src/sync/watch.rs:7 | /// use tokio::sync::watch;
///
/// #[tokio::main]
/// async fn main() {
/// let (tx, mut rx) = watch::channel("hello");
///
/// tokio::spawn(async move {
/// tx.send("goodbye").unwrap();
/// });
///
/// assert!(rx.changed().await.is_ok());
/// ass... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | 652f0ae7287794d7cd476d4965e143b2f3901bb4 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/652f0ae7287794d7cd476d4965e143b2f3901bb4/tokio/src/sync/watch.rs | 241 | 300 |
tokio-rs/tokio:tokio/src/sync/watch.rs:8 | fn maybe_changed<T>(
shared: &Shared<T>,
version: &mut usize,
) -> Option<Result<(), error::RecvError>> {
// Load the version from the state
let state = shared.version.load(SeqCst);
let new_version = state & !CLOSED;
if *version != new_version {
// Observe the new version and return
... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | 652f0ae7287794d7cd476d4965e143b2f3901bb4 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/652f0ae7287794d7cd476d4965e143b2f3901bb4/tokio/src/sync/watch.rs | 281 | 340 |
tokio-rs/tokio:tokio/src/sync/watch.rs:9 | }
impl<T> Sender<T> {
/// Sends a new value via the channel, notifying all receivers.
pub fn send(&self, value: T) -> Result<(), error::SendError<T>> {
// This is pretty much only useful as a hint anyway, so synchronization isn't critical.
if 0 == self.shared.ref_count_rx.load(Relaxed) {
... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | 652f0ae7287794d7cd476d4965e143b2f3901bb4 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/652f0ae7287794d7cd476d4965e143b2f3901bb4/tokio/src/sync/watch.rs | 321 | 380 |
tokio-rs/tokio:tokio/src/sync/watch.rs:10 | /// Checks if the channel has been closed. This happens when all receivers
/// have dropped.
///
/// # Examples
///
/// ```
/// let (tx, rx) = tokio::sync::watch::channel(());
/// assert!(!tx.is_closed());
///
/// drop(rx);
/// assert!(tx.is_closed());
/// ```
pub fn is_c... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | 652f0ae7287794d7cd476d4965e143b2f3901bb4 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/652f0ae7287794d7cd476d4965e143b2f3901bb4/tokio/src/sync/watch.rs | 361 | 420 |
tokio-rs/tokio:tokio/src/sync/watch.rs:11 | pub async fn closed(&self) {
let notified = self.shared.notify_tx.notified();
if self.shared.ref_count_rx.load(Relaxed) == 0 {
return;
}
notified.await;
debug_assert_eq!(0, self.shared.ref_count_rx.load(Relaxed));
}
cfg_signal_internal! {
pub(crate)... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | 652f0ae7287794d7cd476d4965e143b2f3901bb4 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/652f0ae7287794d7cd476d4965e143b2f3901bb4/tokio/src/sync/watch.rs | 401 | 460 |
tokio-rs/tokio:tokio/src/sync/watch.rs:12 | }
}
impl<T> Drop for Sender<T> {
fn drop(&mut self) {
self.shared.version.fetch_or(CLOSED, SeqCst);
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()
}
}
#[c... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | 652f0ae7287794d7cd476d4965e143b2f3901bb4 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/652f0ae7287794d7cd476d4965e143b2f3901bb4/tokio/src/sync/watch.rs | 441 | 500 |
tokio-rs/tokio:tokio/src/sync/watch.rs:10 | /// Checks if the channel has been closed. This happens when all receivers
/// have dropped.
///
/// # Examples
///
/// ```
/// let (tx, rx) = tokio::sync::watch::channel(());
/// assert!(!tx.is_closed());
///
/// drop(rx);
/// assert!(tx.is_closed());
/// ```
pub fn is_c... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | 1e2f893da2f33deef7fb7c6dc62b265f454c4456 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/1e2f893da2f33deef7fb7c6dc62b265f454c4456/tokio/src/sync/watch.rs | 361 | 420 |
tokio-rs/tokio:tokio/src/sync/watch.rs:11 | pub async fn closed(&self) {
let notified = self.shared.notify_tx.notified();
if self.shared.ref_count_rx.load(Relaxed) == 0 {
return;
}
notified.await;
debug_assert_eq!(0, self.shared.ref_count_rx.load(Relaxed));
}
cfg_signal_internal! {
pub(crate)... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | 1e2f893da2f33deef7fb7c6dc62b265f454c4456 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/1e2f893da2f33deef7fb7c6dc62b265f454c4456/tokio/src/sync/watch.rs | 401 | 460 |
tokio-rs/tokio:tokio/src/sync/watch.rs:12 | 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();
let send_threa... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | 1e2f893da2f33deef7fb7c6dc62b265f454c4456 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/1e2f893da2f33deef7fb7c6dc62b265f454c4456/tokio/src/sync/watch.rs | 441 | 500 |
tokio-rs/tokio:tokio/src/sync/watch.rs:13 | 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::spawn(move || {
... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | 1e2f893da2f33deef7fb7c6dc62b265f454c4456 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/1e2f893da2f33deef7fb7c6dc62b265f454c4456/tokio/src/sync/watch.rs | 481 | 518 |
tokio-rs/tokio:tokio/src/sync/watch.rs:5 | /// intermediate values are dropped.
///
/// # Examples
///
/// ```
/// use tokio::sync::watch;
///
/// # async fn dox() -> Result<(), Box<dyn std::error::Error>> {
/// let (tx, mut rx) = watch::channel("hello");
///
/// tokio::spawn(async move {
/// while rx.changed().await.is_ok() {
/// pr... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | e4f76688a00fa2ce81ab6c074700995095c29e1e | github | async-runtime | https://github.com/tokio-rs/tokio/blob/e4f76688a00fa2ce81ab6c074700995095c29e1e/tokio/src/sync/watch.rs | 161 | 220 |
tokio-rs/tokio:tokio/src/sync/watch.rs:2 | //!
//! 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
//! [`Receiver`]: crate::sync::watc... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | bcb95db4e2d4a9a43a3f3f5e129ddb7232599451 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/bcb95db4e2d4a9a43a3f3f5e129ddb7232599451/tokio/src/sync/watch.rs | 41 | 100 |
tokio-rs/tokio:tokio/src/sync/watch.rs:3 | #[derive(Debug)]
pub struct Sender<T> {
shared: Arc<Shared<T>>,
}
/// Returns a reference to the inner value
///
/// Outstanding borrows hold a read lock on the inner value. This means that
/// long lived borrows could cause the produce half to block. It is recommended
/// to keep the borrow as short lived as poss... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | bcb95db4e2d4a9a43a3f3f5e129ddb7232599451 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/bcb95db4e2d4a9a43a3f3f5e129ddb7232599451/tokio/src/sync/watch.rs | 81 | 140 |
tokio-rs/tokio:tokio/src/sync/watch.rs:4 | /// Error produced when sending a value fails.
#[derive(Debug)]
pub struct SendError<T> {
pub(crate) inner: T,
}
// ===== impl SendError =====
impl<T: fmt::Debug> fmt::Display for SendError<T> {
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(fmt, "c... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | bcb95db4e2d4a9a43a3f3f5e129ddb7232599451 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/bcb95db4e2d4a9a43a3f3f5e129ddb7232599451/tokio/src/sync/watch.rs | 121 | 180 |
tokio-rs/tokio:tokio/src/sync/watch.rs:5 | /// # Examples
///
/// ```
/// use tokio::sync::watch;
///
/// # async fn dox() -> Result<(), Box<dyn std::error::Error>> {
/// let (tx, mut rx) = watch::channel("hello");
///
/// tokio::spawn(async move {
/// while rx.changed().await.is_ok() {
/// println!("received = {:?}", *rx.borrow());
... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | bcb95db4e2d4a9a43a3f3f5e129ddb7232599451 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/bcb95db4e2d4a9a43a3f3f5e129ddb7232599451/tokio/src/sync/watch.rs | 161 | 220 |
tokio-rs/tokio:tokio/src/sync/watch.rs:7 | /// assert!(rx.changed().await.is_ok());
/// assert_eq!(*rx.borrow(), "goodbye");
///
/// // The `tx` handle has been dropped
/// assert!(rx.changed().await.is_err());
/// }
/// ```
pub async fn changed(&mut self) -> Result<(), error::RecvError> {
loop {
/... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | bcb95db4e2d4a9a43a3f3f5e129ddb7232599451 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/bcb95db4e2d4a9a43a3f3f5e129ddb7232599451/tokio/src/sync/watch.rs | 241 | 300 |
tokio-rs/tokio:tokio/src/sync/watch.rs:8 | return Some(Err(error::RecvError(())));
}
None
}
impl<T> Clone for Receiver<T> {
fn clone(&self) -> Self {
let version = self.version;
let shared = self.shared.clone();
// No synchronization necessary as this is only used as a counter and
// not memory access.
shar... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | bcb95db4e2d4a9a43a3f3f5e129ddb7232599451 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/bcb95db4e2d4a9a43a3f3f5e129ddb7232599451/tokio/src/sync/watch.rs | 281 | 340 |
tokio-rs/tokio:tokio/src/sync/watch.rs:10 | pub fn is_closed(&self) -> bool {
self.shared.ref_count_rx.load(Relaxed) == 0
}
/// Completes when all receivers have dropped.
///
/// This allows the producer to get notified when interest in the produced
/// values is canceled and immediately stop doing work.
///
/// # Examples
... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | bcb95db4e2d4a9a43a3f3f5e129ddb7232599451 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/bcb95db4e2d4a9a43a3f3f5e129ddb7232599451/tokio/src/sync/watch.rs | 361 | 420 |
tokio-rs/tokio:tokio/src/sync/watch.rs:11 | impl<T> Drop for Sender<T> {
fn drop(&mut self) {
self.shared.version.fetch_or(CLOSED, SeqCst);
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(al... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | bcb95db4e2d4a9a43a3f3f5e129ddb7232599451 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/bcb95db4e2d4a9a43a3f3f5e129ddb7232599451/tokio/src/sync/watch.rs | 401 | 460 |
tokio-rs/tokio:tokio/src/sync/watch.rs:7 | /// assert!(rx.changed().await.is_ok());
/// assert_eq!(*rx.borrow(), "goodbye");
///
/// // The `tx` handle has been dropped
/// assert!(rx.changed().await.is_err());
/// }
/// ```
pub async fn changed(&mut self) -> Result<(), error::RecvError> {
loop {
/... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | 0a04954d5ca74693cf7adbe2dd3a18de2987c519 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/0a04954d5ca74693cf7adbe2dd3a18de2987c519/tokio/src/sync/watch.rs | 241 | 300 |
tokio-rs/tokio:tokio/src/sync/watch.rs:8 | return Some(Err(error::RecvError(())));
}
None
}
impl<T> Clone for Receiver<T> {
fn clone(&self) -> Self {
let version = self.version;
let shared = self.shared.clone();
// No synchronization necessary as this is only used as a counter and
// not memory access.
shar... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | 0a04954d5ca74693cf7adbe2dd3a18de2987c519 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/0a04954d5ca74693cf7adbe2dd3a18de2987c519/tokio/src/sync/watch.rs | 281 | 340 |
tokio-rs/tokio:tokio/src/sync/watch.rs:2 | //!
//! 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
//! [`Receiver`]: crate::sync::watc... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | 1f862d2e950bf5f4fb31f202a544e86880f51191 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/1f862d2e950bf5f4fb31f202a544e86880f51191/tokio/src/sync/watch.rs | 41 | 100 |
tokio-rs/tokio:tokio/src/sync/watch.rs:3 | /// Returns a reference to the inner value
///
/// Outstanding borrows hold a read lock on the inner value. This means that
/// long lived borrows could cause the produce half to block. It is recommended
/// to keep the borrow as short lived as possible.
#[derive(Debug)]
pub struct Ref<'a, T> {
inner: RwLockReadGua... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | 1f862d2e950bf5f4fb31f202a544e86880f51191 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/1f862d2e950bf5f4fb31f202a544e86880f51191/tokio/src/sync/watch.rs | 81 | 140 |
tokio-rs/tokio:tokio/src/sync/watch.rs:4 | }
// ===== impl SendError =====
impl<T: fmt::Debug> fmt::Display for SendError<T> {
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(fmt, "channel closed")
}
}
impl<T: fmt::Debug> std::error::Error for SendError<T> {}
/// Error produced when receivi... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | 1f862d2e950bf5f4fb31f202a544e86880f51191 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/1f862d2e950bf5f4fb31f202a544e86880f51191/tokio/src/sync/watch.rs | 121 | 180 |
tokio-rs/tokio:tokio/src/sync/watch.rs:5 | /// # async fn dox() -> Result<(), Box<dyn std::error::Error>> {
/// let (tx, mut rx) = watch::channel("hello");
///
/// tokio::spawn(async move {
/// while rx.changed().await.is_ok() {
/// println!("received = {:?}", *rx.borrow());
/// }
/// });
///
/// tx.send("world")?;
//... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | 1f862d2e950bf5f4fb31f202a544e86880f51191 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/1f862d2e950bf5f4fb31f202a544e86880f51191/tokio/src/sync/watch.rs | 161 | 220 |
tokio-rs/tokio:tokio/src/sync/watch.rs:6 | ///
/// # Examples
///
/// ```
/// use tokio::sync::watch;
///
/// let (_, rx) = watch::channel("hello");
/// assert_eq!(*rx.borrow(), "hello");
/// ```
pub fn borrow(&self) -> Ref<'_, T> {
let inner = self.shared.value.read().unwrap();
Ref { inner }
}
/// Wa... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | 1f862d2e950bf5f4fb31f202a544e86880f51191 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/1f862d2e950bf5f4fb31f202a544e86880f51191/tokio/src/sync/watch.rs | 201 | 260 |
tokio-rs/tokio:tokio/src/sync/watch.rs:7 | /// }
/// ```
pub async fn changed(&mut self) -> Result<(), error::RecvError> {
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 droppe... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | 1f862d2e950bf5f4fb31f202a544e86880f51191 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/1f862d2e950bf5f4fb31f202a544e86880f51191/tokio/src/sync/watch.rs | 241 | 300 |
tokio-rs/tokio:tokio/src/sync/watch.rs:8 | impl<T> Clone for Receiver<T> {
fn clone(&self) -> Self {
let version = self.version;
let shared = self.shared.clone();
// No synchronization necessary as this is only used as a counter and
// not memory access.
shared.ref_count_rx.fetch_add(1, Relaxed);
Receiver { ... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | 1f862d2e950bf5f4fb31f202a544e86880f51191 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/1f862d2e950bf5f4fb31f202a544e86880f51191/tokio/src/sync/watch.rs | 281 | 340 |
tokio-rs/tokio:tokio/src/sync/watch.rs:10 | ///
/// This allows the producer to get notified when interest in the produced
/// values is canceled and immediately stop doing work.
///
/// # Examples
///
/// ```
/// use tokio::sync::watch;
///
/// #[tokio::main]
/// async fn main() {
/// let (tx, rx) = watch::channel... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | 1f862d2e950bf5f4fb31f202a544e86880f51191 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/1f862d2e950bf5f4fb31f202a544e86880f51191/tokio/src/sync/watch.rs | 361 | 420 |
tokio-rs/tokio:tokio/src/sync/watch.rs:8 | impl<T> Clone for Receiver<T> {
fn clone(&self) -> Self {
let version = self.version;
let shared = self.shared.clone();
// No synchronization necessary as this is only used as a counter and
// not memory access.
shared.ref_count_rx.fetch_add(1, Relaxed);
Receiver { ... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | f60860af7edefef5373d50d77ab605d648d60526 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/f60860af7edefef5373d50d77ab605d648d60526/tokio/src/sync/watch.rs | 281 | 340 |
tokio-rs/tokio:tokio/src/sync/watch.rs:9 | Ok(())
}
/// Checks if the channel has been closed. This happens when all receivers
/// have dropped.
///
/// # Examples
///
/// ```
/// let (tx, rx) = tokio::sync::watch::channel(());
/// assert!(!tx.is_closed());
///
/// drop(rx);
/// assert!(tx.is_closed());
/// `... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | f60860af7edefef5373d50d77ab605d648d60526 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/f60860af7edefef5373d50d77ab605d648d60526/tokio/src/sync/watch.rs | 321 | 380 |
tokio-rs/tokio:tokio/src/sync/watch.rs:10 | /// tx.closed().await;
/// println!("the `rx` handles dropped")
/// }
/// ```
pub async fn closed(&self) {
let notified = self.shared.notify_tx.notified();
if self.shared.ref_count_rx.load(Relaxed) == 0 {
return;
}
notified.await;
debug_asser... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | f60860af7edefef5373d50d77ab605d648d60526 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/f60860af7edefef5373d50d77ab605d648d60526/tokio/src/sync/watch.rs | 361 | 420 |
tokio-rs/tokio:tokio/src/sync/watch.rs:11 | fn watch_spurious_wakeup() {
loom::model(|| {
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().n... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | f60860af7edefef5373d50d77ab605d648d60526 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/f60860af7edefef5373d50d77ab605d648d60526/tokio/src/sync/watch.rs | 401 | 433 |
tokio-rs/tokio:tokio/src/sync/watch.rs:2 | //!
//! 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
//! [`Receiver`]: crate::sync::watc... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | d0ebb4154748166a4ba07baa4b424a1c45efd219 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/d0ebb4154748166a4ba07baa4b424a1c45efd219/tokio/src/sync/watch.rs | 41 | 100 |
tokio-rs/tokio:tokio/src/sync/watch.rs:6 | ///
/// # Examples
///
/// ```
/// use tokio::sync::watch;
///
/// let (_, rx) = watch::channel("hello");
/// assert_eq!(*rx.borrow(), "hello");
/// ```
pub fn borrow(&self) -> Ref<'_, T> {
let inner = self.shared.value.read().unwrap();
Ref { inner }
}
/// Wa... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | d0ebb4154748166a4ba07baa4b424a1c45efd219 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/d0ebb4154748166a4ba07baa4b424a1c45efd219/tokio/src/sync/watch.rs | 201 | 260 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.