1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
use const_init::ConstInit;
use core::alloc::AllocErr;
use core::cell::UnsafeCell;
use core::ptr::NonNull;
use libc;
use memory_units::{Bytes, Pages};

pub(crate) fn alloc_pages(pages: Pages) -> Result<NonNull<u8>, AllocErr> {
    unsafe {
        let bytes: Bytes = pages.into();
        let addr = libc::mmap(
            0 as *mut _,
            bytes.0,
            libc::PROT_WRITE | libc::PROT_READ,
            libc::MAP_ANON | libc::MAP_PRIVATE,
            -1,
            0,
        );
        if addr == libc::MAP_FAILED {
            Err(AllocErr)
        } else {
            NonNull::new(addr as *mut u8).ok_or(AllocErr)
        }
    }
}

// Align to the cache line size on an i7 to prevent false sharing.
#[repr(align(64))]
pub(crate) struct Exclusive<T> {
    lock: UnsafeCell<libc::pthread_mutex_t>,
    inner: UnsafeCell<T>,
}

impl<T: ConstInit> ConstInit for Exclusive<T> {
    const INIT: Self = Exclusive {
        lock: UnsafeCell::new(libc::PTHREAD_MUTEX_INITIALIZER),
        inner: UnsafeCell::new(T::INIT),
    };
}

impl<T> Exclusive<T> {
    /// Get exclusive, mutable access to the inner value.
    ///
    /// # Safety
    ///
    /// Does not assert that `pthread`s calls return OK, unless the
    /// "extra_assertions" feature is enabled. This means that if `f` re-enters
    /// this method for the same `Exclusive` instance, there will be undetected
    /// mutable aliasing, which is UB.
    #[inline]
    pub(crate) unsafe fn with_exclusive_access<'a, F, U>(&'a self, f: F) -> U
    where
        for<'x> F: FnOnce(&'x mut T) -> U,
    {
        let code = libc::pthread_mutex_lock(&mut *self.lock.get());
        extra_assert_eq!(code, 0, "pthread_mutex_lock should run OK");

        let result = f(&mut *self.inner.get());

        let code = libc::pthread_mutex_unlock(&mut *self.lock.get());
        extra_assert_eq!(code, 0, "pthread_mutex_unlock should run OK");

        result
    }
}