v / vlib / datatypes / lockfree / lockfree.v
23 lines · 19 sloc · 432 bytes · c216e59bfc85a863749963938d7131121114afd4
Raw
1module lockfree
2
3import sync.stdatomic as _
4
5// Define cache line size to prevent false sharing between CPU cores
6const cache_line_size = 64
7
8// next_power_of_two calculates the smallest power of two >= n
9@[inline]
10fn next_power_of_two(n u32) u32 {
11 if n == 0 {
12 return 1
13 }
14 mut x := n - 1
15
16 // Efficient bit manipulation to find next power of two
17 x |= x >> 1
18 x |= x >> 2
19 x |= x >> 4
20 x |= x >> 8
21 x |= x >> 16
22 return x + 1
23}
24