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:21 | 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 lock = self.shared.value.write().unwrap();
// Update the value and catch possible panic inside func.
... | 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 | 801 | 860 |
tokio-rs/tokio:tokio/src/sync/watch.rs:26 | ///
/// rx.changed().await.unwrap();
/// assert_eq!(100, *rx.borrow());
/// }
/// ```
pub fn subscribe(&self) -> Receiver<T> {
let shared = self.shared.clone();
let version = shared.state.load().version();
// The CLOSED bit in the state tracks only whether the sender... | 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 | 1,001 | 1,060 |
tokio-rs/tokio:tokio/src/sync/watch.rs:27 | self.shared.notify_rx.notify_waiters();
}
}
// ===== impl Ref =====
impl<T> ops::Deref for Ref<'_, T> {
type Target = T;
fn deref(&self) -> &T {
self.inner.deref()
}
}
#[cfg(all(test, loom))]
mod tests {
use futures::future::FutureExt;
use loom::thread;
// test for https://githu... | 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 | 1,041 | 1,100 |
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 | 00bf5ee8a855c28324fa4dff3abf11ba9f562a85 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/00bf5ee8a855c28324fa4dff3abf11ba9f562a85/tokio/src/sync/watch.rs | 41 | 100 |
tokio-rs/tokio:tokio/src/sync/watch.rs:5 | /// assert!(!rx.borrow().has_changed());
/// }
/// ```
pub fn has_changed(&self) -> bool {
self.has_changed
}
}
#[derive(Debug)]
struct Shared<T> {
/// The most recent value.
value: RwLock<T>,
/// The current version.
///
/// The lowest bit represents a "closed" state. ... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | 00bf5ee8a855c28324fa4dff3abf11ba9f562a85 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/00bf5ee8a855c28324fa4dff3abf11ba9f562a85/tokio/src/sync/watch.rs | 161 | 220 |
tokio-rs/tokio:tokio/src/sync/watch.rs:6 | 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 receiving a change notification.
#[derive(Deb... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | 00bf5ee8a855c28324fa4dff3abf11ba9f562a85 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/00bf5ee8a855c28324fa4dff3abf11ba9f562a85/tokio/src/sync/watch.rs | 201 | 260 |
tokio-rs/tokio:tokio/src/sync/watch.rs:7 | pub(super) struct StateSnapshot(usize);
/// 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)
}
}... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | 00bf5ee8a855c28324fa4dff3abf11ba9f562a85 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/00bf5ee8a855c28324fa4dff3abf11ba9f562a85/tokio/src/sync/watch.rs | 241 | 300 |
tokio-rs/tokio:tokio/src/sync/watch.rs:8 | self.0.fetch_add(2, SeqCst);
}
/// 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 vi... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | 00bf5ee8a855c28324fa4dff3abf11ba9f562a85 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/00bf5ee8a855c28324fa4dff3abf11ba9f562a85/tokio/src/sync/watch.rs | 281 | 340 |
tokio-rs/tokio:tokio/src/sync/watch.rs:9 | state: AtomicState::new(),
ref_count_rx: AtomicUsize::new(1),
notify_rx: Notify::new(),
notify_tx: Notify::new(),
});
let tx = Sender {
shared: shared.clone(),
};
let rx = Receiver {
shared,
version: Version::initial(),
};
(tx, rx)
}
impl<T> Re... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | 00bf5ee8a855c28324fa4dff3abf11ba9f562a85 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/00bf5ee8a855c28324fa4dff3abf11ba9f562a85/tokio/src/sync/watch.rs | 321 | 380 |
tokio-rs/tokio:tokio/src/sync/watch.rs:14 | ///
/// // 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 | 00bf5ee8a855c28324fa4dff3abf11ba9f562a85 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/00bf5ee8a855c28324fa4dff3abf11ba9f562a85/tokio/src/sync/watch.rs | 521 | 580 |
tokio-rs/tokio:tokio/src/sync/watch.rs:15 | }
}
}
fn maybe_changed<T>(
shared: &Shared<T>,
version: &mut Version,
) -> Option<Result<(), error::RecvError>> {
// Load the version from the state
let state = shared.state.load();
let new_version = state.version();
if *version != new_version {
// Observe the new version and retur... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | 00bf5ee8a855c28324fa4dff3abf11ba9f562a85 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/00bf5ee8a855c28324fa4dff3abf11ba9f562a85/tokio/src/sync/watch.rs | 561 | 620 |
tokio-rs/tokio:tokio/src/sync/watch.rs:19 | /// assert_eq!(state_rx.borrow().counter, 1);
///
/// assert!(!state_rx.has_changed().unwrap());
/// assert!(state_tx.send_if_modified(inc_counter_if_odd));
/// assert!(state_rx.has_changed().unwrap());
/// assert_eq!(state_rx.borrow_and_update().counter, 2);
///
/// assert!(!state_rx.has_ch... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | 00bf5ee8a855c28324fa4dff3abf11ba9f562a85 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/00bf5ee8a855c28324fa4dff3abf11ba9f562a85/tokio/src/sync/watch.rs | 721 | 780 |
tokio-rs/tokio:tokio/src/sync/watch.rs:24 | /// let (tx, _rx) = watch::channel(0u64);
/// tx.send(5).unwrap();
/// let mut rx = tx.subscribe();
///
/// tokio::spawn(async move {
/// // by spawning and sleeping, the message is sent after `main`
/// // hits the call to `changed`.
/// # if false {
... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | 00bf5ee8a855c28324fa4dff3abf11ba9f562a85 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/00bf5ee8a855c28324fa4dff3abf11ba9f562a85/tokio/src/sync/watch.rs | 921 | 980 |
tokio-rs/tokio:tokio/src/sync/watch.rs:25 | ///
/// assert_eq!(2, tx.receiver_count());
/// }
/// ```
pub fn receiver_count(&self) -> usize {
self.shared.ref_count_rx.load(Relaxed)
}
}
impl<T> Drop for Sender<T> {
fn drop(&mut self) {
self.shared.state.set_closed();
self.shared.notify_rx.notify_waiters();
... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | 00bf5ee8a855c28324fa4dff3abf11ba9f562a85 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/00bf5ee8a855c28324fa4dff3abf11ba9f562a85/tokio/src/sync/watch.rs | 961 | 1,020 |
tokio-rs/tokio:tokio/src/sync/watch.rs:26 | send.send(2).unwrap();
send
});
recv.changed().now_or_never();
let send = send_thread.join().unwrap();
let recv_thread = thread::spawn(move || {
recv.changed().now_or_never();
recv.changed().now_or_never();
... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | 00bf5ee8a855c28324fa4dff3abf11ba9f562a85 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/00bf5ee8a855c28324fa4dff3abf11ba9f562a85/tokio/src/sync/watch.rs | 1,001 | 1,060 |
tokio-rs/tokio:tokio/src/sync/watch.rs:27 | });
recv.changed().now_or_never();
let send = send_thread.join().unwrap();
let recv_thread = thread::spawn(move || {
recv.changed().now_or_never();
recv.changed().now_or_never();
recv
});
send.send(3).unwrap()... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | 00bf5ee8a855c28324fa4dff3abf11ba9f562a85 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/00bf5ee8a855c28324fa4dff3abf11ba9f562a85/tokio/src/sync/watch.rs | 1,041 | 1,066 |
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 | 431ec68f93401fbca3c2976edc0fb6e55530cbc3 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/431ec68f93401fbca3c2976edc0fb6e55530cbc3/tokio/src/sync/watch.rs | 41 | 100 |
tokio-rs/tokio:tokio/src/sync/watch.rs:5 | /// }
/// ```
pub fn has_changed(&self) -> bool {
self.has_changed
}
}
#[derive(Debug)]
struct Shared<T> {
/// The most recent value.
value: RwLock<T>,
/// The current version.
///
/// The lowest bit represents a "closed" state. The rest of the bits
/// represent the curren... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | 431ec68f93401fbca3c2976edc0fb6e55530cbc3 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/431ec68f93401fbca3c2976edc0fb6e55530cbc3/tokio/src/sync/watch.rs | 161 | 220 |
tokio-rs/tokio:tokio/src/sync/watch.rs:6 | 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 receiving a change notification.
#[derive(Debug, Clone)]
pub struct RecvError(pub(super) ());
//... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | 431ec68f93401fbca3c2976edc0fb6e55530cbc3 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/431ec68f93401fbca3c2976edc0fb6e55530cbc3/tokio/src/sync/watch.rs | 201 | 260 |
tokio-rs/tokio:tokio/src/sync/watch.rs:7 | /// 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 | 431ec68f93401fbca3c2976edc0fb6e55530cbc3 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/431ec68f93401fbca3c2976edc0fb6e55530cbc3/tokio/src/sync/watch.rs | 241 | 300 |
tokio-rs/tokio:tokio/src/sync/watch.rs:8 | }
/// 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.
//... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | 431ec68f93401fbca3c2976edc0fb6e55530cbc3 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/431ec68f93401fbca3c2976edc0fb6e55530cbc3/tokio/src/sync/watch.rs | 281 | 340 |
tokio-rs/tokio:tokio/src/sync/watch.rs:9 | ref_count_rx: AtomicUsize::new(1),
notify_rx: Notify::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(vers... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | 431ec68f93401fbca3c2976edc0fb6e55530cbc3 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/431ec68f93401fbca3c2976edc0fb6e55530cbc3/tokio/src/sync/watch.rs | 321 | 380 |
tokio-rs/tokio:tokio/src/sync/watch.rs:14 | /// }
/// ```
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 | 431ec68f93401fbca3c2976edc0fb6e55530cbc3 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/431ec68f93401fbca3c2976edc0fb6e55530cbc3/tokio/src/sync/watch.rs | 521 | 580 |
tokio-rs/tokio:tokio/src/sync/watch.rs:15 | fn maybe_changed<T>(
shared: &Shared<T>,
version: &mut Version,
) -> Option<Result<(), error::RecvError>> {
// Load the version from the state
let state = shared.state.load();
let new_version = state.version();
if *version != new_version {
// Observe the new version and return
*... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | 431ec68f93401fbca3c2976edc0fb6e55530cbc3 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/431ec68f93401fbca3c2976edc0fb6e55530cbc3/tokio/src/sync/watch.rs | 561 | 620 |
tokio-rs/tokio:tokio/src/sync/watch.rs:19 | /// assert!(state_tx.send_if_modified(inc_counter_if_odd));
/// assert!(state_rx.has_changed().unwrap());
/// assert_eq!(state_rx.borrow_and_update().counter, 2);
///
/// assert!(!state_rx.has_changed().unwrap());
/// assert!(!state_tx.send_if_modified(inc_counter_if_odd));
/// assert!(!state_rx... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | 431ec68f93401fbca3c2976edc0fb6e55530cbc3 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/431ec68f93401fbca3c2976edc0fb6e55530cbc3/tokio/src/sync/watch.rs | 721 | 780 |
tokio-rs/tokio:tokio/src/sync/watch.rs:20 | // 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(lock);
}
self.shared.notify_rx.notify_waiters();
true
}
/// Sends a ne... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | 431ec68f93401fbca3c2976edc0fb6e55530cbc3 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/431ec68f93401fbca3c2976edc0fb6e55530cbc3/tokio/src/sync/watch.rs | 761 | 820 |
tokio-rs/tokio:tokio/src/sync/watch.rs:24 | /// // hits the call to `changed`.
/// # if false {
/// tokio::time::sleep(Duration::from_millis(10)).await;
/// # }
/// tx.send(100).unwrap();
/// });
///
/// rx.changed().await.unwrap();
/// assert_eq!(100, *rx.borrow());
/// }
... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | 431ec68f93401fbca3c2976edc0fb6e55530cbc3 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/431ec68f93401fbca3c2976edc0fb6e55530cbc3/tokio/src/sync/watch.rs | 921 | 980 |
tokio-rs/tokio:tokio/src/sync/watch.rs:25 | }
}
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, l... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | 431ec68f93401fbca3c2976edc0fb6e55530cbc3 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/431ec68f93401fbca3c2976edc0fb6e55530cbc3/tokio/src/sync/watch.rs | 961 | 1,020 |
tokio-rs/tokio:tokio/src/sync/watch.rs:26 | let send = send_thread.join().unwrap();
let recv_thread = thread::spawn(move || {
recv.changed().now_or_never();
recv.changed().now_or_never();
recv
});
send.send(3).unwrap();
let mut recv = recv_thread.join().unwrap();
... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | 431ec68f93401fbca3c2976edc0fb6e55530cbc3 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/431ec68f93401fbca3c2976edc0fb6e55530cbc3/tokio/src/sync/watch.rs | 1,001 | 1,060 |
tokio-rs/tokio:tokio/src/sync/watch.rs:27 | recv.changed().now_or_never();
recv.changed().now_or_never();
recv
});
send.send(3).unwrap();
let recv = recv_thread.join().unwrap();
assert!(recv.borrow().eq(&3));
assert!(send.borrow().eq(&3));
send.send(2).unwr... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | 431ec68f93401fbca3c2976edc0fb6e55530cbc3 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/431ec68f93401fbca3c2976edc0fb6e55530cbc3/tokio/src/sync/watch.rs | 1,041 | 1,060 |
tokio-rs/tokio:tokio/src/sync/watch.rs:15 | fn maybe_changed<T>(
shared: &Shared<T>,
version: &mut Version,
) -> Option<Result<(), error::RecvError>> {
// Load the version from the state
let state = shared.state.load();
let new_version = state.version();
if *version != new_version {
// Observe the new version and return
*... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | de81985762a242c77361a6ab9de198372ca85987 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/de81985762a242c77361a6ab9de198372ca85987/tokio/src/sync/watch.rs | 561 | 620 |
tokio-rs/tokio:tokio/src/sync/watch.rs:16 | }
}
impl<T> Sender<T> {
/// Sends a new value via the channel, notifying all receivers.
///
/// This method fails if the channel has been closed, which happens when
/// every receiver has been dropped.
pub fn send(&self, value: T) -> Result<(), error::SendError<T>> {
// This is pretty much ... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | de81985762a242c77361a6ab9de198372ca85987 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/de81985762a242c77361a6ab9de198372ca85987/tokio/src/sync/watch.rs | 601 | 660 |
tokio-rs/tokio:tokio/src/sync/watch.rs:18 | ///
/// This function panics when the invocation of the `modify` closure panics.
/// No receivers are notified when panicking. All changes of the watched
/// value applied by the closure before panicking will be visible in
/// subsequent calls to `borrow`.
///
/// # Examples
///
/// ```
... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | de81985762a242c77361a6ab9de198372ca85987 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/de81985762a242c77361a6ab9de198372ca85987/tokio/src/sync/watch.rs | 681 | 740 |
tokio-rs/tokio:tokio/src/sync/watch.rs:19 | // Acquire the write lock and update the value.
let mut lock = self.shared.value.write().unwrap();
// Update the value and catch possible panic inside func.
let result = panic::catch_unwind(panic::AssertUnwindSafe(|| modify(&mut lock)));
match result {
Ok... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | de81985762a242c77361a6ab9de198372ca85987 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/de81985762a242c77361a6ab9de198372ca85987/tokio/src/sync/watch.rs | 721 | 780 |
tokio-rs/tokio:tokio/src/sync/watch.rs:24 | // The CLOSED bit in the state tracks only whether the sender is
// dropped, so we do not need to unset it if this reopens the channel.
Receiver::from_shared(version, shared)
}
/// Returns the number of receivers that currently exist.
///
/// # Examples
///
/// ```
/// use t... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | de81985762a242c77361a6ab9de198372ca85987 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/de81985762a242c77361a6ab9de198372ca85987/tokio/src/sync/watch.rs | 921 | 980 |
tokio-rs/tokio:tokio/src/sync/watch.rs:25 | fn deref(&self) -> &T {
self.inner.deref()
}
}
#[cfg(all(test, loom))]
mod tests {
use futures::future::FutureExt;
use loom::thread;
// test for https://github.com/tokio-rs/tokio/issues/3168
#[test]
fn watch_spurious_wakeup() {
loom::model(|| {
let (send, mut recv) ... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | de81985762a242c77361a6ab9de198372ca85987 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/de81985762a242c77361a6ab9de198372ca85987/tokio/src/sync/watch.rs | 961 | 1,020 |
tokio-rs/tokio:tokio/src/sync/watch.rs:26 | recv.changed().now_or_never();
send_thread.join().unwrap();
});
}
#[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));
... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | de81985762a242c77361a6ab9de198372ca85987 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/de81985762a242c77361a6ab9de198372ca85987/tokio/src/sync/watch.rs | 1,001 | 1,046 |
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. Thi... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | 45f24f1b8684c45ee6e78689a6ede8f75629b5a5 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/45f24f1b8684c45ee6e78689a6ede8f75629b5a5/tokio/src/sync/watch.rs | 81 | 140 |
tokio-rs/tokio:tokio/src/sync/watch.rs:4 | /// The most recent value.
value: RwLock<T>,
/// The current version.
///
/// The lowest bit represents a "closed" state. The rest of the bits
/// represent the current version.
state: AtomicState,
/// Tracks the number of `Receiver` instances.
ref_count_rx: AtomicUsize,
/// Notif... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | 45f24f1b8684c45ee6e78689a6ede8f75629b5a5 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/45f24f1b8684c45ee6e78689a6ede8f75629b5a5/tokio/src/sync/watch.rs | 121 | 180 |
tokio-rs/tokio:tokio/src/sync/watch.rs:5 | pub struct RecvError(pub(super) ());
// ===== impl RecvError =====
impl fmt::Display for RecvError {
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(fmt, "channel closed")
}
}
impl std::error::Error for RecvError {}
}
use self::state::{AtomicState, Ver... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | 45f24f1b8684c45ee6e78689a6ede8f75629b5a5 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/45f24f1b8684c45ee6e78689a6ede8f75629b5a5/tokio/src/sync/watch.rs | 161 | 220 |
tokio-rs/tokio:tokio/src/sync/watch.rs:6 | }
}
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
}
}
... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | 45f24f1b8684c45ee6e78689a6ede8f75629b5a5 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/45f24f1b8684c45ee6e78689a6ede8f75629b5a5/tokio/src/sync/watch.rs | 201 | 260 |
tokio-rs/tokio:tokio/src/sync/watch.rs:7 | /// 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 | 45f24f1b8684c45ee6e78689a6ede8f75629b5a5 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/45f24f1b8684c45ee6e78689a6ede8f75629b5a5/tokio/src/sync/watch.rs | 241 | 300 |
tokio-rs/tokio:tokio/src/sync/watch.rs:8 | 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 | 45f24f1b8684c45ee6e78689a6ede8f75629b5a5 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/45f24f1b8684c45ee6e78689a6ede8f75629b5a5/tokio/src/sync/watch.rs | 281 | 340 |
tokio-rs/tokio:tokio/src/sync/watch.rs:12 | ///
/// ```
/// 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... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | 45f24f1b8684c45ee6e78689a6ede8f75629b5a5 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/45f24f1b8684c45ee6e78689a6ede8f75629b5a5/tokio/src/sync/watch.rs | 441 | 500 |
tokio-rs/tokio:tokio/src/sync/watch.rs:13 | /// let (tx, rx) = tokio::sync::watch::channel(true);
/// let rx2 = rx.clone();
/// assert!(rx.same_channel(&rx2));
///
/// let (tx3, rx3) = tokio::sync::watch::channel(true);
/// assert!(!rx3.same_channel(&rx2));
/// ```
pub fn same_channel(&self, other: &Self) -> bool {
Arc::ptr_eq... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | 45f24f1b8684c45ee6e78689a6ede8f75629b5a5 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/45f24f1b8684c45ee6e78689a6ede8f75629b5a5/tokio/src/sync/watch.rs | 481 | 540 |
tokio-rs/tokio:tokio/src/sync/watch.rs:14 | 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 | 45f24f1b8684c45ee6e78689a6ede8f75629b5a5 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/45f24f1b8684c45ee6e78689a6ede8f75629b5a5/tokio/src/sync/watch.rs | 521 | 580 |
tokio-rs/tokio:tokio/src/sync/watch.rs:18 | // 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(lock);
}
self.shared.notify_rx.not... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | 45f24f1b8684c45ee6e78689a6ede8f75629b5a5 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/45f24f1b8684c45ee6e78689a6ede8f75629b5a5/tokio/src/sync/watch.rs | 681 | 740 |
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 | c14566e9df5c71de8ac393dd40973362282ac157 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/c14566e9df5c71de8ac393dd40973362282ac157/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. Thi... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | c14566e9df5c71de8ac393dd40973362282ac157 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/c14566e9df5c71de8ac393dd40973362282ac157/tokio/src/sync/watch.rs | 81 | 140 |
tokio-rs/tokio:tokio/src/sync/watch.rs:4 | /// The current version.
///
/// The lowest bit represents a "closed" state. The rest of the bits
/// represent the current version.
state: AtomicState,
/// Tracks the number of `Receiver` instances.
ref_count_rx: AtomicUsize,
/// Notifies waiting receivers that the value changed.
noti... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | c14566e9df5c71de8ac393dd40973362282ac157 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/c14566e9df5c71de8ac393dd40973362282ac157/tokio/src/sync/watch.rs | 121 | 180 |
tokio-rs/tokio:tokio/src/sync/watch.rs:5 | // ===== impl RecvError =====
impl fmt::Display for RecvError {
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(fmt, "channel closed")
}
}
impl std::error::Error for RecvError {}
}
use self::state::{AtomicState, Version};
mod state {
use crate::loom::sy... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | c14566e9df5c71de8ac393dd40973362282ac157 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/c14566e9df5c71de8ac393dd40973362282ac157/tokio/src/sync/watch.rs | 161 | 220 |
tokio-rs/tokio:tokio/src/sync/watch.rs:7 | /// 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 | c14566e9df5c71de8ac393dd40973362282ac157 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/c14566e9df5c71de8ac393dd40973362282ac157/tokio/src/sync/watch.rs | 241 | 300 |
tokio-rs/tokio:tokio/src/sync/watch.rs:8 | 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_count_rx.fetch_add(1, Relaxed);
Self {... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | c14566e9df5c71de8ac393dd40973362282ac157 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/c14566e9df5c71de8ac393dd40973362282ac157/tokio/src/sync/watch.rs | 281 | 340 |
tokio-rs/tokio:tokio/src/sync/watch.rs:12 | /// let (tx, mut rx) = watch::channel("hello");
///
/// 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... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | c14566e9df5c71de8ac393dd40973362282ac157 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/c14566e9df5c71de8ac393dd40973362282ac157/tokio/src/sync/watch.rs | 441 | 500 |
tokio-rs/tokio:tokio/src/sync/watch.rs:13 | /// ```
pub fn same_channel(&self, other: &Self) -> bool {
Arc::ptr_eq(&self.shared, &other.shared)
}
cfg_process_driver! {
pub(crate) fn try_has_changed(&mut self) -> Option<Result<(), error::RecvError>> {
maybe_changed(&self.shared, &mut self.version)
}
}
}
fn may... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | c14566e9df5c71de8ac393dd40973362282ac157 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/c14566e9df5c71de8ac393dd40973362282ac157/tokio/src/sync/watch.rs | 481 | 540 |
tokio-rs/tokio:tokio/src/sync/watch.rs:14 | }
}
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 | c14566e9df5c71de8ac393dd40973362282ac157 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/c14566e9df5c71de8ac393dd40973362282ac157/tokio/src/sync/watch.rs | 521 | 580 |
tokio-rs/tokio:tokio/src/sync/watch.rs:17 | ///
/// assert!(!state_rx.has_changed().unwrap());
/// assert!(!state_tx.send_if_modified(inc_counter_if_odd));
/// assert!(!state_rx.has_changed().unwrap());
/// assert_eq!(state_rx.borrow_and_update().counter, 2);
/// ```
pub fn send_if_modified<F>(&self, modify: F) -> bool
where
F... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | c14566e9df5c71de8ac393dd40973362282ac157 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/c14566e9df5c71de8ac393dd40973362282ac157/tokio/src/sync/watch.rs | 641 | 700 |
tokio-rs/tokio:tokio/src/sync/watch.rs:18 | drop(lock);
}
self.shared.notify_rx.notify_waiters();
true
}
/// Sends a new value via the channel, notifying all receivers and returning
/// the previous value in the channel.
///
/// This can be useful for reusing the buffers inside a watched value.
/// Additionally,... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | c14566e9df5c71de8ac393dd40973362282ac157 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/c14566e9df5c71de8ac393dd40973362282ac157/tokio/src/sync/watch.rs | 681 | 740 |
tokio-rs/tokio:tokio/src/sync/watch.rs:22 | /// rx.changed().await.unwrap();
/// assert_eq!(100, *rx.borrow());
/// }
/// ```
pub fn subscribe(&self) -> Receiver<T> {
let shared = self.shared.clone();
let version = shared.state.load().version();
// The CLOSED bit in the state tracks only whether the sender is
... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | c14566e9df5c71de8ac393dd40973362282ac157 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/c14566e9df5c71de8ac393dd40973362282ac157/tokio/src/sync/watch.rs | 841 | 900 |
tokio-rs/tokio:tokio/src/sync/watch.rs:23 | }
}
// ===== impl Ref =====
impl<T> ops::Deref for Ref<'_, T> {
type Target = T;
fn deref(&self) -> &T {
self.inner.deref()
}
}
#[cfg(all(test, loom))]
mod tests {
use futures::future::FutureExt;
use loom::thread;
// test for https://github.com/tokio-rs/tokio/issues/3168
#[test]... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | c14566e9df5c71de8ac393dd40973362282ac157 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/c14566e9df5c71de8ac393dd40973362282ac157/tokio/src/sync/watch.rs | 881 | 940 |
tokio-rs/tokio:tokio/src/sync/watch.rs:14 | }
}
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 | 3652f71ade9caa07378ac0225a198e83207f4213 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/3652f71ade9caa07378ac0225a198e83207f4213/tokio/src/sync/watch.rs | 521 | 580 |
tokio-rs/tokio:tokio/src/sync/watch.rs:15 | ///
/// # Examples
///
/// ```
/// use tokio::sync::watch;
///
/// struct State {
/// counter: usize,
/// }
/// let (state_tx, state_rx) = watch::channel(State { counter: 0 });
/// state_tx.send_modify(|state| state.counter += 1);
/// assert_eq!(state_rx.borrow().counter,... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | 3652f71ade9caa07378ac0225a198e83207f4213 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/3652f71ade9caa07378ac0225a198e83207f4213/tokio/src/sync/watch.rs | 561 | 620 |
tokio-rs/tokio:tokio/src/sync/watch.rs:17 | /// ```
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 | 3652f71ade9caa07378ac0225a198e83207f4213 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/3652f71ade9caa07378ac0225a198e83207f4213/tokio/src/sync/watch.rs | 641 | 700 |
tokio-rs/tokio:tokio/src/sync/watch.rs:20 | pub fn subscribe(&self) -> Receiver<T> {
let shared = self.shared.clone();
let version = shared.state.load().version();
// The CLOSED bit in the state tracks only whether the sender is
// dropped, so we do not need to unset it if this reopens the channel.
Receiver::from_shared(v... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | 3652f71ade9caa07378ac0225a198e83207f4213 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/3652f71ade9caa07378ac0225a198e83207f4213/tokio/src/sync/watch.rs | 761 | 820 |
tokio-rs/tokio:tokio/src/sync/watch.rs:21 | impl<T> ops::Deref for Ref<'_, T> {
type Target = T;
fn deref(&self) -> &T {
self.inner.deref()
}
}
#[cfg(all(test, loom))]
mod tests {
use futures::future::FutureExt;
use loom::thread;
// test for https://github.com/tokio-rs/tokio/issues/3168
#[test]
fn watch_spurious_wakeup(... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | 3652f71ade9caa07378ac0225a198e83207f4213 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/3652f71ade9caa07378ac0225a198e83207f4213/tokio/src/sync/watch.rs | 801 | 860 |
tokio-rs/tokio:tokio/src/sync/watch.rs:22 | send.send(2).unwrap();
});
recv.changed().now_or_never();
send_thread.join().unwrap();
});
}
#[test]
fn watch_borrow() {
loom::model(|| {
let (send, mut recv) = crate::sync::watch::channel(0i32);
assert!(send.borrow().eq(&0));
... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | 3652f71ade9caa07378ac0225a198e83207f4213 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/3652f71ade9caa07378ac0225a198e83207f4213/tokio/src/sync/watch.rs | 841 | 889 |
tokio-rs/tokio:tokio/src/sync/watch.rs:4 | /// The current version.
///
/// The lowest bit represents a "closed" state. The rest of the bits
/// represent the current version.
state: AtomicState,
/// Tracks the number of `Receiver` instances.
ref_count_rx: AtomicUsize,
/// Notifies waiting receivers that the value changed.
noti... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | a8b75dbdf4360c9fd7fb874022169f0c00d38c4a | github | async-runtime | https://github.com/tokio-rs/tokio/blob/a8b75dbdf4360c9fd7fb874022169f0c00d38c4a/tokio/src/sync/watch.rs | 121 | 180 |
tokio-rs/tokio:tokio/src/sync/watch.rs:12 | /// let (tx, mut rx) = watch::channel("hello");
///
/// 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... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | 067ddff0635fe3b7edc9831167f7804de9891cf8 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/067ddff0635fe3b7edc9831167f7804de9891cf8/tokio/src/sync/watch.rs | 441 | 500 |
tokio-rs/tokio:tokio/src/sync/watch.rs:13 | // 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 | 067ddff0635fe3b7edc9831167f7804de9891cf8 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/067ddff0635fe3b7edc9831167f7804de9891cf8/tokio/src/sync/watch.rs | 481 | 540 |
tokio-rs/tokio:tokio/src/sync/watch.rs:14 | ///
/// This method fails if the channel has been closed, which happens when
/// every receiver has been dropped.
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.receiver_co... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | 067ddff0635fe3b7edc9831167f7804de9891cf8 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/067ddff0635fe3b7edc9831167f7804de9891cf8/tokio/src/sync/watch.rs | 521 | 580 |
tokio-rs/tokio:tokio/src/sync/watch.rs:15 | {
{
// Acquire the write lock and update the value.
let mut lock = self.shared.value.write().unwrap();
// Update the value and catch possible panic inside func.
let result = panic::catch_unwind(panic::AssertUnwindSafe(|| {
func(&mut lock);
... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | 067ddff0635fe3b7edc9831167f7804de9891cf8 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/067ddff0635fe3b7edc9831167f7804de9891cf8/tokio/src/sync/watch.rs | 561 | 620 |
tokio-rs/tokio:tokio/src/sync/watch.rs:20 | /// #[tokio::main]
/// async fn main() {
/// let (tx, rx1) = watch::channel("hello");
///
/// assert_eq!(1, tx.receiver_count());
///
/// let mut _rx2 = rx1.clone();
///
/// assert_eq!(2, tx.receiver_count());
/// }
/// ```
pub fn receiver_count(&self) -> usiz... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | 067ddff0635fe3b7edc9831167f7804de9891cf8 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/067ddff0635fe3b7edc9831167f7804de9891cf8/tokio/src/sync/watch.rs | 761 | 820 |
tokio-rs/tokio:tokio/src/sync/watch.rs:21 | 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 | 067ddff0635fe3b7edc9831167f7804de9891cf8 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/067ddff0635fe3b7edc9831167f7804de9891cf8/tokio/src/sync/watch.rs | 801 | 860 |
tokio-rs/tokio:tokio/src/sync/watch.rs:22 | send.send(1).unwrap();
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_threa... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | 067ddff0635fe3b7edc9831167f7804de9891cf8 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/067ddff0635fe3b7edc9831167f7804de9891cf8/tokio/src/sync/watch.rs | 841 | 873 |
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 | cec1bc151e30b7afe26c44e041c56cc826fda938 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/cec1bc151e30b7afe26c44e041c56cc826fda938/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. Thi... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | cec1bc151e30b7afe26c44e041c56cc826fda938 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/cec1bc151e30b7afe26c44e041c56cc826fda938/tokio/src/sync/watch.rs | 81 | 140 |
tokio-rs/tokio:tokio/src/sync/watch.rs:4 | /// The current version.
///
/// The lowest bit represents a "closed" state. The rest of the bits
/// represent the current version.
state: AtomicState,
/// Tracks the number of `Receiver` instances.
ref_count_rx: AtomicUsize,
/// Notifies waiting receivers that the value changed.
noti... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | cec1bc151e30b7afe26c44e041c56cc826fda938 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/cec1bc151e30b7afe26c44e041c56cc826fda938/tokio/src/sync/watch.rs | 121 | 180 |
tokio-rs/tokio:tokio/src/sync/watch.rs:5 | impl fmt::Display for RecvError {
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(fmt, "channel closed")
}
}
impl std::error::Error for RecvError {}
}
use self::state::{AtomicState, Version};
mod state {
use crate::loom::sync::atomic::AtomicUsize;
use cr... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | cec1bc151e30b7afe26c44e041c56cc826fda938 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/cec1bc151e30b7afe26c44e041c56cc826fda938/tokio/src/sync/watch.rs | 161 | 220 |
tokio-rs/tokio:tokio/src/sync/watch.rs:6 | 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 | cec1bc151e30b7afe26c44e041c56cc826fda938 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/cec1bc151e30b7afe26c44e041c56cc826fda938/tokio/src/sync/watch.rs | 201 | 260 |
tokio-rs/tokio:tokio/src/sync/watch.rs:7 | /// 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>> {
/// let (tx, mut rx) = watch::channel("hello");
///
/// tokio::spawn(a... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | cec1bc151e30b7afe26c44e041c56cc826fda938 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/cec1bc151e30b7afe26c44e041c56cc826fda938/tokio/src/sync/watch.rs | 241 | 300 |
tokio-rs/tokio:tokio/src/sync/watch.rs:12 | ///
/// 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 | cec1bc151e30b7afe26c44e041c56cc826fda938 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/cec1bc151e30b7afe26c44e041c56cc826fda938/tokio/src/sync/watch.rs | 441 | 500 |
tokio-rs/tokio:tokio/src/sync/watch.rs:13 | 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 | cec1bc151e30b7afe26c44e041c56cc826fda938 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/cec1bc151e30b7afe26c44e041c56cc826fda938/tokio/src/sync/watch.rs | 481 | 540 |
tokio-rs/tokio:tokio/src/sync/watch.rs:14 | /// This method fails if the channel has been closed, which happens when
/// every receiver has been dropped.
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.receiver_count() {
... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | cec1bc151e30b7afe26c44e041c56cc826fda938 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/cec1bc151e30b7afe26c44e041c56cc826fda938/tokio/src/sync/watch.rs | 521 | 580 |
tokio-rs/tokio:tokio/src/sync/watch.rs:15 | // value they are currently looking at.
drop(lock);
old
};
// Notify all watchers
self.shared.notify_rx.notify_waiters();
old
}
/// Returns a reference to the most recently sent value
///
/// Outstanding borrows hold a read lock. This means tha... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | cec1bc151e30b7afe26c44e041c56cc826fda938 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/cec1bc151e30b7afe26c44e041c56cc826fda938/tokio/src/sync/watch.rs | 561 | 620 |
tokio-rs/tokio:tokio/src/sync/watch.rs:19 | ///
/// #[tokio::main]
/// async fn main() {
/// let (tx, rx1) = watch::channel("hello");
///
/// assert_eq!(1, tx.receiver_count());
///
/// let mut _rx2 = rx1.clone();
///
/// assert_eq!(2, tx.receiver_count());
/// }
/// ```
pub fn receiver_count(&self)... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | cec1bc151e30b7afe26c44e041c56cc826fda938 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/cec1bc151e30b7afe26c44e041c56cc826fda938/tokio/src/sync/watch.rs | 721 | 780 |
tokio-rs/tokio:tokio/src/sync/watch.rs:20 | #[test]
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... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | cec1bc151e30b7afe26c44e041c56cc826fda938 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/cec1bc151e30b7afe26c44e041c56cc826fda938/tokio/src/sync/watch.rs | 761 | 820 |
tokio-rs/tokio:tokio/src/sync/watch.rs:21 | assert!(recv.borrow().eq(&0));
send.send(1).unwrap();
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... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | cec1bc151e30b7afe26c44e041c56cc826fda938 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/cec1bc151e30b7afe26c44e041c56cc826fda938/tokio/src/sync/watch.rs | 801 | 834 |
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 | 12dd06336d2af8c2d735d4d9e3dc0454ad7942a0 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/12dd06336d2af8c2d735d4d9e3dc0454ad7942a0/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. Thi... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | 12dd06336d2af8c2d735d4d9e3dc0454ad7942a0 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/12dd06336d2af8c2d735d4d9e3dc0454ad7942a0/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 | 12dd06336d2af8c2d735d4d9e3dc0454ad7942a0 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/12dd06336d2af8c2d735d4d9e3dc0454ad7942a0/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.
///
/// The CLOSED bit tracks whet... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | 12dd06336d2af8c2d735d4d9e3dc0454ad7942a0 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/12dd06336d2af8c2d735d4d9e3dc0454ad7942a0/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 | 12dd06336d2af8c2d735d4d9e3dc0454ad7942a0 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/12dd06336d2af8c2d735d4d9e3dc0454ad7942a0/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: AtomicUsize::... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | 12dd06336d2af8c2d735d4d9e3dc0454ad7942a0 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/12dd06336d2af8c2d735d4d9e3dc0454ad7942a0/tokio/src/sync/watch.rs | 241 | 300 |
tokio-rs/tokio:tokio/src/sync/watch.rs:11 | /// ```
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.
... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | 12dd06336d2af8c2d735d4d9e3dc0454ad7942a0 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/12dd06336d2af8c2d735d4d9e3dc0454ad7942a0/tokio/src/sync/watch.rs | 401 | 460 |
tokio-rs/tokio:tokio/src/sync/watch.rs:12 | 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, shared)
}
}
impl<T> Drop for Receiver<T> {
fn drop(&mut self) {
// N... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | 12dd06336d2af8c2d735d4d9e3dc0454ad7942a0 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/12dd06336d2af8c2d735d4d9e3dc0454ad7942a0/tokio/src/sync/watch.rs | 441 | 500 |
tokio-rs/tokio:tokio/src/sync/watch.rs:13 | /// Sends a new value via the channel, notifying all receivers and returning
/// 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.
///
/// # ... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | 12dd06336d2af8c2d735d4d9e3dc0454ad7942a0 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/12dd06336d2af8c2d735d4d9e3dc0454ad7942a0/tokio/src/sync/watch.rs | 481 | 540 |
tokio-rs/tokio:tokio/src/sync/watch.rs:17 | ///
/// tokio::spawn(async move {
/// // by spawning and sleeping, the message is sent after `main`
/// // hits the call to `changed`.
/// # if false {
/// tokio::time::sleep(Duration::from_millis(10)).await;
/// # }
/// tx.send(100).unwrap... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | 12dd06336d2af8c2d735d4d9e3dc0454ad7942a0 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/12dd06336d2af8c2d735d4d9e3dc0454ad7942a0/tokio/src/sync/watch.rs | 641 | 700 |
tokio-rs/tokio:tokio/src/sync/watch.rs:18 | /// ```
pub fn receiver_count(&self) -> usize {
self.shared.ref_count_rx.load(Relaxed)
}
}
impl<T> Drop for Sender<T> {
fn drop(&mut self) {
self.shared.state.set_closed();
self.shared.notify_rx.notify_waiters();
}
}
// ===== impl Ref =====
impl<T> ops::Deref for Ref<'_, T> {
... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | 12dd06336d2af8c2d735d4d9e3dc0454ad7942a0 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/12dd06336d2af8c2d735d4d9e3dc0454ad7942a0/tokio/src/sync/watch.rs | 681 | 740 |
tokio-rs/tokio:tokio/src/sync/watch.rs:19 | recv.changed().now_or_never();
let send = send_thread.join().unwrap();
let recv_thread = thread::spawn(move || {
recv.changed().now_or_never();
recv.changed().now_or_never();
recv
});
send.send(3).unwrap();
le... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | 12dd06336d2af8c2d735d4d9e3dc0454ad7942a0 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/12dd06336d2af8c2d735d4d9e3dc0454ad7942a0/tokio/src/sync/watch.rs | 721 | 780 |
tokio-rs/tokio:tokio/src/sync/watch.rs:20 | let send = send_thread.join().unwrap();
let recv_thread = thread::spawn(move || {
recv.changed().now_or_never();
recv.changed().now_or_never();
recv
});
send.send(3).unwrap();
let recv = recv_thread.join().unwrap();
... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | 12dd06336d2af8c2d735d4d9e3dc0454ad7942a0 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/12dd06336d2af8c2d735d4d9e3dc0454ad7942a0/tokio/src/sync/watch.rs | 761 | 783 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.