1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
use core::fmt;
use core::sync::atomic::{AtomicUsize, Ordering};
use crate::epoch::Epoch;
use self::State::{Active, Inactive};
const INACTIVE_BIT: usize = 0b1;
#[derive(Debug)]
pub struct ThreadState(AtomicUsize);
impl ThreadState {
#[inline]
pub fn new(global_epoch: Epoch) -> Self {
Self(AtomicUsize::new(global_epoch.into_inner() | INACTIVE_BIT))
}
#[inline]
pub fn is_same(&self, other: &Self) -> bool {
self as *const Self == other as *const Self
}
#[inline]
pub fn load(&self, order: Ordering) -> (Epoch, State) {
let state = self.0.load(order);
(Epoch::with_epoch(state & !INACTIVE_BIT), State::from(state & INACTIVE_BIT == 0))
}
#[inline]
pub fn store(&self, epoch: Epoch, state: State, order: Ordering) {
match state {
Active => self.0.store(epoch.into_inner(), order),
Inactive => self.0.store(epoch.into_inner() | INACTIVE_BIT, order),
};
}
}
impl fmt::Display for ThreadState {
#[inline]
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let (epoch, state) = self.load(Ordering::SeqCst);
write!(f, "epoch {}, state: {}", epoch, state)
}
}
#[derive(Debug, Copy, Clone, Eq, Ord, PartialEq, PartialOrd)]
pub enum State {
Active,
Inactive,
}
impl From<bool> for State {
#[inline]
fn from(is_active: bool) -> Self {
if is_active {
Active
} else {
Inactive
}
}
}
impl fmt::Display for State {
#[inline]
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
Active => write!(f, "active"),
Inactive => write!(f, "inactive"),
}
}
}
#[cfg(test)]
mod tests {
use std::sync::atomic::Ordering::Relaxed;
use crate::epoch::Epoch;
use super::{
State::{self, Active, Inactive},
ThreadState,
};
#[test]
fn thread_state_equality() {
let epoch = Epoch::with_epoch(128);
let thread_state = ThreadState::new(epoch);
let other_thread_state = ThreadState::new(epoch);
assert!(thread_state.is_same(&thread_state));
assert!(!thread_state.is_same(&other_thread_state));
}
#[test]
fn load_thread_state() {
let init_epoch = Epoch::with_epoch(128);
let thread_state = ThreadState::new(init_epoch);
let (epoch, state) = thread_state.load(Relaxed);
assert_eq!(init_epoch, epoch);
assert_eq!(state, Inactive);
}
#[test]
fn store_thread_state() {
let init_epoch = Epoch::with_epoch(1000);
let thread_state = ThreadState::new(init_epoch);
let next_epoch = init_epoch + 1;
thread_state.store(next_epoch, Active, Relaxed);
let (epoch, state) = thread_state.load(Relaxed);
assert_eq!(epoch, next_epoch);
assert_eq!(state, Active);
}
#[test]
fn from_bool() {
assert_eq!(Active, State::from(true));
assert_eq!(Inactive, State::from(false));
}
}