Skip to main content

flize/ebr/
mod.rs

1mod bag;
2mod ct;
3mod epoch;
4mod global;
5mod local;
6mod shield;
7
8pub use epoch::DefinitiveEpoch;
9pub use local::Local;
10pub use shield::{unprotected, CowShield, FullShield, Shield, ThinShield, UnprotectedShield};
11
12use core::fmt;
13use global::Global;
14use std::sync::Arc;
15
16const ADVANCE_PROBABILITY: usize = 128;
17
18/// The `Collector` acts like the central bookkeeper, it stores all the retired functions that are queued
19/// for execution along with information on what each participant is doing, Participants are pretty much always
20/// thread specific as of now but cross-thread participants may be added in the future. This information can be used to determine approximately
21/// when a participant last was in in a critical section and relevant shield history. The collector
22/// uses this information to determine when it is safe to execute a retired function.
23pub struct Collector {
24    global: Arc<Global>,
25}
26
27impl Collector {
28    pub fn new() -> Self {
29        Self {
30            global: Arc::new(Global::new()),
31        }
32    }
33
34    pub fn epoch(&self) -> DefinitiveEpoch {
35        self.global.definitive_epoch()
36    }
37
38    /// Creates a shield on the appropriate local given the current thread.
39    pub fn thin_shield(&self) -> ThinShield<'_> {
40        Global::thin_shield(&self.global)
41    }
42
43    pub fn full_shield(&self) -> FullShield<'_> {
44        Global::full_shield(&self.global)
45    }
46
47    /// Get the local for the current thread.
48    pub fn local(&self) -> Local {
49        Global::local(&self.global)
50    }
51
52    /// Attempt to advance the epoch and collect garbage.
53    /// The result represents whether or not the attempt to advance the global epoch
54    /// was successful and if it was the integer is how many retired functions were executed.
55    pub fn try_collect_light(&self) -> Result<usize, ()> {
56        Global::try_collect_light(&self.global)
57    }
58}
59
60impl Default for Collector {
61    fn default() -> Self {
62        Self::new()
63    }
64}
65
66unsafe impl Send for Collector {}
67unsafe impl Sync for Collector {}
68
69impl fmt::Debug for Collector {
70    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
71        f.pad("Collector { .. }")
72    }
73}