1
use std::cell::UnsafeCell;
2
use std::mem::ManuallyDrop;
3
use std::ops::Deref;
4
use std::ops::DerefMut;
5

            
6
use crate::storage::GlobalTermPoolGuard;
7
use crate::storage::THREAD_TERM_POOL;
8

            
9
/// Global mutex that prevents garbage collection.
10
pub struct GcMutex<T> {
11
    inner: UnsafeCell<T>,
12
}
13

            
14
unsafe impl<T: Send> Send for GcMutex<T> {}
15
unsafe impl<T: Send> Sync for GcMutex<T> {}
16

            
17
impl<T> GcMutex<T> {
18
3324656
    pub fn new(value: T) -> GcMutex<T> {
19
3324656
        GcMutex {
20
3324656
            inner: UnsafeCell::new(value),
21
3324656
        }
22
3324656
    }
23

            
24
    /// Provides mutable access to the underlying value.
25
39829621
    pub fn write(&self) -> GcMutexGuard<'_, T> {
26
        GcMutexGuard {
27
39829621
            mutex: self,
28
39829621
            guard: ManuallyDrop::new(THREAD_TERM_POOL.with_borrow(|tp| unsafe {
29
39829621
                std::mem::transmute(tp.term_pool().read_recursive().expect("Lock poisoned!"))
30
39829621
            })),
31
        }
32
39829621
    }
33

            
34
    /// Provides immutable access to the underlying value.
35
25651370
    pub fn read(&self) -> GcMutexGuard<'_, T> {
36
        GcMutexGuard {
37
25651370
            mutex: self,
38
25651370
            guard: ManuallyDrop::new(THREAD_TERM_POOL.with_borrow(|tp| unsafe {
39
25651370
                std::mem::transmute(tp.term_pool().read_recursive().expect("Lock poisoned!"))
40
25651370
            })),
41
        }
42
25651370
    }
43
}
44

            
45
pub struct GcMutexGuard<'a, T> {
46
    mutex: &'a GcMutex<T>,
47

            
48
    /// Only used to avoid garbage collection, will be released on drop.
49
    guard: ManuallyDrop<GlobalTermPoolGuard<'a>>,
50
}
51

            
52
impl<T> Deref for GcMutexGuard<'_, T> {
53
    type Target = T;
54

            
55
290540805
    fn deref(&self) -> &Self::Target {
56
290540805
        unsafe { &*self.mutex.inner.get() }
57
290540805
    }
58
}
59

            
60
impl<T> DerefMut for GcMutexGuard<'_, T> {
61
60533086
    fn deref_mut(&mut self) -> &mut Self::Target {
62
        // We are the only guard after `write()`, so we can provide mutable access to the underlying object.
63
60533086
        unsafe { &mut *self.mutex.inner.get() }
64
60533086
    }
65
}
66

            
67
impl<T> Drop for GcMutexGuard<'_, T> {
68
65480991
    fn drop(&mut self) {
69
65480991
        if self.guard.read_depth() == 1 {
70
            // If this is the last guard, we can trigger garbage collection when it was delayed earlier.
71
26098890
            THREAD_TERM_POOL.with_borrow(|tp| unsafe { tp.trigger_delayed_garbage_collection(&mut self.guard) })
72
39382101
        } else {
73
39382101
            // Just drop the guard
74
39382101
            unsafe { ManuallyDrop::drop(&mut self.guard) };
75
39382101
        }
76
65480991
    }
77
}