Path: blob/main/crates/bevy_platform/src/sync/barrier.rs
6849 views
//! Provides `Barrier` and `BarrierWaitResult`12pub use implementation::{Barrier, BarrierWaitResult};34#[cfg(feature = "std")]5use std::sync as implementation;67#[cfg(not(feature = "std"))]8mod implementation {9use core::fmt;1011/// Fallback implementation of `Barrier` from the standard library.12pub struct Barrier {13inner: spin::Barrier,14}1516impl Barrier {17/// Creates a new barrier that can block a given number of threads.18///19/// See the standard library for further details.20#[must_use]21pub const fn new(n: usize) -> Self {22Self {23inner: spin::Barrier::new(n),24}25}2627/// Blocks the current thread until all threads have rendezvoused here.28///29/// See the standard library for further details.30pub fn wait(&self) -> BarrierWaitResult {31BarrierWaitResult {32inner: self.inner.wait(),33}34}35}3637impl fmt::Debug for Barrier {38fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {39f.debug_struct("Barrier").finish_non_exhaustive()40}41}4243/// Fallback implementation of `BarrierWaitResult` from the standard library.44pub struct BarrierWaitResult {45inner: spin::barrier::BarrierWaitResult,46}4748impl BarrierWaitResult {49/// Returns `true` if this thread is the "leader thread" for the call to [`Barrier::wait()`].50///51/// See the standard library for further details.52#[must_use]53pub fn is_leader(&self) -> bool {54self.inner.is_leader()55}56}5758impl fmt::Debug for BarrierWaitResult {59fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {60f.debug_struct("BarrierWaitResult")61.field("is_leader", &self.is_leader())62.finish()63}64}65}666768