1use std::cell::{Cell, UnsafeCell};
2use std::sync::{Arc, PoisonError, RwLock};
3use std::thread;
4
5use crate::Scope;
6use crate::{scope::Stack, Client, Hub};
7
8use once_cell::sync::Lazy;
9
10static PROCESS_HUB: Lazy<(Arc<Hub>, thread::ThreadId)> = Lazy::new(|| {
11 (
12 Arc::new(Hub::new(None, Arc::new(Default::default()))),
13 thread::current().id(),
14 )
15});
16
17thread_local! {
18 static THREAD_HUB: (UnsafeCell<Arc<Hub>>, Cell<bool>) = (
19 UnsafeCell::new(Arc::new(Hub::new_from_top(&PROCESS_HUB.0))),
20 Cell::new(PROCESS_HUB.1 == thread::current().id())
21 );
22}
23
24pub struct SwitchGuard {
27 inner: Option<(Arc<Hub>, bool)>,
28}
29
30impl SwitchGuard {
31 pub fn new(mut hub: Arc<Hub>) -> Self {
35 let inner = THREAD_HUB.with(|(thread_hub, is_process_hub)| {
36 let thread_hub = unsafe { &mut *thread_hub.get() };
39 if std::ptr::eq(thread_hub.as_ref(), hub.as_ref()) {
40 return None;
41 }
42 std::mem::swap(thread_hub, &mut hub);
43 let was_process_hub = is_process_hub.replace(false);
44 Some((hub, was_process_hub))
45 });
46 SwitchGuard { inner }
47 }
48
49 fn swap(&mut self) -> Option<Arc<Hub>> {
50 if let Some((mut hub, was_process_hub)) = self.inner.take() {
51 Some(THREAD_HUB.with(|(thread_hub, is_process_hub)| {
52 let thread_hub = unsafe { &mut *thread_hub.get() };
53 std::mem::swap(thread_hub, &mut hub);
54 if was_process_hub {
55 is_process_hub.set(true);
56 }
57 hub
58 }))
59 } else {
60 None
61 }
62 }
63}
64
65impl Drop for SwitchGuard {
66 fn drop(&mut self) {
67 let _ = self.swap();
68 }
69}
70
71#[derive(Debug)]
72pub(crate) struct HubImpl {
73 pub(crate) stack: Arc<RwLock<Stack>>,
74}
75
76impl HubImpl {
77 pub(crate) fn with<F: FnOnce(&Stack) -> R, R>(&self, f: F) -> R {
78 let guard = self.stack.read().unwrap_or_else(PoisonError::into_inner);
79 f(&guard)
80 }
81
82 pub(crate) fn with_mut<F: FnOnce(&mut Stack) -> R, R>(&self, f: F) -> R {
83 let mut guard = self.stack.write().unwrap_or_else(PoisonError::into_inner);
84 f(&mut guard)
85 }
86
87 pub(crate) fn is_active_and_usage_safe(&self) -> bool {
88 let guard = match self.stack.read() {
89 Err(err) => err.into_inner(),
90 Ok(guard) => guard,
91 };
92
93 guard.top().client.as_ref().is_some_and(|c| c.is_enabled())
94 }
95}
96
97impl Hub {
98 pub fn new(client: Option<Arc<Client>>, scope: Arc<Scope>) -> Hub {
100 Hub {
101 inner: HubImpl {
102 stack: Arc::new(RwLock::new(Stack::from_client_and_scope(client, scope))),
103 },
104 last_event_id: RwLock::new(None),
105 }
106 }
107
108 pub fn new_from_top<H: AsRef<Hub>>(other: H) -> Hub {
110 let hub = other.as_ref();
111 hub.inner.with(|stack| {
112 let top = stack.top();
113 Hub::new(top.client.clone(), top.scope.clone())
114 })
115 }
116
117 pub fn current() -> Arc<Hub> {
131 Hub::with(Arc::clone)
132 }
133
134 pub fn main() -> Arc<Hub> {
139 PROCESS_HUB.0.clone()
140 }
141
142 pub fn with<F, R>(f: F) -> R
147 where
148 F: FnOnce(&Arc<Hub>) -> R,
149 {
150 THREAD_HUB.with(|(hub, is_process_hub)| {
151 if is_process_hub.get() {
152 f(&PROCESS_HUB.0)
153 } else {
154 f(unsafe { &*hub.get() })
155 }
156 })
157 }
158
159 pub fn run<F: FnOnce() -> R, R>(hub: Arc<Hub>, f: F) -> R {
168 let _guard = SwitchGuard::new(hub);
169 f()
170 }
171
172 pub fn client(&self) -> Option<Arc<Client>> {
174 self.inner.with(|stack| stack.top().client.clone())
175 }
176
177 pub fn bind_client(&self, client: Option<Arc<Client>>) {
179 self.inner.with_mut(|stack| {
180 stack.top_mut().client = client;
181 })
182 }
183
184 pub(crate) fn is_active_and_usage_safe(&self) -> bool {
185 self.inner.is_active_and_usage_safe()
186 }
187
188 pub(crate) fn with_current_scope<F: FnOnce(&Scope) -> R, R>(&self, f: F) -> R {
189 self.inner.with(|stack| f(&stack.top().scope))
190 }
191
192 pub(crate) fn with_current_scope_mut<F: FnOnce(&mut Scope) -> R, R>(&self, f: F) -> R {
193 self.inner
194 .with_mut(|stack| f(Arc::make_mut(&mut stack.top_mut().scope)))
195 }
196}