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
66
67
68
69
70
71
72
73
74
75
76
77
78
use crate::collector::RawCollectorImpl;
use crate::{ContextState, ShadowStack, CollectorRef};
use std::mem::ManuallyDrop;
use std::fmt::Debug;

/// The internal state of the collector
///
/// Has a thread-safe and thread-unsafe implementation.

#[cfg(feature = "sync")]
pub mod sync;
pub mod nosync;

/// Manages coordination of garbage collections
pub unsafe trait CollectionManager<C>: self::sealed::Sealed
    where C: RawCollectorImpl<Manager=Self, RawContext=Self::Context> {
    type Context: RawContext<C>;
    fn new() -> Self;
    fn is_collecting(&self) -> bool;
    fn should_trigger_collection(&self) -> bool;
    unsafe fn freeze_context(&self, context: &Self::Context);
    unsafe fn unfreeze_context(&self, context: &Self::Context);

    //
    // Extension methods on collector
    //

    /// Attempt to prevent garbage collection for the duration of the closure
    ///
    /// This method is **OPTIONAL** and will panic if unimplemented.
    fn prevent_collection<R>(collector: &C, func: impl FnOnce() -> R) -> R;

    /// Free the specified context
    ///
    /// ## Safety
    /// - Assumes the specified pointer is valid
    /// - Assumes there are no more outstanding borrows to values in the context
    unsafe fn free_context(collector: &C, context: *mut Self::Context);
}

/// The underlying state of a context
///
/// Each context is bound to one and only one thread,
/// even if the collector supports multi-threading.
pub unsafe trait RawContext<C>: Debug + self::sealed::Sealed
    where C: RawCollectorImpl<RawContext=Self> {
    unsafe fn register_new(collector: &CollectorRef<C>) -> ManuallyDrop<Box<Self>>;
    /// Trigger a safepoint for this context.
    ///
    /// This implicitly attempts a collection,
    /// potentially blocking until completion..
    ///
    /// Undefined behavior if mutated during collection
    unsafe fn trigger_safepoint(&self);
    /// Borrow a reference to the shadow stack,
    /// assuming this context is valid (not active).
    ///
    /// A context is valid if it is either frozen
    /// or paused at a safepoint.
    #[inline]
    unsafe fn assume_valid_shadow_stack(&self) -> &ShadowStack<C> {
        match self.state() {
            ContextState::Active => unreachable!("active context: {:?}", self),
            ContextState::SafePoint { .. } | ContextState::Frozen { .. } => {},
        }
        &*self.shadow_stack_ptr()
    }
    /// Get a pointer to the shadow stack
    fn shadow_stack_ptr(&self) -> *mut ShadowStack<C>;
    /// Get a reference to the collector,
    /// assuming that it's valid
    unsafe fn collector(&self) -> &C;
    fn state(&self) -> ContextState;
}

mod sealed {
    pub trait Sealed {}
}