Coverage Report

Created: 2026-09-19 07:25

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