Unsafe code review request (casting between atomics and bytes)

I have code which allows to "pop" u32s from a list which is stored in file from different threads. For efficiency it implements prefetching of file data into internal buffers.

A very simplified version of the code can be found here:

It contains two unsafe parts and a bunch of atomic-based logic. IIUC the code is sound, but I would like to get an independent confirmation.

Note that the example is extremely simplified and taken out of context, so please do not recommend to mmap the file or Mutex everything. Casting of page data into atomics is also intentional and I know that with this example I could've used something like u32::from_ne_bytes((&page_data[pos..][..4]).try_into().unwrap()) to do it safely.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.