| 1 | module lockfree |
| 2 | |
| 3 | import sync.stdatomic as _ |
| 4 | |
| 5 | // Define cache line size to prevent false sharing between CPU cores |
| 6 | const cache_line_size = 64 |
| 7 | |
| 8 | // next_power_of_two calculates the smallest power of two >= n |
| 9 | @[inline] |
| 10 | fn 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 |