Skip to content

Commit

Permalink
sync: add watch::Sender::borrow() (#3269)
Browse files Browse the repository at this point in the history
  • Loading branch information
JOT85 authored Dec 14, 2020
1 parent 9149d7b commit 1f862d2
Showing 1 changed file with 59 additions and 0 deletions.
59 changes: 59 additions & 0 deletions tokio/src/sync/watch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,25 @@ impl<T> Sender<T> {
Ok(())
}

/// 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.
///
/// # Examples
///
/// ```
/// use tokio::sync::watch;
///
/// let (tx, _) = watch::channel("hello");
/// assert_eq!(*tx.borrow(), "hello");
/// ```
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.
///
Expand Down Expand Up @@ -430,4 +449,44 @@ mod tests {
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));

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_thread = thread::spawn(move || {
recv.changed().now_or_never();
recv.changed().now_or_never();
recv
});

send.send(3).unwrap();

let recv = recv_thread.join().unwrap();
assert!(recv.borrow().eq(&3));
assert!(send.borrow().eq(&3));

send.send(2).unwrap();

thread::spawn(move || {
assert!(recv.borrow().eq(&2));
});
assert!(send.borrow().eq(&2));
});
}
}

0 comments on commit 1f862d2

Please sign in to comment.