omango_util/cache_padded.rs
1// Copyright (c) 2024 Trung Tran <tqtrungse@gmail.com>
2//
3// Permission is hereby granted, free of charge, to any person obtaining a copy
4// of this software and associated documentation files (the "Software"), to deal
5// in the Software without restriction, including without limitation the rights
6// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7// copies of the Software, and to permit persons to whom the Software is
8// furnished to do so, subject to the following conditions:
9//
10// The above copyright notice and this permission notice shall be included in all
11// copies or substantial portions of the Software.
12//
13// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19// SOFTWARE.
20
21//! This file was re-modified based on the following program in Crossbeam-Utils.
22//!
23//! Source: `<https://github.com/crossbeam-rs/crossbeam/blob/master/crossbeam-utils/src/cache_padded.rs>`
24//!
25//! Copyright & License:
26//! - The Crossbeam Project Developers
27//! - The MIT License (MIT) or Apache License 2.0
28//! '<https://opensource.org/licenses/MIT>'
29//! '<https://www.apache.org/licenses/LICENSE-2.0>'
30
31use core::ops::{Deref, DerefMut};
32
33// Starting from Intel's Sandy Bridge, spatial prefetch is now pulling pairs of 64-byte cache
34// lines at a time, so we have to align to 128 bytes rather than 64.
35//
36// Sources:
37// - https://www.intel.com/content/dam/www/public/us/en/documents/manuals/64-ia-32-architectures-optimization-manual.pdf
38// - https://github.com/facebook/folly/blob/1b5288e6eea6df074758f877c849b6e73bbb9fbb/folly/lang/Align.h#L107
39//
40// ARM's big.LITTLE architecture has asymmetric cores and "big" cores have 128-byte cache line size.
41//
42// Sources:
43// - https://www.mono-project.com/news/2016/09/12/arm64-icache/
44//
45// powerpc64 has 128-byte cache line size.
46//
47// Sources:
48// - https://github.com/golang/go/blob/3dd58676054223962cd915bb0934d1f9f489d4d2/src/internal/cpu/cpu_ppc64x.go#L9
49// - https://github.com/torvalds/linux/blob/3516bd729358a2a9b090c1905bd2a3fa926e24c6/arch/powerpc/include/asm/cache.h#L26
50#[cfg_attr(
51 any(
52 target_arch = "x86_64",
53 target_arch = "aarch64",
54 target_arch = "powerpc64",
55 ),
56 repr(align(128))
57)]
58
59// arm, mips, mips64, sparc, and hexagon have 32-byte cache line size.
60//
61// Sources:
62// - https://github.com/golang/go/blob/3dd58676054223962cd915bb0934d1f9f489d4d2/src/internal/cpu/cpu_arm.go#L7
63// - https://github.com/golang/go/blob/3dd58676054223962cd915bb0934d1f9f489d4d2/src/internal/cpu/cpu_mips.go#L7
64// - https://github.com/golang/go/blob/3dd58676054223962cd915bb0934d1f9f489d4d2/src/internal/cpu/cpu_mipsle.go#L7
65// - https://github.com/golang/go/blob/3dd58676054223962cd915bb0934d1f9f489d4d2/src/internal/cpu/cpu_mips64x.go#L9
66// - https://github.com/torvalds/linux/blob/3516bd729358a2a9b090c1905bd2a3fa926e24c6/arch/sparc/include/asm/cache.h#L17
67// - https://github.com/torvalds/linux/blob/3516bd729358a2a9b090c1905bd2a3fa926e24c6/arch/hexagon/include/asm/cache.h#L12
68#[cfg_attr(
69 any(
70 target_arch = "arm",
71 target_arch = "mips",
72 target_arch = "mips32r6",
73 target_arch = "mips64",
74 target_arch = "mips64r6",
75 target_arch = "sparc",
76 target_arch = "hexagon",
77 ),
78 repr(align(32))
79)]
80
81// m68k has 16-byte cache line size.
82//
83// Sources:
84// - https://github.com/torvalds/linux/blob/3516bd729358a2a9b090c1905bd2a3fa926e24c6/arch/m68k/include/asm/cache.h#L9
85#[cfg_attr(target_arch = "m68k", repr(align(16)))]
86// s390x has 256-byte cache line size.
87//
88// Sources:
89// - https://github.com/golang/go/blob/3dd58676054223962cd915bb0934d1f9f489d4d2/src/internal/cpu/cpu_s390x.go#L7
90// - https://github.com/torvalds/linux/blob/3516bd729358a2a9b090c1905bd2a3fa926e24c6/arch/s390/include/asm/cache.h#L13
91#[cfg_attr(target_arch = "s390x", repr(align(256)))]
92
93// x86, wasm, riscv, and sparc64 have 64-byte cache line size.
94//
95// Sources:
96// - https://github.com/golang/go/blob/dda2991c2ea0c5914714469c4defc2562a907230/src/internal/cpu/cpu_x86.go#L9
97// - https://github.com/golang/go/blob/3dd58676054223962cd915bb0934d1f9f489d4d2/src/internal/cpu/cpu_wasm.go#L7
98// - https://github.com/torvalds/linux/blob/3516bd729358a2a9b090c1905bd2a3fa926e24c6/arch/riscv/include/asm/cache.h#L10
99// - https://github.com/torvalds/linux/blob/3516bd729358a2a9b090c1905bd2a3fa926e24c6/arch/sparc/include/asm/cache.h#L19
100//
101// All others are assumed to have 64-byte cache line size.
102#[cfg_attr(
103 not(any(
104 target_arch = "x86_64",
105 target_arch = "aarch64",
106 target_arch = "powerpc64",
107 target_arch = "arm",
108 target_arch = "mips",
109 target_arch = "mips32r6",
110 target_arch = "mips64",
111 target_arch = "mips64r6",
112 target_arch = "sparc",
113 target_arch = "hexagon",
114 target_arch = "m68k",
115 target_arch = "s390x",
116 )),
117 repr(align(64))
118)]
119/// Aligns an object size to CPU cache line size to prevent false-sharing.
120pub struct CachePadded<T> {
121 value: T,
122}
123
124unsafe impl<T: Send> Send for CachePadded<T> {}
125
126unsafe impl<T: Sync> Sync for CachePadded<T> {}
127
128impl<T> CachePadded<T> {
129 /// Create an object is aligned to CPU cache line size.
130 #[inline]
131 pub const fn new(t: T) -> CachePadded<T> {
132 CachePadded::<T> { value: t }
133 }
134}
135
136impl<T> Deref for CachePadded<T> {
137 type Target = T;
138
139 fn deref(&self) -> &T {
140 &self.value
141 }
142}
143
144impl<T> DerefMut for CachePadded<T> {
145 fn deref_mut(&mut self) -> &mut T {
146 &mut self.value
147 }
148}