1use crate::{gc, private};
2
3pub trait GcToken: private::Sealed {}
4
5pub struct ThreadToken {
6 _non_send: std::marker::PhantomData<*const ()>,
7}
8
9impl private::Sealed for ThreadToken {}
10impl GcToken for ThreadToken {}
11
12impl ThreadToken {
13 pub(crate) fn new() -> Self {
14 Self {
15 _non_send: std::marker::PhantomData,
16 }
17 }
18
19 pub fn get() -> Self {
20 Self::try_get()
21 .expect("Thread is not registered with the GC. Please call init_thread() first.")
22 }
23
24 pub fn try_get() -> Option<Self> {
25 if crate::GC_REGISTERED.get() {
26 Some(ThreadToken {
27 _non_send: std::marker::PhantomData,
28 })
29 } else {
30 None
31 }
32 }
33}
34
35pub struct ThreadGuard {
36 _non_send: std::marker::PhantomData<*const ()>,
37}
38
39impl private::Sealed for ThreadGuard {}
40impl GcToken for ThreadGuard {}
41
42impl ThreadGuard {
43 pub(crate) fn new() -> Self {
44 Self {
45 _non_send: std::marker::PhantomData,
46 }
47 }
48}
49
50impl Drop for ThreadGuard {
51 fn drop(&mut self) {
52 unsafe { gc::GC_unregister_my_thread() };
53 }
54}