Jakub Kotur | fc1672b | 2020-12-21 17:28:14 +0100 | [diff] [blame] | 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 | |
Joel Galenson | 0d44092 | 2021-04-01 15:34:31 -0700 | [diff] [blame] | 10 | use crate::primitive::sync::atomic::AtomicUsize; |
| 11 | use core::sync::atomic::Ordering; |
Jakub Kotur | fc1672b | 2020-12-21 17:28:14 +0100 | [diff] [blame] | 12 | |
| 13 | /// An epoch that can be marked as pinned or unpinned. |
| 14 | /// |
| 15 | /// Internally, the epoch is represented as an integer that wraps around at some unspecified point |
| 16 | /// and a flag that represents whether it is pinned or unpinned. |
| 17 | #[derive(Copy, Clone, Default, Debug, Eq, PartialEq)] |
Joel Galenson | 0d44092 | 2021-04-01 15:34:31 -0700 | [diff] [blame] | 18 | pub(crate) struct Epoch { |
Jakub Kotur | fc1672b | 2020-12-21 17:28:14 +0100 | [diff] [blame] | 19 | /// The least significant bit is set if pinned. The rest of the bits hold the epoch. |
| 20 | data: usize, |
| 21 | } |
| 22 | |
| 23 | impl Epoch { |
| 24 | /// Returns the starting epoch in unpinned state. |
| 25 | #[inline] |
Joel Galenson | 0d44092 | 2021-04-01 15:34:31 -0700 | [diff] [blame] | 26 | pub(crate) fn starting() -> Self { |
Jakub Kotur | fc1672b | 2020-12-21 17:28:14 +0100 | [diff] [blame] | 27 | Self::default() |
| 28 | } |
| 29 | |
| 30 | /// Returns the number of epochs `self` is ahead of `rhs`. |
| 31 | /// |
| 32 | /// Internally, epochs are represented as numbers in the range `(isize::MIN / 2) .. (isize::MAX |
| 33 | /// / 2)`, so the returned distance will be in the same interval. |
Joel Galenson | 0d44092 | 2021-04-01 15:34:31 -0700 | [diff] [blame] | 34 | pub(crate) fn wrapping_sub(self, rhs: Self) -> isize { |
Jakub Kotur | fc1672b | 2020-12-21 17:28:14 +0100 | [diff] [blame] | 35 | // The result is the same with `(self.data & !1).wrapping_sub(rhs.data & !1) as isize >> 1`, |
| 36 | // because the possible difference of LSB in `(self.data & !1).wrapping_sub(rhs.data & !1)` |
| 37 | // will be ignored in the shift operation. |
| 38 | self.data.wrapping_sub(rhs.data & !1) as isize >> 1 |
| 39 | } |
| 40 | |
| 41 | /// Returns `true` if the epoch is marked as pinned. |
| 42 | #[inline] |
Joel Galenson | 0d44092 | 2021-04-01 15:34:31 -0700 | [diff] [blame] | 43 | pub(crate) fn is_pinned(self) -> bool { |
Jakub Kotur | fc1672b | 2020-12-21 17:28:14 +0100 | [diff] [blame] | 44 | (self.data & 1) == 1 |
| 45 | } |
| 46 | |
| 47 | /// Returns the same epoch, but marked as pinned. |
| 48 | #[inline] |
Joel Galenson | 0d44092 | 2021-04-01 15:34:31 -0700 | [diff] [blame] | 49 | pub(crate) fn pinned(self) -> Epoch { |
Jakub Kotur | fc1672b | 2020-12-21 17:28:14 +0100 | [diff] [blame] | 50 | Epoch { |
| 51 | data: self.data | 1, |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | /// Returns the same epoch, but marked as unpinned. |
| 56 | #[inline] |
Joel Galenson | 0d44092 | 2021-04-01 15:34:31 -0700 | [diff] [blame] | 57 | pub(crate) fn unpinned(self) -> Epoch { |
Jakub Kotur | fc1672b | 2020-12-21 17:28:14 +0100 | [diff] [blame] | 58 | Epoch { |
| 59 | data: self.data & !1, |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | /// Returns the successor epoch. |
| 64 | /// |
| 65 | /// The returned epoch will be marked as pinned only if the previous one was as well. |
| 66 | #[inline] |
Joel Galenson | 0d44092 | 2021-04-01 15:34:31 -0700 | [diff] [blame] | 67 | pub(crate) fn successor(self) -> Epoch { |
Jakub Kotur | fc1672b | 2020-12-21 17:28:14 +0100 | [diff] [blame] | 68 | Epoch { |
| 69 | data: self.data.wrapping_add(2), |
| 70 | } |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | /// An atomic value that holds an `Epoch`. |
| 75 | #[derive(Default, Debug)] |
Joel Galenson | 0d44092 | 2021-04-01 15:34:31 -0700 | [diff] [blame] | 76 | pub(crate) struct AtomicEpoch { |
Jakub Kotur | fc1672b | 2020-12-21 17:28:14 +0100 | [diff] [blame] | 77 | /// Since `Epoch` is just a wrapper around `usize`, an `AtomicEpoch` is similarly represented |
| 78 | /// using an `AtomicUsize`. |
| 79 | data: AtomicUsize, |
| 80 | } |
| 81 | |
| 82 | impl AtomicEpoch { |
| 83 | /// Creates a new atomic epoch. |
| 84 | #[inline] |
Joel Galenson | 0d44092 | 2021-04-01 15:34:31 -0700 | [diff] [blame] | 85 | pub(crate) fn new(epoch: Epoch) -> Self { |
Jakub Kotur | fc1672b | 2020-12-21 17:28:14 +0100 | [diff] [blame] | 86 | let data = AtomicUsize::new(epoch.data); |
| 87 | AtomicEpoch { data } |
| 88 | } |
| 89 | |
| 90 | /// Loads a value from the atomic epoch. |
| 91 | #[inline] |
Joel Galenson | 0d44092 | 2021-04-01 15:34:31 -0700 | [diff] [blame] | 92 | pub(crate) fn load(&self, ord: Ordering) -> Epoch { |
Jakub Kotur | fc1672b | 2020-12-21 17:28:14 +0100 | [diff] [blame] | 93 | Epoch { |
| 94 | data: self.data.load(ord), |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | /// Stores a value into the atomic epoch. |
| 99 | #[inline] |
Joel Galenson | 0d44092 | 2021-04-01 15:34:31 -0700 | [diff] [blame] | 100 | pub(crate) fn store(&self, epoch: Epoch, ord: Ordering) { |
Jakub Kotur | fc1672b | 2020-12-21 17:28:14 +0100 | [diff] [blame] | 101 | self.data.store(epoch.data, ord); |
| 102 | } |
| 103 | |
| 104 | /// Stores a value into the atomic epoch if the current value is the same as `current`. |
| 105 | /// |
Joel Galenson | 0d44092 | 2021-04-01 15:34:31 -0700 | [diff] [blame] | 106 | /// The return value is a result indicating whether the new value was written and containing |
| 107 | /// the previous value. On success this value is guaranteed to be equal to `current`. |
Jakub Kotur | fc1672b | 2020-12-21 17:28:14 +0100 | [diff] [blame] | 108 | /// |
Joel Galenson | 0d44092 | 2021-04-01 15:34:31 -0700 | [diff] [blame] | 109 | /// This method takes two `Ordering` arguments to describe the memory |
| 110 | /// ordering of this operation. `success` describes the required ordering for the |
| 111 | /// read-modify-write operation that takes place if the comparison with `current` succeeds. |
| 112 | /// `failure` describes the required ordering for the load operation that takes place when |
| 113 | /// the comparison fails. Using `Acquire` as success ordering makes the store part |
| 114 | /// of this operation `Relaxed`, and using `Release` makes the successful load |
| 115 | /// `Relaxed`. The failure ordering can only be `SeqCst`, `Acquire` or `Relaxed` |
| 116 | /// and must be equivalent to or weaker than the success ordering. |
Jakub Kotur | fc1672b | 2020-12-21 17:28:14 +0100 | [diff] [blame] | 117 | #[inline] |
Joel Galenson | 0d44092 | 2021-04-01 15:34:31 -0700 | [diff] [blame] | 118 | pub(crate) fn compare_exchange( |
| 119 | &self, |
| 120 | current: Epoch, |
| 121 | new: Epoch, |
| 122 | success: Ordering, |
| 123 | failure: Ordering, |
| 124 | ) -> Result<Epoch, Epoch> { |
| 125 | match self |
| 126 | .data |
| 127 | .compare_exchange(current.data, new.data, success, failure) |
| 128 | { |
| 129 | Ok(data) => Ok(Epoch { data }), |
| 130 | Err(data) => Err(Epoch { data }), |
| 131 | } |
Jakub Kotur | fc1672b | 2020-12-21 17:28:14 +0100 | [diff] [blame] | 132 | } |
| 133 | } |