sentry_core/
hub_impl.rs

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
24/// A Hub switch guard used to temporarily swap
25/// active hub in thread local storage.
26pub struct SwitchGuard {
27    inner: Option<(Arc<Hub>, bool)>,
28}
29
30impl SwitchGuard {
31    /// Swaps the current thread's Hub by the one provided
32    /// and returns a guard that, when dropped, replaces it
33    /// to the previous one.
34    pub fn new(mut hub: Arc<Hub>) -> Self {
35        let inner = THREAD_HUB.with(|(thread_hub, is_process_hub)| {
36            // SAFETY: `thread_hub` will always be a valid thread local hub,
37            // by definition not shared between threads.
38            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    /// Creates a new hub from the given client and scope.
99    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    /// Creates a new hub based on the top scope of the given hub.
109    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    /// Returns the current, thread-local hub.
118    ///
119    /// Invoking this will return the current thread-local hub.  The first
120    /// time it is called on a thread, a new thread-local hub will be
121    /// created based on the topmost scope of the hub on the main thread as
122    /// returned by [`Hub::main`].  If the main thread did not yet have a
123    /// hub it will be created when invoking this function.
124    ///
125    /// To have control over which hub is installed as the current
126    /// thread-local hub, use [`Hub::run`].
127    ///
128    /// This method is unavailable if the client implementation is disabled.
129    /// When using the minimal API set use [`Hub::with_active`] instead.
130    pub fn current() -> Arc<Hub> {
131        Hub::with(Arc::clone)
132    }
133
134    /// Returns the main thread's hub.
135    ///
136    /// This is similar to [`Hub::current`] but instead of picking the
137    /// current thread's hub it returns the main thread's hub instead.
138    pub fn main() -> Arc<Hub> {
139        PROCESS_HUB.0.clone()
140    }
141
142    /// Invokes the callback with the default hub.
143    ///
144    /// This is a slightly more efficient version than [`Hub::current`] and
145    /// also unavailable in minimal mode.
146    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    /// Binds a hub to the current thread for the duration of the call.
160    ///
161    /// During the execution of `f` the given hub will be installed as the
162    /// thread-local hub.  So any call to [`Hub::current`] during this time
163    /// will return the provided hub.
164    ///
165    /// Once the function is finished executing, including after it
166    /// panicked, the original hub is re-installed if one was present.
167    pub fn run<F: FnOnce() -> R, R>(hub: Arc<Hub>, f: F) -> R {
168        let _guard = SwitchGuard::new(hub);
169        f()
170    }
171
172    /// Returns the currently bound client.
173    pub fn client(&self) -> Option<Arc<Client>> {
174        self.inner.with(|stack| stack.top().client.clone())
175    }
176
177    /// Binds a new client to the hub.
178    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}