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:7 | /// }
/// ```
pub async fn changed(&mut self) -> Result<(), error::RecvError> {
// 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 = s... | 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 | 241 | 300 |
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 | d0ebb4154748166a4ba07baa4b424a1c45efd219 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/d0ebb4154748166a4ba07baa4b424a1c45efd219/tokio/src/sync/watch.rs | 361 | 392 |
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 | 7d7b79e1d53cacc24fb6c28ea67b25c7261e21de | github | async-runtime | https://github.com/tokio-rs/tokio/blob/7d7b79e1d53cacc24fb6c28ea67b25c7261e21de/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> {
use std::future::Future;
use std::pin::Pin;
use std::task::Poll;
// In order to avoid a race condition, we first request a notification,
// **then** check the current value's version. If a ne... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | 7d7b79e1d53cacc24fb6c28ea67b25c7261e21de | github | async-runtime | https://github.com/tokio-rs/tokio/blob/7d7b79e1d53cacc24fb6c28ea67b25c7261e21de/tokio/src/sync/watch.rs | 241 | 300 |
tokio-rs/tokio:tokio/src/sync/watch.rs:8 | 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(Err(error::RecvError(())));
}
None
... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | 7d7b79e1d53cacc24fb6c28ea67b25c7261e21de | github | async-runtime | https://github.com/tokio-rs/tokio/blob/7d7b79e1d53cacc24fb6c28ea67b25c7261e21de/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 | 7d7b79e1d53cacc24fb6c28ea67b25c7261e21de | github | async-runtime | https://github.com/tokio-rs/tokio/blob/7d7b79e1d53cacc24fb6c28ea67b25c7261e21de/tokio/src/sync/watch.rs | 321 | 380 |
tokio-rs/tokio:tokio/src/sync/watch.rs:10 | /// # Examples
///
/// ```
/// use tokio::sync::watch;
///
/// #[tokio::main]
/// async fn main() {
/// let (tx, rx) = watch::channel("hello");
///
/// tokio::spawn(async move {
/// // use `rx`
/// drop(rx);
/// });
///
/// // Waits... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | 7d7b79e1d53cacc24fb6c28ea67b25c7261e21de | github | async-runtime | https://github.com/tokio-rs/tokio/blob/7d7b79e1d53cacc24fb6c28ea67b25c7261e21de/tokio/src/sync/watch.rs | 361 | 407 |
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 | 2bc9a4815259c8ff4daa5e24f128ec826970d17f | github | async-runtime | https://github.com/tokio-rs/tokio/blob/2bc9a4815259c8ff4daa5e24f128ec826970d17f/tokio/src/sync/watch.rs | 41 | 100 |
tokio-rs/tokio:tokio/src/sync/watch.rs:3 | ///
/// 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: RwLockReadGuard<'a, T>,
}
#[derive(Debug)]
struct Share... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | 2bc9a4815259c8ff4daa5e24f128ec826970d17f | github | async-runtime | https://github.com/tokio-rs/tokio/blob/2bc9a4815259c8ff4daa5e24f128ec826970d17f/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 receiving a ch... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | 2bc9a4815259c8ff4daa5e24f128ec826970d17f | github | async-runtime | https://github.com/tokio-rs/tokio/blob/2bc9a4815259c8ff4daa5e24f128ec826970d17f/tokio/src/sync/watch.rs | 121 | 180 |
tokio-rs/tokio:tokio/src/sync/watch.rs:5 | /// let (tx, mut rx) = watch::channel("hello");
///
/// tokio::spawn(async move {
/// while rx.changed().await.is_ok() {
/// println!("received = {:?}", *rx.borrow());
/// }
/// });
///
/// tx.send("world")?;
/// # Ok(())
/// # }
/// ```
///
/// [`Sender`]: struct@Sender
/// ... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | 2bc9a4815259c8ff4daa5e24f128ec826970d17f | github | async-runtime | https://github.com/tokio-rs/tokio/blob/2bc9a4815259c8ff4daa5e24f128ec826970d17f/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 }
}
/// Wait for a... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | 2bc9a4815259c8ff4daa5e24f128ec826970d17f | github | async-runtime | https://github.com/tokio-rs/tokio/blob/2bc9a4815259c8ff4daa5e24f128ec826970d17f/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> {
use std::future::Future;
use std::pin::Pin;
use std::task::Poll;
// In order to avoid a race condition, we first request a notification,
// **then** check the current value's version. If a new version ... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | 2bc9a4815259c8ff4daa5e24f128ec826970d17f | github | async-runtime | https://github.com/tokio-rs/tokio/blob/2bc9a4815259c8ff4daa5e24f128ec826970d17f/tokio/src/sync/watch.rs | 241 | 300 |
tokio-rs/tokio:tokio/src/sync/watch.rs:8 | 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(Err(error::RecvError(())));
}
None
}
impl<T> Clone for Receiver<T> {
f... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | 2bc9a4815259c8ff4daa5e24f128ec826970d17f | github | async-runtime | https://github.com/tokio-rs/tokio/blob/2bc9a4815259c8ff4daa5e24f128ec826970d17f/tokio/src/sync/watch.rs | 281 | 340 |
tokio-rs/tokio:tokio/src/sync/watch.rs:9 | /// 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) {
return Err(error::Sen... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | 2bc9a4815259c8ff4daa5e24f128ec826970d17f | github | async-runtime | https://github.com/tokio-rs/tokio/blob/2bc9a4815259c8ff4daa5e24f128ec826970d17f/tokio/src/sync/watch.rs | 321 | 380 |
tokio-rs/tokio:tokio/src/sync/watch.rs:10 | /// }
/// ```
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));
}
}
impl<T> Drop for Send... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | 2bc9a4815259c8ff4daa5e24f128ec826970d17f | github | async-runtime | https://github.com/tokio-rs/tokio/blob/2bc9a4815259c8ff4daa5e24f128ec826970d17f/tokio/src/sync/watch.rs | 361 | 390 |
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 | 5a1a6dc90c6d5a7eb5f31ae215f9ec383d6767aa | github | async-runtime | https://github.com/tokio-rs/tokio/blob/5a1a6dc90c6d5a7eb5f31ae215f9ec383d6767aa/tokio/src/sync/watch.rs | 41 | 100 |
tokio-rs/tokio:tokio/src/sync/watch.rs:3 | pub struct Sender<T> {
shared: Weak<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 possible.
#[derive(D... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | 5a1a6dc90c6d5a7eb5f31ae215f9ec383d6767aa | github | async-runtime | https://github.com/tokio-rs/tokio/blob/5a1a6dc90c6d5a7eb5f31ae215f9ec383d6767aa/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.
version: AtomicUsize,
/// All watchers
watchers: Mutex<Watchers>,
/// Task to notify when all watchers drop
cancel: AtomicWaker,
}
type Watchers = FnvHas... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | 5a1a6dc90c6d5a7eb5f31ae215f9ec383d6767aa | github | async-runtime | https://github.com/tokio-rs/tokio/blob/5a1a6dc90c6d5a7eb5f31ae215f9ec383d6767aa/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 let Some(value) = Some(rx.recv().await) {
/// println!("received = {:?}", value);
/// }
/// });
///
/// tx.send("world"... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | 5a1a6dc90c6d5a7eb5f31ae215f9ec383d6767aa | github | async-runtime | https://github.com/tokio-rs/tokio/blob/5a1a6dc90c6d5a7eb5f31ae215f9ec383d6767aa/tokio/src/sync/watch.rs | 161 | 220 |
tokio-rs/tokio:tokio/src/sync/watch.rs:6 | (tx, rx)
}
impl<T> Receiver<T> {
/// Returns a reference to the most recently sent value
///
/// 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.
///
/// # Ex... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | 5a1a6dc90c6d5a7eb5f31ae215f9ec383d6767aa | github | async-runtime | https://github.com/tokio-rs/tokio/blob/5a1a6dc90c6d5a7eb5f31ae215f9ec383d6767aa/tokio/src/sync/watch.rs | 201 | 260 |
tokio-rs/tokio:tokio/src/sync/watch.rs:7 | let inner = self.shared.value.read().unwrap();
return Ready(Ref { inner });
}
Pending
}
}
impl<T: Clone> Receiver<T> {
/// Attempts to clone the latest value sent via the channel.
///
/// If this is the first time the function is called on a `Receiver`
/// instance, th... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | 5a1a6dc90c6d5a7eb5f31ae215f9ec383d6767aa | github | async-runtime | https://github.com/tokio-rs/tokio/blob/5a1a6dc90c6d5a7eb5f31ae215f9ec383d6767aa/tokio/src/sync/watch.rs | 241 | 300 |
tokio-rs/tokio:tokio/src/sync/watch.rs:8 | /// assert_eq!(v, "goodbye");
/// }
/// ```
pub async fn recv(&mut self) -> T {
poll_fn(|cx| {
let v_ref = ready!(self.poll_recv_ref(cx));
Poll::Ready((*v_ref).clone())
})
.await
}
}
#[cfg(feature = "stream")]
impl<T: Clone> crate::stream::Stream for ... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | 5a1a6dc90c6d5a7eb5f31ae215f9ec383d6767aa | github | async-runtime | https://github.com/tokio-rs/tokio/blob/5a1a6dc90c6d5a7eb5f31ae215f9ec383d6767aa/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>> {
let shared = match self.shared.upgrade() {
Some(shared) => shared,
// All `Watch` handles have been canceled
None => r... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | 5a1a6dc90c6d5a7eb5f31ae215f9ec383d6767aa | github | async-runtime | https://github.com/tokio-rs/tokio/blob/5a1a6dc90c6d5a7eb5f31ae215f9ec383d6767aa/tokio/src/sync/watch.rs | 321 | 380 |
tokio-rs/tokio:tokio/src/sync/watch.rs:10 | }
}
}
/// Notifies all watchers of a change
fn notify_all<T>(shared: &Shared<T>) {
let watchers = shared.watchers.lock().unwrap();
for watcher in watchers.iter() {
// Notify the task
watcher.waker.wake();
}
}
impl<T> Drop for Sender<T> {
fn drop(&mut self) {
if let Some(sh... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | 5a1a6dc90c6d5a7eb5f31ae215f9ec383d6767aa | github | async-runtime | https://github.com/tokio-rs/tokio/blob/5a1a6dc90c6d5a7eb5f31ae215f9ec383d6767aa/tokio/src/sync/watch.rs | 361 | 420 |
tokio-rs/tokio:tokio/src/sync/watch.rs:11 | // ===== impl Watcher =====
impl Watcher {
fn new_version(version: usize) -> Self {
Watcher(Arc::new(WatchInner {
version: AtomicUsize::new(version),
waker: AtomicWaker::new(),
}))
}
}
impl std::cmp::PartialEq for Watcher {
fn eq(&self, other: &Watcher) -> bool {
... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | 5a1a6dc90c6d5a7eb5f31ae215f9ec383d6767aa | github | async-runtime | https://github.com/tokio-rs/tokio/blob/5a1a6dc90c6d5a7eb5f31ae215f9ec383d6767aa/tokio/src/sync/watch.rs | 401 | 433 |
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.
version: AtomicUsize,
/// All watchers
watchers: Mutex<Watchers>,
/// Task to notify when all watchers drop
cancel: AtomicWaker,
}
type Watchers = FnvHas... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | 6dcce1901a53f099bf10f242943b44010f264171 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/6dcce1901a53f099bf10f242943b44010f264171/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 let Some(value) = rx.recv().await {
/// println!("received = {:?}", value);
/// }
/// });
///
/// tx.broadcast("world")... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | 6dcce1901a53f099bf10f242943b44010f264171 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/6dcce1901a53f099bf10f242943b44010f264171/tokio/src/sync/watch.rs | 161 | 220 |
tokio-rs/tokio:tokio/src/sync/watch.rs:6 | (tx, rx)
}
impl<T> Receiver<T> {
/// Returns a reference to the most recently sent value
///
/// 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.
///
/// # Ex... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | 6dcce1901a53f099bf10f242943b44010f264171 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/6dcce1901a53f099bf10f242943b44010f264171/tokio/src/sync/watch.rs | 201 | 260 |
tokio-rs/tokio:tokio/src/sync/watch.rs:7 | return Ready(None);
}
Pending
}
}
impl<T: Clone> Receiver<T> {
/// Attempts to clone the latest value sent via the channel.
///
/// If this is the first time the function is called on a `Receiver`
/// instance, then the function completes immediately with the **current**
/// va... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | 6dcce1901a53f099bf10f242943b44010f264171 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/6dcce1901a53f099bf10f242943b44010f264171/tokio/src/sync/watch.rs | 241 | 300 |
tokio-rs/tokio:tokio/src/sync/watch.rs:8 | /// ```
pub async fn recv(&mut self) -> Option<T> {
poll_fn(|cx| {
let v_ref = ready!(self.poll_recv_ref(cx));
Poll::Ready(v_ref.map(|v_ref| (*v_ref).clone()))
})
.await
}
}
#[cfg(feature = "stream")]
impl<T: Clone> crate::stream::Stream for Receiver<T> {
typ... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | 6dcce1901a53f099bf10f242943b44010f264171 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/6dcce1901a53f099bf10f242943b44010f264171/tokio/src/sync/watch.rs | 281 | 340 |
tokio-rs/tokio:tokio/src/sync/watch.rs:9 | /// Broadcasts a new value via the channel, notifying all receivers.
pub fn broadcast(&self, value: T) -> Result<(), error::SendError<T>> {
let shared = match self.shared.upgrade() {
Some(shared) => shared,
// All `Watch` handles have been canceled
None => return Err(erro... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | 6dcce1901a53f099bf10f242943b44010f264171 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/6dcce1901a53f099bf10f242943b44010f264171/tokio/src/sync/watch.rs | 321 | 380 |
tokio-rs/tokio:tokio/src/sync/watch.rs:10 | }
/// Notifies all watchers of a change
fn notify_all<T>(shared: &Shared<T>) {
let watchers = shared.watchers.lock().unwrap();
for watcher in watchers.iter() {
// Notify the task
watcher.waker.wake();
}
}
impl<T> Drop for Sender<T> {
fn drop(&mut self) {
if let Some(shared) = ... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | 6dcce1901a53f099bf10f242943b44010f264171 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/6dcce1901a53f099bf10f242943b44010f264171/tokio/src/sync/watch.rs | 361 | 420 |
tokio-rs/tokio:tokio/src/sync/watch.rs:11 | impl Watcher {
fn new_version(version: usize) -> Self {
Watcher(Arc::new(WatchInner {
version: AtomicUsize::new(version),
waker: AtomicWaker::new(),
}))
}
}
impl std::cmp::PartialEq for Watcher {
fn eq(&self, other: &Watcher) -> bool {
Arc::ptr_eq(&self.0, &o... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | 6dcce1901a53f099bf10f242943b44010f264171 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/6dcce1901a53f099bf10f242943b44010f264171/tokio/src/sync/watch.rs | 401 | 431 |
tokio-rs/tokio:tokio/src/sync/watch.rs:9 | /// Broadcasts a new value via the channel, notifying all receivers.
pub fn broadcast(&self, value: T) -> Result<(), error::SendError<T>> {
let shared = match self.shared.upgrade() {
Some(shared) => shared,
// All `Watch` handles have been canceled
None => return Err(erro... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | f39c15334e74b07a44efaa0f7201262e17e4f062 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/f39c15334e74b07a44efaa0f7201262e17e4f062/tokio/src/sync/watch.rs | 321 | 380 |
tokio-rs/tokio:tokio/src/sync/watch.rs:10 | }
}
/// Notifies all watchers of a change
fn notify_all<T>(shared: &Shared<T>) {
let watchers = shared.watchers.lock().unwrap();
for watcher in watchers.iter() {
// Notify the task
watcher.waker.wake();
}
}
impl<T> Drop for Sender<T> {
fn drop(&mut self) {
if let Some(shared) ... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | f39c15334e74b07a44efaa0f7201262e17e4f062 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/f39c15334e74b07a44efaa0f7201262e17e4f062/tokio/src/sync/watch.rs | 361 | 420 |
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 | 116a18b849e3dd9e5fcddc92a299e87ff855936a | github | async-runtime | https://github.com/tokio-rs/tokio/blob/116a18b849e3dd9e5fcddc92a299e87ff855936a/tokio/src/sync/watch.rs | 41 | 100 |
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.
version: AtomicUsize,
/// All watchers
watchers: Mutex<Watchers>,
/// Task to notify when all watchers drop
cancel: AtomicWaker,
}
type Watchers = FnvHas... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | 116a18b849e3dd9e5fcddc92a299e87ff855936a | github | async-runtime | https://github.com/tokio-rs/tokio/blob/116a18b849e3dd9e5fcddc92a299e87ff855936a/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 let Some(value) = rx.recv().await {
/// println!("received = {:?}", value);
/// }
/// });
///
/// tx.broadcast("world")... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | 116a18b849e3dd9e5fcddc92a299e87ff855936a | github | async-runtime | https://github.com/tokio-rs/tokio/blob/116a18b849e3dd9e5fcddc92a299e87ff855936a/tokio/src/sync/watch.rs | 161 | 220 |
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 | bcba4aaa5414eeb12b57e86a3abaf61425cef22b | github | async-runtime | https://github.com/tokio-rs/tokio/blob/bcba4aaa5414eeb12b57e86a3abaf61425cef22b/tokio/src/sync/watch.rs | 41 | 100 |
tokio-rs/tokio:tokio/src/sync/watch.rs:3 | }
/// Sends values to the associated [`Receiver`](struct.Receiver.html).
///
/// Instances are created by the [`channel`](fn.channel.html) function.
#[derive(Debug)]
pub struct Sender<T> {
shared: Weak<Shared<T>>,
}
/// Returns a reference to the inner value
///
/// Outstanding borrows hold a read lock on the inn... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | bcba4aaa5414eeb12b57e86a3abaf61425cef22b | github | async-runtime | https://github.com/tokio-rs/tokio/blob/bcba4aaa5414eeb12b57e86a3abaf61425cef22b/tokio/src/sync/watch.rs | 81 | 140 |
tokio-rs/tokio:tokio/src/sync/watch.rs:4 | }
#[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 current version.
version: AtomicUsize,
/// All watchers
watchers: Mutex<Watchers>,
... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | bcba4aaa5414eeb12b57e86a3abaf61425cef22b | github | async-runtime | https://github.com/tokio-rs/tokio/blob/bcba4aaa5414eeb12b57e86a3abaf61425cef22b/tokio/src/sync/watch.rs | 121 | 180 |
tokio-rs/tokio:tokio/src/sync/watch.rs:5 | ///
/// ```
/// 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 let Some(value) = rx.recv().await {
/// println!("received = {:?}", value);
/// }
/... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | bcba4aaa5414eeb12b57e86a3abaf61425cef22b | github | async-runtime | https://github.com/tokio-rs/tokio/blob/bcba4aaa5414eeb12b57e86a3abaf61425cef22b/tokio/src/sync/watch.rs | 161 | 220 |
tokio-rs/tokio:tokio/src/sync/watch.rs:6 | shared: Arc::downgrade(&shared),
};
let rx = Receiver {
shared,
inner,
id: INIT_ID,
ver: 0,
};
(tx, rx)
}
impl<T> Receiver<T> {
/// Returns a reference to the most recently sent value
///
/// Outstanding borrows hold a read lock. This means that long lived ... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | bcba4aaa5414eeb12b57e86a3abaf61425cef22b | github | async-runtime | https://github.com/tokio-rs/tokio/blob/bcba4aaa5414eeb12b57e86a3abaf61425cef22b/tokio/src/sync/watch.rs | 201 | 260 |
tokio-rs/tokio:tokio/src/sync/watch.rs:7 | let version = state & !CLOSED;
if version != self.ver {
let inner = self.shared.value.read().unwrap();
self.ver = version;
return Ready(Some(Ref { inner }));
}
if CLOSED == state & CLOSED {
// The `Store` handle has been dropped.
ret... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | bcba4aaa5414eeb12b57e86a3abaf61425cef22b | github | async-runtime | https://github.com/tokio-rs/tokio/blob/bcba4aaa5414eeb12b57e86a3abaf61425cef22b/tokio/src/sync/watch.rs | 241 | 300 |
tokio-rs/tokio:tokio/src/sync/watch.rs:8 | /// tokio::spawn(async move {
/// tx.broadcast("goodbye").unwrap();
/// });
///
/// // Waits for the new task to spawn and send the value.
/// let v = rx.recv().await.unwrap();
/// assert_eq!(v, "goodbye");
///
/// let v = rx.recv().await;
/// asse... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | bcba4aaa5414eeb12b57e86a3abaf61425cef22b | github | async-runtime | https://github.com/tokio-rs/tokio/blob/bcba4aaa5414eeb12b57e86a3abaf61425cef22b/tokio/src/sync/watch.rs | 281 | 340 |
tokio-rs/tokio:tokio/src/sync/watch.rs:9 | watchers.next_id += 1;
watchers.watchers.insert(id, inner.clone());
id
};
let ver = self.ver;
Receiver {
shared,
inner,
id,
ver,
}
}
}
impl<T> Drop for Receiver<T> {
fn drop(&mut self) {
let mut w... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | bcba4aaa5414eeb12b57e86a3abaf61425cef22b | github | async-runtime | https://github.com/tokio-rs/tokio/blob/bcba4aaa5414eeb12b57e86a3abaf61425cef22b/tokio/src/sync/watch.rs | 321 | 380 |
tokio-rs/tokio:tokio/src/sync/watch.rs:10 | };
// Replace the value
{
let mut lock = shared.value.write().unwrap();
*lock = value;
}
// Update the version. 2 is used so that the CLOSED bit is not set.
shared.version.fetch_add(2, SeqCst);
// Notify all watchers
notify_all(&*shared)... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | bcba4aaa5414eeb12b57e86a3abaf61425cef22b | github | async-runtime | https://github.com/tokio-rs/tokio/blob/bcba4aaa5414eeb12b57e86a3abaf61425cef22b/tokio/src/sync/watch.rs | 361 | 420 |
tokio-rs/tokio:tokio/src/sync/watch.rs:11 | for watcher in watchers.watchers.values() {
// Notify the task
watcher.waker.wake();
}
}
impl<T> Drop for Sender<T> {
fn drop(&mut self) {
if let Some(shared) = self.shared.upgrade() {
shared.version.fetch_or(CLOSED, SeqCst);
notify_all(&*shared);
}
}... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | bcba4aaa5414eeb12b57e86a3abaf61425cef22b | github | async-runtime | https://github.com/tokio-rs/tokio/blob/bcba4aaa5414eeb12b57e86a3abaf61425cef22b/tokio/src/sync/watch.rs | 401 | 433 |
tokio-rs/tokio:tokio/src/sync/watch.rs:7 | let version = state & !CLOSED;
if version != self.ver {
let inner = self.shared.value.read().unwrap();
self.ver = version;
return Ready(Some(Ref { inner }));
}
if CLOSED == state & CLOSED {
// The `Store` handle has been dropped.
ret... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | f9ddb93604a830d106475bd4c4cae436fafcc0da | github | async-runtime | https://github.com/tokio-rs/tokio/blob/f9ddb93604a830d106475bd4c4cae436fafcc0da/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 inner = Arc::new(WatchInner::new());
let shared = self.shared.clone();
let id = {
let mut watchers = shared.watchers.lock().unwrap();
let id = watchers.next_id;
watchers.next_id += 1;
... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | f9ddb93604a830d106475bd4c4cae436fafcc0da | github | async-runtime | https://github.com/tokio-rs/tokio/blob/f9ddb93604a830d106475bd4c4cae436fafcc0da/tokio/src/sync/watch.rs | 281 | 340 |
tokio-rs/tokio:tokio/src/sync/watch.rs:9 | impl<T> Sender<T> {
/// Broadcasts a new value via the channel, notifying all receivers.
pub fn broadcast(&self, value: T) -> Result<(), error::SendError<T>> {
let shared = match self.shared.upgrade() {
Some(shared) => shared,
// All `Watch` handles have been canceled
... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | f9ddb93604a830d106475bd4c4cae436fafcc0da | github | async-runtime | https://github.com/tokio-rs/tokio/blob/f9ddb93604a830d106475bd4c4cae436fafcc0da/tokio/src/sync/watch.rs | 321 | 380 |
tokio-rs/tokio:tokio/src/sync/watch.rs:10 | None => Ready(()),
}
}
}
/// Notifies all watchers of a change
fn notify_all<T>(shared: &Shared<T>) {
let watchers = shared.watchers.lock().unwrap();
for watcher in watchers.watchers.values() {
// Notify the task
watcher.waker.wake();
}
}
impl<T> Drop for Sender<T> {
fn dr... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | f9ddb93604a830d106475bd4c4cae436fafcc0da | github | async-runtime | https://github.com/tokio-rs/tokio/blob/f9ddb93604a830d106475bd4c4cae436fafcc0da/tokio/src/sync/watch.rs | 361 | 401 |
tokio-rs/tokio:tokio/src/sync/watch.rs:4 | }
#[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 current version.
version: AtomicUsize,
/// All watchers
watchers: Mutex<Watchers>,
... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | dcfa895b512e3ed522b81b18baf3e33fd78a600c | github | async-runtime | https://github.com/tokio-rs/tokio/blob/dcfa895b512e3ed522b81b18baf3e33fd78a600c/tokio/src/sync/watch.rs | 121 | 180 |
tokio-rs/tokio:tokio/src/sync/watch.rs:8 | impl<T> Clone for Receiver<T> {
fn clone(&self) -> Self {
let inner = Arc::new(WatchInner::new());
let shared = self.shared.clone();
let id = {
let mut watchers = shared.watchers.lock().unwrap();
let id = watchers.next_id;
watchers.next_id += 1;
... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | dcfa895b512e3ed522b81b18baf3e33fd78a600c | github | async-runtime | https://github.com/tokio-rs/tokio/blob/dcfa895b512e3ed522b81b18baf3e33fd78a600c/tokio/src/sync/watch.rs | 281 | 340 |
tokio-rs/tokio:tokio/src/sync/watch.rs:9 | impl<T> Sender<T> {
/// Broadcast a new value via the channel, notifying all receivers.
pub fn broadcast(&self, value: T) -> Result<(), error::SendError<T>> {
let shared = match self.shared.upgrade() {
Some(shared) => shared,
// All `Watch` handles have been canceled
... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | dcfa895b512e3ed522b81b18baf3e33fd78a600c | github | async-runtime | https://github.com/tokio-rs/tokio/blob/dcfa895b512e3ed522b81b18baf3e33fd78a600c/tokio/src/sync/watch.rs | 321 | 380 |
tokio-rs/tokio:tokio/src/sync/watch.rs:10 | None => Ready(()),
}
}
}
/// Notify all watchers of a change
fn notify_all<T>(shared: &Shared<T>) {
let watchers = shared.watchers.lock().unwrap();
for watcher in watchers.watchers.values() {
// Notify the task
watcher.waker.wake();
}
}
impl<T> Drop for Sender<T> {
fn drop... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | dcfa895b512e3ed522b81b18baf3e33fd78a600c | github | async-runtime | https://github.com/tokio-rs/tokio/blob/dcfa895b512e3ed522b81b18baf3e33fd78a600c/tokio/src/sync/watch.rs | 361 | 401 |
tokio-rs/tokio:tokio/src/sync/watch.rs:3 | }
/// Sends values to the associated [`Receiver`](struct.Receiver.html).
///
/// Instances are created by the [`channel`](fn.channel.html) function.
#[derive(Debug)]
pub struct Sender<T> {
shared: Weak<Shared<T>>,
}
/// Returns a reference to the inner value
///
/// Outstanding borrows hold a read lock on the inn... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | 4c645866ef4ea5b0ef8c7852281a09b2f96d969b | github | async-runtime | https://github.com/tokio-rs/tokio/blob/4c645866ef4ea5b0ef8c7852281a09b2f96d969b/tokio/src/sync/watch.rs | 81 | 140 |
tokio-rs/tokio:tokio/src/sync/watch.rs:7 | let version = state & !CLOSED;
if version != self.ver {
let inner = self.shared.value.read().unwrap();
self.ver = version;
return Ready(Some(Ref { inner }));
}
if CLOSED == state & CLOSED {
// The `Store` handle has been dropped.
ret... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | 4ddc4371709562d2bd1d0373f0555f7c31926e53 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/4ddc4371709562d2bd1d0373f0555f7c31926e53/tokio/src/sync/watch.rs | 241 | 300 |
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`]: struct.Sender.html
//! [`Receiver`]: s... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | 44f10fe47fc83884b61599de95b494d3405878cd | github | async-runtime | https://github.com/tokio-rs/tokio/blob/44f10fe47fc83884b61599de95b494d3405878cd/tokio/src/sync/watch.rs | 41 | 100 |
tokio-rs/tokio:tokio/src/sync/watch.rs:3 | /// Sends values to the associated [`Receiver`](struct.Receiver.html).
///
/// Instances are created by the [`channel`](fn.channel.html) function.
#[derive(Debug)]
pub struct Sender<T> {
shared: Weak<Shared<T>>,
}
/// Returns a reference to the inner value
///
/// Outstanding borrows hold a read lock on the inner ... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | 44f10fe47fc83884b61599de95b494d3405878cd | github | async-runtime | https://github.com/tokio-rs/tokio/blob/44f10fe47fc83884b61599de95b494d3405878cd/tokio/src/sync/watch.rs | 81 | 140 |
tokio-rs/tokio:tokio/src/sync/watch.rs:4 | #[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 current version.
version: AtomicUsize,
/// All watchers
watchers: Mutex<Watchers>,
... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | 44f10fe47fc83884b61599de95b494d3405878cd | github | async-runtime | https://github.com/tokio-rs/tokio/blob/44f10fe47fc83884b61599de95b494d3405878cd/tokio/src/sync/watch.rs | 121 | 180 |
tokio-rs/tokio:tokio/src/sync/watch.rs:5 | /// ```
/// 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 let Some(value) = rx.recv().await {
/// println!("received = {:?}", value);
/// }
/// ... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | 44f10fe47fc83884b61599de95b494d3405878cd | github | async-runtime | https://github.com/tokio-rs/tokio/blob/44f10fe47fc83884b61599de95b494d3405878cd/tokio/src/sync/watch.rs | 161 | 220 |
tokio-rs/tokio:tokio/src/sync/watch.rs:6 | };
let rx = Receiver {
shared,
inner,
id: INIT_ID,
ver: 0,
};
(tx, rx)
}
impl<T> Receiver<T> {
/// Returns a reference to the most recently sent value
///
/// Outstanding borrows hold a read lock. This means that long lived borrows
/// could cause the send ... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | 44f10fe47fc83884b61599de95b494d3405878cd | github | async-runtime | https://github.com/tokio-rs/tokio/blob/44f10fe47fc83884b61599de95b494d3405878cd/tokio/src/sync/watch.rs | 201 | 260 |
tokio-rs/tokio:tokio/src/sync/watch.rs:7 | if version != self.ver {
let inner = self.shared.value.read().unwrap();
self.ver = version;
return Ready(Some(Ref { inner }));
}
if CLOSED == state & CLOSED {
// The `Store` handle has been dropped.
return Ready(None);
}
Pend... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | 44f10fe47fc83884b61599de95b494d3405878cd | github | async-runtime | https://github.com/tokio-rs/tokio/blob/44f10fe47fc83884b61599de95b494d3405878cd/tokio/src/sync/watch.rs | 241 | 300 |
tokio-rs/tokio:tokio/src/sync/watch.rs:8 | fn clone(&self) -> Self {
let inner = Arc::new(WatchInner::new());
let shared = self.shared.clone();
let id = {
let mut watchers = shared.watchers.lock().unwrap();
let id = watchers.next_id;
watchers.next_id += 1;
watchers.watchers.insert(id, inn... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | 44f10fe47fc83884b61599de95b494d3405878cd | github | async-runtime | https://github.com/tokio-rs/tokio/blob/44f10fe47fc83884b61599de95b494d3405878cd/tokio/src/sync/watch.rs | 281 | 340 |
tokio-rs/tokio:tokio/src/sync/watch.rs:9 | impl<T> Sender<T> {
/// Broadcast a new value via the channel, notifying all receivers.
pub fn broadcast(&self, value: T) -> Result<(), error::SendError<T>> {
let shared = match self.shared.upgrade() {
Some(shared) => shared,
// All `Watch` handles have been canceled
... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | 44f10fe47fc83884b61599de95b494d3405878cd | github | async-runtime | https://github.com/tokio-rs/tokio/blob/44f10fe47fc83884b61599de95b494d3405878cd/tokio/src/sync/watch.rs | 321 | 380 |
tokio-rs/tokio:tokio/src/sync/watch.rs:10 | }
}
}
/// Notify all watchers of a change
fn notify_all<T>(shared: &Shared<T>) {
let watchers = shared.watchers.lock().unwrap();
for watcher in watchers.watchers.values() {
// Notify the task
watcher.waker.wake();
}
}
impl<T> Drop for Sender<T> {
fn drop(&mut self) {
if le... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | 44f10fe47fc83884b61599de95b494d3405878cd | github | async-runtime | https://github.com/tokio-rs/tokio/blob/44f10fe47fc83884b61599de95b494d3405878cd/tokio/src/sync/watch.rs | 361 | 400 |
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`]: struct.Sender.html
//! [`Receiver`]: s... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | 8a7e57786a5dca139f5b4261685e22991ded0859 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/8a7e57786a5dca139f5b4261685e22991ded0859/tokio/src/sync/watch.rs | 41 | 100 |
tokio-rs/tokio:tokio/src/sync/watch.rs:5 | ///
/// ```
/// 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 let Some(value) = rx.recv().await {
/// println!("received = {:?}", value);
/// }
/... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | 8a7e57786a5dca139f5b4261685e22991ded0859 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/8a7e57786a5dca139f5b4261685e22991ded0859/tokio/src/sync/watch.rs | 161 | 220 |
tokio-rs/tokio:tokio/src/sync/watch.rs:6 | shared: Arc::downgrade(&shared),
};
let rx = Receiver {
shared,
inner,
id: INIT_ID,
ver: 0,
};
(tx, rx)
}
impl<T> Receiver<T> {
/// Returns a reference to the most recently sent value
///
/// Outstanding borrows hold a read lock. This means that long lived ... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | 8a7e57786a5dca139f5b4261685e22991ded0859 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/8a7e57786a5dca139f5b4261685e22991ded0859/tokio/src/sync/watch.rs | 201 | 260 |
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`]: struct.Sender.html
//! [`Receiver`]: s... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | 0da23aad772afb22db8edf73ac0f034c5ada3bde | github | async-runtime | https://github.com/tokio-rs/tokio/blob/0da23aad772afb22db8edf73ac0f034c5ada3bde/tokio/src/sync/watch.rs | 41 | 100 |
tokio-rs/tokio:tokio/src/sync/watch.rs:3 | id: u64,
/// Last observed version
ver: usize,
}
/// Sends values to the associated [`Receiver`](struct.Receiver.html).
///
/// Instances are created by the [`channel`](fn.channel.html) function.
#[derive(Debug)]
pub struct Sender<T> {
shared: Weak<Shared<T>>,
}
/// Returns a reference to the inner value... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | 0da23aad772afb22db8edf73ac0f034c5ada3bde | github | async-runtime | https://github.com/tokio-rs/tokio/blob/0da23aad772afb22db8edf73ac0f034c5ada3bde/tokio/src/sync/watch.rs | 81 | 140 |
tokio-rs/tokio:tokio/src/sync/watch.rs:4 | }
}
impl<T: fmt::Debug> ::std::error::Error for SendError<T> {}
}
#[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 current version.
ver... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | 0da23aad772afb22db8edf73ac0f034c5ada3bde | github | async-runtime | https://github.com/tokio-rs/tokio/blob/0da23aad772afb22db8edf73ac0f034c5ada3bde/tokio/src/sync/watch.rs | 121 | 180 |
tokio-rs/tokio:tokio/src/sync/watch.rs:5 | /// 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 | 0da23aad772afb22db8edf73ac0f034c5ada3bde | github | async-runtime | https://github.com/tokio-rs/tokio/blob/0da23aad772afb22db8edf73ac0f034c5ada3bde/tokio/src/sync/watch.rs | 161 | 220 |
tokio-rs/tokio:tokio/src/sync/watch.rs:6 | cancel: AtomicWaker::new(),
});
let tx = Sender {
shared: Arc::downgrade(&shared),
};
let rx = Receiver {
shared,
inner,
id: INIT_ID,
ver: 0,
};
(tx, rx)
}
impl<T> Receiver<T> {
/// Returns a reference to the most recently sent value
///
//... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | 0da23aad772afb22db8edf73ac0f034c5ada3bde | github | async-runtime | https://github.com/tokio-rs/tokio/blob/0da23aad772afb22db8edf73ac0f034c5ada3bde/tokio/src/sync/watch.rs | 201 | 260 |
tokio-rs/tokio:tokio/src/sync/watch.rs:7 | /// returned. If no new value has been sent, then `Pending` is returned and
/// the current task is notified once a new value is sent.
///
/// Only the **most recent** value is returned. If the receiver is falling
/// behind the sender, intermediate values are dropped.
pub async fn recv_ref(&mut sel... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | 0da23aad772afb22db8edf73ac0f034c5ada3bde | github | async-runtime | https://github.com/tokio-rs/tokio/blob/0da23aad772afb22db8edf73ac0f034c5ada3bde/tokio/src/sync/watch.rs | 241 | 300 |
tokio-rs/tokio:tokio/src/sync/watch.rs:8 | return Ready(None);
}
Pending
}
impl<T: Clone> Receiver<T> {
/// Attempts to clone the latest value sent via the channel.
///
/// This is equivalent to calling `clone()` on the value returned by
/// `recv_ref()`.
#[allow(clippy::map_clone)] // false positive: https://github.com/rust-lang/r... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | 0da23aad772afb22db8edf73ac0f034c5ada3bde | github | async-runtime | https://github.com/tokio-rs/tokio/blob/0da23aad772afb22db8edf73ac0f034c5ada3bde/tokio/src/sync/watch.rs | 281 | 340 |
tokio-rs/tokio:tokio/src/sync/watch.rs:9 | watchers.next_id += 1;
watchers.watchers.insert(id, inner.clone());
id
};
let ver = self.ver;
Receiver {
shared,
inner,
id,
ver,
}
}
}
impl<T> Drop for Receiver<T> {
fn drop(&mut self) {
let mut w... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | 0da23aad772afb22db8edf73ac0f034c5ada3bde | github | async-runtime | https://github.com/tokio-rs/tokio/blob/0da23aad772afb22db8edf73ac0f034c5ada3bde/tokio/src/sync/watch.rs | 321 | 380 |
tokio-rs/tokio:tokio/src/sync/watch.rs:10 | // Replace the value
{
let mut lock = shared.value.write().unwrap();
*lock = value;
}
// Update the version. 2 is used so that the CLOSED bit is not set.
shared.version.fetch_add(2, SeqCst);
// Notify all watchers
notify_all(&*shared);
/... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | 0da23aad772afb22db8edf73ac0f034c5ada3bde | github | async-runtime | https://github.com/tokio-rs/tokio/blob/0da23aad772afb22db8edf73ac0f034c5ada3bde/tokio/src/sync/watch.rs | 361 | 420 |
tokio-rs/tokio:tokio/src/sync/watch.rs:11 | Ready(Ok(()))
}
fn start_send(self: Pin<&mut Self>, item: T) -> Result<(), Self::Error> {
self.as_ref().get_ref().broadcast(item)?;
Ok(())
}
fn poll_flush(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
Ready(Ok(()))
}
fn poll_close(self... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | 0da23aad772afb22db8edf73ac0f034c5ada3bde | github | async-runtime | https://github.com/tokio-rs/tokio/blob/0da23aad772afb22db8edf73ac0f034c5ada3bde/tokio/src/sync/watch.rs | 401 | 453 |
tokio-rs/tokio:tokio/src/sync/watch.rs:12 | fn deref(&self) -> &T {
self.inner.deref()
}
}
// ===== impl Shared =====
impl<T> Drop for Shared<T> {
fn drop(&mut self) {
self.cancel.wake();
}
} | rust | rust | rust-source | tokio-rs/tokio | tokio/src/sync/watch.rs | MIT | 0da23aad772afb22db8edf73ac0f034c5ada3bde | github | async-runtime | https://github.com/tokio-rs/tokio/blob/0da23aad772afb22db8edf73ac0f034c5ada3bde/tokio/src/sync/watch.rs | 441 | 453 |
tokio-rs/tokio:tokio/src/task/blocking.rs:3 | /// operations to finish. You can use [`shutdown_timeout`] to stop waiting
/// for them after a certain timeout. Be aware that this will still not
/// cancel the tasks — they are simply allowed to keep running after the
/// method returns.
///
/// Note that if you are using the [basic scheduler], th... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/task/blocking.rs | MIT | b9e3d2edde33a12ead6df3895caeafa90f9db7e4 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/b9e3d2edde33a12ead6df3895caeafa90f9db7e4/tokio/src/task/blocking.rs | 81 | 132 |
tokio-rs/tokio:tokio/src/task/blocking.rs:4 | "task",
kind = %"blocking",
function = %std::any::type_name::<F>(),
);
move || {
let _g = span.enter();
f()
}
};
crate::runtime::spawn_blocking(f)
}
} | rust | rust | rust-source | tokio-rs/tokio | tokio/src/task/blocking.rs | MIT | b9e3d2edde33a12ead6df3895caeafa90f9db7e4 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/b9e3d2edde33a12ead6df3895caeafa90f9db7e4/tokio/src/task/blocking.rs | 121 | 132 |
tokio-rs/tokio:tokio/src/task/blocking.rs:2 | ///
/// More information about the blocking threads is available at [CPU-bound tasks and blocking code][blocking].
///
/// [blocking]: ../index.html#cpu-bound-tasks-and-blocking-code
///
/// # Examples
///
/// ```
/// use tokio::task;
///
/// # async fn docs() -> Result<(), Box<... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/task/blocking.rs | MIT | f83f6388c42aa62c2096073b1dd80459189d7ea9 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/f83f6388c42aa62c2096073b1dd80459189d7ea9/tokio/src/task/blocking.rs | 41 | 68 |
tokio-rs/tokio:tokio/src/task/blocking.rs:2 | ///
/// In general, issuing a blocking call or performing a lot of compute in a future without
/// yielding is not okay, as it may prevent the executor from driving other futures forward.
/// A closure that is run through this method will instead be run on a dedicated thread pool for
/// such blocking t... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/task/blocking.rs | MIT | 67c4cc03919a58076c139f0930f28c3c41dad1e5 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/67c4cc03919a58076c139f0930f28c3c41dad1e5/tokio/src/task/blocking.rs | 41 | 69 |
tokio-rs/tokio:tokio/src/task/blocking.rs:2 | cfg_blocking! {
/// Runs the provided closure on a thread where blocking is acceptable.
///
/// In general, issuing a blocking call or performing a lot of compute in a future without
/// yielding is not okay, as it may prevent the executor from driving other futures forward.
/// A closure that is ru... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/task/blocking.rs | MIT | f9ddb93604a830d106475bd4c4cae436fafcc0da | github | async-runtime | https://github.com/tokio-rs/tokio/blob/f9ddb93604a830d106475bd4c4cae436fafcc0da/tokio/src/task/blocking.rs | 41 | 71 |
tokio-rs/tokio:tokio/src/task/blocking.rs:2 | cfg_blocking! {
/// Run the provided closure on a thread where blocking is acceptable.
///
/// In general, issuing a blocking call or performing a lot of compute in a future without
/// yielding is not okay, as it may prevent the executor from driving other futures forward.
/// A closure that is run... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/task/blocking.rs | MIT | 5bbf9762681ae75c6489558e15f894d0466516a3 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/5bbf9762681ae75c6489558e15f894d0466516a3/tokio/src/task/blocking.rs | 41 | 71 |
tokio-rs/tokio:tokio/src/task/blocking.rs:1 | use crate::task::JoinHandle;
cfg_rt_threaded! {
/// Run the provided blocking function without blocking the executor.
///
/// In general, issuing a blocking call or performing a lot of compute in a
/// future without yielding is not okay, as it may prevent the executor from
/// driving other future... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/task/blocking.rs | MIT | 8b60c5386a60be4dacffe71823350040e8ba90fd | github | async-runtime | https://github.com/tokio-rs/tokio/blob/8b60c5386a60be4dacffe71823350040e8ba90fd/tokio/src/task/blocking.rs | 1 | 60 |
tokio-rs/tokio:tokio/src/task/blocking.rs:2 | /// such blocking tasks without holding up the main futures executor.
///
/// # Examples
///
/// ```
/// use tokio::task;
///
/// # async fn docs() -> Result<(), Box<dyn std::error::Error>>{
/// let res = task::spawn_blocking(move || {
/// // do some compute-heavy work or call sy... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/task/blocking.rs | MIT | 8b60c5386a60be4dacffe71823350040e8ba90fd | github | async-runtime | https://github.com/tokio-rs/tokio/blob/8b60c5386a60be4dacffe71823350040e8ba90fd/tokio/src/task/blocking.rs | 41 | 65 |
tokio-rs/tokio:tokio/src/task/blocking.rs:1 | use crate::task::JoinHandle;
cfg_rt_threaded! {
/// Run the provided blocking function without blocking the executor.
///
/// In general, issuing a blocking call or performing a lot of compute in a
/// future without yielding is not okay, as it may prevent the executor from
/// driving other future... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/task/blocking.rs | MIT | 942feab040e28d1e1547012a0fb81212299e6596 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/942feab040e28d1e1547012a0fb81212299e6596/tokio/src/task/blocking.rs | 1 | 60 |
tokio-rs/tokio:tokio/src/task/blocking.rs:2 | ///
/// # Examples
///
/// ```
/// use tokio::task;
///
/// # async fn docs() -> Result<(), Box<dyn std::error::Error>>{
/// let res = task::spawn_blocking(move || {
/// // do some compute-heavy work or call synchronous code
/// "done computing"
/// }).await?;
///
... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/task/blocking.rs | MIT | 942feab040e28d1e1547012a0fb81212299e6596 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/942feab040e28d1e1547012a0fb81212299e6596/tokio/src/task/blocking.rs | 41 | 64 |
tokio-rs/tokio:tokio/src/task/blocking.rs:1 | use crate::task::JoinHandle;
cfg_rt_threaded! {
/// Run the provided blocking function without blocking the executor.
///
/// In general, issuing a blocking call or performing a lot of compute in a
/// future without yielding is not okay, as it may prevent the executor from
/// driving other future... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/task/blocking.rs | MIT | 8546ff826db8dba1e39b4119ad909fb6cab2492a | github | async-runtime | https://github.com/tokio-rs/tokio/blob/8546ff826db8dba1e39b4119ad909fb6cab2492a/tokio/src/task/blocking.rs | 1 | 60 |
tokio-rs/tokio:tokio/src/task/blocking.rs:1 | use crate::blocking;
use crate::task::JoinHandle;
cfg_rt_threaded! {
/// Run the provided blocking function without blocking the executor.
///
/// In general, issuing a blocking call or performing a lot of compute in a
/// future without yielding is not okay, as it may prevent the executor from
///... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/task/blocking.rs | MIT | 0d38936b35779b604770120da2e98560bbb6241f | github | async-runtime | https://github.com/tokio-rs/tokio/blob/0d38936b35779b604770120da2e98560bbb6241f/tokio/src/task/blocking.rs | 1 | 60 |
tokio-rs/tokio:tokio/src/task/blocking.rs:2 | /// such blocking tasks without holding up the main futures executor.
///
/// # Examples
///
/// ```
/// use tokio::task;
///
/// # async fn docs() -> Result<(), Box<dyn std::error::Error>>{
/// let res = task::spawn_blocking(move || {
/// // do some compute-heavy work or call sy... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/task/blocking.rs | MIT | 0d38936b35779b604770120da2e98560bbb6241f | github | async-runtime | https://github.com/tokio-rs/tokio/blob/0d38936b35779b604770120da2e98560bbb6241f/tokio/src/task/blocking.rs | 41 | 65 |
tokio-rs/tokio:tokio/src/task/blocking.rs:1 | use crate::blocking;
use crate::task::JoinHandle;
/// Run the provided blocking function without blocking the executor.
///
/// In general, issuing a blocking call or performing a lot of compute in a
/// future without yielding is not okay, as it may prevent the executor from
/// driving other futures forward. If you... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/task/blocking.rs | MIT | b1d9e55487d8411da89788ce0fbb3eb99a1f3a40 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/b1d9e55487d8411da89788ce0fbb3eb99a1f3a40/tokio/src/task/blocking.rs | 1 | 60 |
tokio-rs/tokio:tokio/src/task/blocking.rs:2 | /// # Examples
///
/// ```
/// use tokio::task;
///
/// # async fn docs() -> Result<(), Box<dyn std::error::Error>>{
/// let res = task::spawn_blocking(move || {
/// // do some compute-heavy work or call synchronous code
/// "done computing"
/// }).await?;
///
/// assert_eq!(res, "done computing");
/// # Ok(())... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/task/blocking.rs | MIT | b1d9e55487d8411da89788ce0fbb3eb99a1f3a40 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/b1d9e55487d8411da89788ce0fbb3eb99a1f3a40/tokio/src/task/blocking.rs | 41 | 62 |
tokio-rs/tokio:tokio/src/task/builder.rs:2 | /// async fn main() -> io::Result<()> {
/// let listener = TcpListener::bind("127.0.0.1:8080").await?;
///
/// loop {
/// let (socket, _) = listener.accept().await?;
///
/// tokio::task::Builder::new()
/// .name("tcp connection handler")
/// .spawn(async move {
/// ... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/task/builder.rs | MIT | c6d58ce7e7bf4a6e7e6efc1ffeb42daa3a627c17 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/c6d58ce7e7bf4a6e7e6efc1ffeb42daa3a627c17/tokio/src/task/builder.rs | 41 | 100 |
tokio-rs/tokio:tokio/src/task/builder.rs:3 | ///
/// This method panics if called outside of a Tokio runtime.
///
/// See [`task::spawn`](crate::task::spawn()) for
/// more details.
#[track_caller]
pub fn spawn<Fut>(self, future: Fut) -> io::Result<JoinHandle<Fut::Output>>
where
Fut: Future + Send + 'static,
Fut::Output... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/task/builder.rs | MIT | c6d58ce7e7bf4a6e7e6efc1ffeb42daa3a627c17 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/c6d58ce7e7bf4a6e7e6efc1ffeb42daa3a627c17/tokio/src/task/builder.rs | 81 | 140 |
tokio-rs/tokio:tokio/src/task/builder.rs:4 | /// Spawns a `!Send` task on the current [`LocalSet`] or [`LocalRuntime`] with
/// this builder's settings.
///
/// The spawned future will be run on the same thread that called `spawn_local`.
/// This may only be called from the context of a [local task set][`LocalSet`]
/// or a [`LocalRuntime`].
... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/task/builder.rs | MIT | c6d58ce7e7bf4a6e7e6efc1ffeb42daa3a627c17 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/c6d58ce7e7bf4a6e7e6efc1ffeb42daa3a627c17/tokio/src/task/builder.rs | 121 | 180 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.