std/sync/mpmc/utils.rs
1use crate::cell::Cell;
2use crate::ops::{Deref, DerefMut};
3
4/// Pads and aligns a value to the length of a cache line.
5#[derive(Clone, Copy, Default, Hash, PartialEq, Eq)]
6// Starting from Intel's Sandy Bridge, spatial prefetcher is now pulling pairs of 64-byte cache
7// lines at a time, so we have to align to 128 bytes rather than 64.
8//
9// Sources:
10// - https://www.intel.com/content/dam/www/public/us/en/documents/manuals/64-ia-32-architectures-optimization-manual.pdf
11// - https://github.com/facebook/folly/blob/1b5288e6eea6df074758f877c849b6e73bbb9fbb/folly/lang/Align.h#L107
12//
13// ARM's big.LITTLE architecture has asymmetric cores and "big" cores have 128-byte cache line size.
14//
15// Sources:
16// - https://www.mono-project.com/news/2016/09/12/arm64-icache/
17//
18// powerpc64 has 128-byte cache line size.
19//
20// Sources:
21// - https://github.com/golang/go/blob/3dd58676054223962cd915bb0934d1f9f489d4d2/src/internal/cpu/cpu_ppc64x.go#L9
22#[cfg_attr(
23 any(
24 target_arch = "x86_64",
25 target_arch = "aarch64",
26 target_arch = "powerpc64",
27 target_arch = "sw64",
28 ),
29 repr(align(128))
30)]
31// arm, mips, mips64, and riscv64 have 32-byte cache line size.
32//
33// Sources:
34// - https://github.com/golang/go/blob/3dd58676054223962cd915bb0934d1f9f489d4d2/src/internal/cpu/cpu_arm.go#L7
35// - https://github.com/golang/go/blob/3dd58676054223962cd915bb0934d1f9f489d4d2/src/internal/cpu/cpu_mips.go#L7
36// - https://github.com/golang/go/blob/3dd58676054223962cd915bb0934d1f9f489d4d2/src/internal/cpu/cpu_mipsle.go#L7
37// - https://github.com/golang/go/blob/3dd58676054223962cd915bb0934d1f9f489d4d2/src/internal/cpu/cpu_mips64x.go#L9
38// - https://github.com/golang/go/blob/3dd58676054223962cd915bb0934d1f9f489d4d2/src/internal/cpu/cpu_riscv64.go#L7
39#[cfg_attr(
40 any(
41 target_arch = "arm",
42 target_arch = "mips",
43 target_arch = "mips32r6",
44 target_arch = "mips64",
45 target_arch = "mips64r6",
46 target_arch = "riscv64",
47 ),
48 repr(align(32))
49)]
50// s390x has 256-byte cache line size.
51//
52// Sources:
53// - https://github.com/golang/go/blob/3dd58676054223962cd915bb0934d1f9f489d4d2/src/internal/cpu/cpu_s390x.go#L7
54#[cfg_attr(target_arch = "s390x", repr(align(256)))]
55// x86 and wasm have 64-byte cache line size.
56//
57// Sources:
58// - https://github.com/golang/go/blob/dda2991c2ea0c5914714469c4defc2562a907230/src/internal/cpu/cpu_x86.go#L9
59// - https://github.com/golang/go/blob/3dd58676054223962cd915bb0934d1f9f489d4d2/src/internal/cpu/cpu_wasm.go#L7
60//
61// All others are assumed to have 64-byte cache line size.
62#[cfg_attr(
63 not(any(
64 target_arch = "x86_64",
65 target_arch = "aarch64",
66 target_arch = "powerpc64",
67 target_arch = "arm",
68 target_arch = "mips",
69 target_arch = "mips32r6",
70 target_arch = "mips64",
71 target_arch = "mips64r6",
72 target_arch = "riscv64",
73 target_arch = "s390x",
74 target_arch = "sw64",
75 )),
76 repr(align(64))
77)]
78pub struct CachePadded<T> {
79 value: T,
80}
81
82impl<T> CachePadded<T> {
83 /// Pads and aligns a value to the length of a cache line.
84 pub fn new(value: T) -> CachePadded<T> {
85 CachePadded::<T> { value }
86 }
87}
88
89impl<T> Deref for CachePadded<T> {
90 type Target = T;
91
92 fn deref(&self) -> &T {
93 &self.value
94 }
95}
96
97impl<T> DerefMut for CachePadded<T> {
98 fn deref_mut(&mut self) -> &mut T {
99 &mut self.value
100 }
101}
102
103const SPIN_LIMIT: u32 = 6;
104
105/// Performs quadratic backoff in spin loops.
106pub struct Backoff {
107 step: Cell<u32>,
108}
109
110impl Backoff {
111 /// Creates a new `Backoff`.
112 pub fn new() -> Self {
113 Backoff { step: Cell::new(0) }
114 }
115
116 /// Backs off using lightweight spinning.
117 ///
118 /// This method should be used for retrying an operation because another thread made
119 /// progress. i.e. on CAS failure.
120 #[inline]
121 pub fn spin_light(&self) {
122 let step = self.step.get().min(SPIN_LIMIT);
123 for _ in 0..step.pow(2) {
124 crate::hint::spin_loop();
125 }
126
127 self.step.set(self.step.get() + 1);
128 }
129
130 /// Backs off using heavyweight spinning.
131 ///
132 /// This method should be used in blocking loops where parking the thread is not an option.
133 #[inline]
134 pub fn spin_heavy(&self) {
135 if self.step.get() <= SPIN_LIMIT {
136 for _ in 0..self.step.get().pow(2) {
137 crate::hint::spin_loop()
138 }
139 } else {
140 crate::thread::yield_now();
141 }
142
143 self.step.set(self.step.get() + 1);
144 }
145}