rayon_core/sleep/
counters.rs1use std::sync::atomic::{AtomicUsize, Ordering};
2
3pub(super) struct AtomicCounters {
4 value: AtomicUsize,
16}
17
18#[derive(Copy, Clone)]
19pub(super) struct Counters {
20 word: usize,
21}
22
23#[derive(Copy, Clone, Debug, PartialEq, PartialOrd)]
27pub(super) struct JobsEventCounter(usize);
28
29impl JobsEventCounter {
30 pub(super) const DUMMY: JobsEventCounter = JobsEventCounter(std::usize::MAX);
31
32 #[inline]
33 pub(super) fn as_usize(self) -> usize {
34 self.0
35 }
36
37 #[inline]
42 pub(super) fn is_sleepy(self) -> bool {
43 (self.as_usize() & 1) == 0
44 }
45
46 #[inline]
50 pub(super) fn is_active(self) -> bool {
51 !self.is_sleepy()
52 }
53}
54
55#[cfg(target_pointer_width = "64")]
57const THREADS_BITS: usize = 16;
58
59#[cfg(target_pointer_width = "32")]
60const THREADS_BITS: usize = 8;
61
62#[allow(clippy::erasing_op)]
65const SLEEPING_SHIFT: usize = 0 * THREADS_BITS;
66
67#[allow(clippy::identity_op)]
70const INACTIVE_SHIFT: usize = 1 * THREADS_BITS;
71
72const JEC_SHIFT: usize = 2 * THREADS_BITS;
75
76pub(crate) const THREADS_MAX: usize = (1 << THREADS_BITS) - 1;
78
79const ONE_SLEEPING: usize = 1;
81
82const ONE_INACTIVE: usize = 1 << INACTIVE_SHIFT;
85
86const ONE_JEC: usize = 1 << JEC_SHIFT;
88
89impl AtomicCounters {
90 #[inline]
91 pub(super) fn new() -> AtomicCounters {
92 AtomicCounters {
93 value: AtomicUsize::new(0),
94 }
95 }
96
97 #[inline]
101 pub(super) fn load(&self, ordering: Ordering) -> Counters {
102 Counters::new(self.value.load(ordering))
103 }
104
105 #[inline]
106 fn try_exchange(&self, old_value: Counters, new_value: Counters, ordering: Ordering) -> bool {
107 self.value
108 .compare_exchange(old_value.word, new_value.word, ordering, Ordering::Relaxed)
109 .is_ok()
110 }
111
112 #[inline]
120 pub(super) fn add_inactive_thread(&self) {
121 self.value.fetch_add(ONE_INACTIVE, Ordering::SeqCst);
122 }
123
124 pub(super) fn increment_jobs_event_counter_if(
129 &self,
130 increment_when: impl Fn(JobsEventCounter) -> bool,
131 ) -> Counters {
132 loop {
133 let old_value = self.load(Ordering::SeqCst);
134 if increment_when(old_value.jobs_counter()) {
135 let new_value = old_value.increment_jobs_counter();
136 if self.try_exchange(old_value, new_value, Ordering::SeqCst) {
137 return new_value;
138 }
139 } else {
140 return old_value;
141 }
142 }
143 }
144
145 #[inline]
151 pub(super) fn sub_inactive_thread(&self) -> usize {
152 let old_value = Counters::new(self.value.fetch_sub(ONE_INACTIVE, Ordering::SeqCst));
153 debug_assert!(
154 old_value.inactive_threads() > 0,
155 "sub_inactive_thread: old_value {:?} has no inactive threads",
156 old_value,
157 );
158 debug_assert!(
159 old_value.sleeping_threads() <= old_value.inactive_threads(),
160 "sub_inactive_thread: old_value {:?} had {} sleeping threads and {} inactive threads",
161 old_value,
162 old_value.sleeping_threads(),
163 old_value.inactive_threads(),
164 );
165
166 let sleeping_threads = old_value.sleeping_threads();
169 std::cmp::min(sleeping_threads, 2)
170 }
171
172 #[inline]
177 pub(super) fn sub_sleeping_thread(&self) {
178 let old_value = Counters::new(self.value.fetch_sub(ONE_SLEEPING, Ordering::SeqCst));
179 debug_assert!(
180 old_value.sleeping_threads() > 0,
181 "sub_sleeping_thread: old_value {:?} had no sleeping threads",
182 old_value,
183 );
184 debug_assert!(
185 old_value.sleeping_threads() <= old_value.inactive_threads(),
186 "sub_sleeping_thread: old_value {:?} had {} sleeping threads and {} inactive threads",
187 old_value,
188 old_value.sleeping_threads(),
189 old_value.inactive_threads(),
190 );
191 }
192
193 #[inline]
194 pub(super) fn try_add_sleeping_thread(&self, old_value: Counters) -> bool {
195 debug_assert!(
196 old_value.inactive_threads() > 0,
197 "try_add_sleeping_thread: old_value {:?} has no inactive threads",
198 old_value,
199 );
200 debug_assert!(
201 old_value.sleeping_threads() < THREADS_MAX,
202 "try_add_sleeping_thread: old_value {:?} has too many sleeping threads",
203 old_value,
204 );
205
206 let mut new_value = old_value;
207 new_value.word += ONE_SLEEPING;
208
209 self.try_exchange(old_value, new_value, Ordering::SeqCst)
210 }
211}
212
213#[inline]
214fn select_thread(word: usize, shift: usize) -> usize {
215 (word >> shift) & THREADS_MAX
216}
217
218#[inline]
219fn select_jec(word: usize) -> usize {
220 word >> JEC_SHIFT
221}
222
223impl Counters {
224 #[inline]
225 fn new(word: usize) -> Counters {
226 Counters { word }
227 }
228
229 #[inline]
230 fn increment_jobs_counter(self) -> Counters {
231 Counters {
234 word: self.word.wrapping_add(ONE_JEC),
235 }
236 }
237
238 #[inline]
239 pub(super) fn jobs_counter(self) -> JobsEventCounter {
240 JobsEventCounter(select_jec(self.word))
241 }
242
243 #[inline]
246 pub(super) fn inactive_threads(self) -> usize {
247 select_thread(self.word, INACTIVE_SHIFT)
248 }
249
250 #[inline]
251 pub(super) fn awake_but_idle_threads(self) -> usize {
252 debug_assert!(
253 self.sleeping_threads() <= self.inactive_threads(),
254 "sleeping threads: {} > raw idle threads {}",
255 self.sleeping_threads(),
256 self.inactive_threads()
257 );
258 self.inactive_threads() - self.sleeping_threads()
259 }
260
261 #[inline]
262 pub(super) fn sleeping_threads(self) -> usize {
263 select_thread(self.word, SLEEPING_SHIFT)
264 }
265}
266
267impl std::fmt::Debug for Counters {
268 fn fmt(&self, fmt: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
269 let word = format!("{:016x}", self.word);
270 fmt.debug_struct("Counters")
271 .field("word", &word)
272 .field("jobs", &self.jobs_counter().0)
273 .field("inactive", &self.inactive_threads())
274 .field("sleeping", &self.sleeping_threads())
275 .finish()
276 }
277}