crossbeam_epoch/epoch.rs
1//! The global epoch
2//!
3//! The last bit in this number is unused and is always zero. Every so often the global epoch is
4//! incremented, i.e. we say it "advances". A pinned participant may advance the global epoch only
5//! if all currently pinned participants have been pinned in the current epoch.
6//!
7//! If an object became garbage in some epoch, then we can be sure that after two advancements no
8//! participant will hold a reference to it. That is the crux of safe memory reclamation.
9
10use crate::primitive::sync::atomic::Ordering;
11
12// Ideally, we want to always use AtomicU64, but since it is not available on all platforms,
13// we only use it when it is available for now.
14// TODO: On platforms where AtomicU64 is unavailable, we may want to use AtomicCell instead of AtomicUsize.
15#[cfg(target_has_atomic = "64")]
16type AtomicEpochRepr = crate::primitive::sync::atomic::AtomicU64;
17#[cfg(not(target_has_atomic = "64"))]
18type AtomicEpochRepr = crate::primitive::sync::atomic::AtomicUsize;
19#[cfg(target_has_atomic = "64")]
20type EpochRepr = u64;
21#[cfg(not(target_has_atomic = "64"))]
22type EpochRepr = usize;
23#[cfg(target_has_atomic = "64")]
24type EpochReprSigned = i64;
25#[cfg(not(target_has_atomic = "64"))]
26type EpochReprSigned = isize;
27
28/// An epoch that can be marked as pinned or unpinned.
29///
30/// Internally, the epoch is represented as an integer that wraps around at some unspecified point
31/// and a flag that represents whether it is pinned or unpinned.
32#[derive(Copy, Clone, Default, Debug, Eq, PartialEq)]
33pub(crate) struct Epoch {
34 /// The least significant bit is set if pinned. The rest of the bits hold the epoch.
35 data: EpochRepr,
36}
37
38impl Epoch {
39 /// Returns the starting epoch in unpinned state.
40 #[inline]
41 pub(crate) fn starting() -> Self {
42 Self::default()
43 }
44
45 /// Returns the number of epochs `self` is ahead of `rhs`.
46 ///
47 /// Internally, epochs are represented as numbers in the range `(isize::MIN / 2) .. (isize::MAX
48 /// / 2)`, so the returned distance will be in the same interval.
49 pub(crate) fn wrapping_sub(self, rhs: Self) -> EpochReprSigned {
50 // The result is the same with `(self.data & !1).wrapping_sub(rhs.data & !1) as isize >> 1`,
51 // because the possible difference of LSB in `(self.data & !1).wrapping_sub(rhs.data & !1)`
52 // will be ignored in the shift operation.
53 self.data.wrapping_sub(rhs.data & !1) as EpochReprSigned >> 1
54 }
55
56 /// Returns `true` if the epoch is marked as pinned.
57 #[inline]
58 pub(crate) fn is_pinned(self) -> bool {
59 (self.data & 1) == 1
60 }
61
62 /// Returns the same epoch, but marked as pinned.
63 #[inline]
64 pub(crate) fn pinned(self) -> Epoch {
65 Epoch {
66 data: self.data | 1,
67 }
68 }
69
70 /// Returns the same epoch, but marked as unpinned.
71 #[inline]
72 pub(crate) fn unpinned(self) -> Epoch {
73 Epoch {
74 data: self.data & !1,
75 }
76 }
77
78 /// Returns the successor epoch.
79 ///
80 /// The returned epoch will be marked as pinned only if the previous one was as well.
81 #[inline]
82 pub(crate) fn successor(self) -> Epoch {
83 Epoch {
84 data: self.data.wrapping_add(2),
85 }
86 }
87}
88
89/// An atomic value that holds an `Epoch`.
90#[derive(Default, Debug)]
91pub(crate) struct AtomicEpoch {
92 /// Since `Epoch` is just a wrapper around `usize`, an `AtomicEpoch` is similarly represented
93 /// using an `AtomicUsize`.
94 data: AtomicEpochRepr,
95}
96
97impl AtomicEpoch {
98 /// Creates a new atomic epoch.
99 #[inline]
100 pub(crate) fn new(epoch: Epoch) -> Self {
101 let data = AtomicEpochRepr::new(epoch.data);
102 Self { data }
103 }
104
105 /// Loads a value from the atomic epoch.
106 #[inline]
107 pub(crate) fn load(&self, ord: Ordering) -> Epoch {
108 Epoch {
109 data: self.data.load(ord),
110 }
111 }
112
113 /// Stores a value into the atomic epoch.
114 #[inline]
115 pub(crate) fn store(&self, epoch: Epoch, ord: Ordering) {
116 self.data.store(epoch.data, ord);
117 }
118
119 /// Stores a value into the atomic epoch if the current value is the same as `current`.
120 ///
121 /// The return value is a result indicating whether the new value was written and containing
122 /// the previous value. On success this value is guaranteed to be equal to `current`.
123 ///
124 /// This method takes two `Ordering` arguments to describe the memory
125 /// ordering of this operation. `success` describes the required ordering for the
126 /// read-modify-write operation that takes place if the comparison with `current` succeeds.
127 /// `failure` describes the required ordering for the load operation that takes place when
128 /// the comparison fails. Using `Acquire` as success ordering makes the store part
129 /// of this operation `Relaxed`, and using `Release` makes the successful load
130 /// `Relaxed`. The failure ordering can only be `SeqCst`, `Acquire` or `Relaxed`
131 /// and must be equivalent to or weaker than the success ordering.
132 #[inline]
133 pub(crate) fn compare_exchange(
134 &self,
135 current: Epoch,
136 new: Epoch,
137 success: Ordering,
138 failure: Ordering,
139 ) -> Result<Epoch, Epoch> {
140 match self
141 .data
142 .compare_exchange(current.data, new.data, success, failure)
143 {
144 Ok(data) => Ok(Epoch { data }),
145 Err(data) => Err(Epoch { data }),
146 }
147 }
148}