mirror of
https://github.com/tokio-rs/tokio.git
synced 2026-08-29 00:00:11 +02:00
sync: Barrier doc should use task, not thread (#3780)
This commit is contained in:
@@ -2,7 +2,7 @@ use crate::sync::watch;
|
|||||||
|
|
||||||
use std::sync::Mutex;
|
use std::sync::Mutex;
|
||||||
|
|
||||||
/// A barrier enables multiple threads to synchronize the beginning of some computation.
|
/// A barrier enables multiple tasks to synchronize the beginning of some computation.
|
||||||
///
|
///
|
||||||
/// ```
|
/// ```
|
||||||
/// # #[tokio::main]
|
/// # #[tokio::main]
|
||||||
@@ -52,10 +52,10 @@ struct BarrierState {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl Barrier {
|
impl Barrier {
|
||||||
/// Creates a new barrier that can block a given number of threads.
|
/// Creates a new barrier that can block a given number of tasks.
|
||||||
///
|
///
|
||||||
/// A barrier will block `n`-1 threads which call [`Barrier::wait`] and then wake up all
|
/// A barrier will block `n`-1 tasks which call [`Barrier::wait`] and then wake up all
|
||||||
/// threads at once when the `n`th thread calls `wait`.
|
/// tasks at once when the `n`th task calls `wait`.
|
||||||
pub fn new(mut n: usize) -> Barrier {
|
pub fn new(mut n: usize) -> Barrier {
|
||||||
let (waker, wait) = crate::sync::watch::channel(0);
|
let (waker, wait) = crate::sync::watch::channel(0);
|
||||||
|
|
||||||
@@ -79,11 +79,11 @@ impl Barrier {
|
|||||||
|
|
||||||
/// Does not resolve until all tasks have rendezvoused here.
|
/// Does not resolve until all tasks have rendezvoused here.
|
||||||
///
|
///
|
||||||
/// Barriers are re-usable after all threads have rendezvoused once, and can
|
/// Barriers are re-usable after all tasks have rendezvoused once, and can
|
||||||
/// be used continuously.
|
/// be used continuously.
|
||||||
///
|
///
|
||||||
/// A single (arbitrary) future will receive a [`BarrierWaitResult`] that returns `true` from
|
/// A single (arbitrary) future will receive a [`BarrierWaitResult`] that returns `true` from
|
||||||
/// [`BarrierWaitResult::is_leader`] when returning from this function, and all other threads
|
/// [`BarrierWaitResult::is_leader`] when returning from this function, and all other tasks
|
||||||
/// will receive a result that will return `false` from `is_leader`.
|
/// will receive a result that will return `false` from `is_leader`.
|
||||||
pub async fn wait(&self) -> BarrierWaitResult {
|
pub async fn wait(&self) -> BarrierWaitResult {
|
||||||
// NOTE: we are taking a _synchronous_ lock here.
|
// NOTE: we are taking a _synchronous_ lock here.
|
||||||
@@ -129,14 +129,14 @@ impl Barrier {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// A `BarrierWaitResult` is returned by `wait` when all threads in the `Barrier` have rendezvoused.
|
/// A `BarrierWaitResult` is returned by `wait` when all tasks in the `Barrier` have rendezvoused.
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct BarrierWaitResult(bool);
|
pub struct BarrierWaitResult(bool);
|
||||||
|
|
||||||
impl BarrierWaitResult {
|
impl BarrierWaitResult {
|
||||||
/// Returns `true` if this thread from wait is the "leader thread".
|
/// Returns `true` if this task from wait is the "leader task".
|
||||||
///
|
///
|
||||||
/// Only one thread will have `true` returned from their result, all other threads will have
|
/// Only one task will have `true` returned from their result, all other tasks will have
|
||||||
/// `false` returned.
|
/// `false` returned.
|
||||||
pub fn is_leader(&self) -> bool {
|
pub fn is_leader(&self) -> bool {
|
||||||
self.0
|
self.0
|
||||||
|
|||||||
Reference in New Issue
Block a user