Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
bevyengine
GitHub Repository: bevyengine/bevy
Path: blob/main/crates/bevy_platform/src/sync/barrier.rs
6849 views
1
//! Provides `Barrier` and `BarrierWaitResult`
2
3
pub use implementation::{Barrier, BarrierWaitResult};
4
5
#[cfg(feature = "std")]
6
use std::sync as implementation;
7
8
#[cfg(not(feature = "std"))]
9
mod implementation {
10
use core::fmt;
11
12
/// Fallback implementation of `Barrier` from the standard library.
13
pub struct Barrier {
14
inner: spin::Barrier,
15
}
16
17
impl Barrier {
18
/// Creates a new barrier that can block a given number of threads.
19
///
20
/// See the standard library for further details.
21
#[must_use]
22
pub const fn new(n: usize) -> Self {
23
Self {
24
inner: spin::Barrier::new(n),
25
}
26
}
27
28
/// Blocks the current thread until all threads have rendezvoused here.
29
///
30
/// See the standard library for further details.
31
pub fn wait(&self) -> BarrierWaitResult {
32
BarrierWaitResult {
33
inner: self.inner.wait(),
34
}
35
}
36
}
37
38
impl fmt::Debug for Barrier {
39
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
40
f.debug_struct("Barrier").finish_non_exhaustive()
41
}
42
}
43
44
/// Fallback implementation of `BarrierWaitResult` from the standard library.
45
pub struct BarrierWaitResult {
46
inner: spin::barrier::BarrierWaitResult,
47
}
48
49
impl BarrierWaitResult {
50
/// Returns `true` if this thread is the "leader thread" for the call to [`Barrier::wait()`].
51
///
52
/// See the standard library for further details.
53
#[must_use]
54
pub fn is_leader(&self) -> bool {
55
self.inner.is_leader()
56
}
57
}
58
59
impl fmt::Debug for BarrierWaitResult {
60
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
61
f.debug_struct("BarrierWaitResult")
62
.field("is_leader", &self.is_leader())
63
.finish()
64
}
65
}
66
}
67
68