Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 16 additions & 9 deletions sentry-core/src/hub_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,20 @@ use std::cell::{Cell, RefCell};
use std::marker::PhantomData;
use std::sync::{Arc, LazyLock, MutexGuard, PoisonError, RwLock};
use std::thread;
use std::thread::ThreadId;

use crate::Scope;
use crate::{scope::Stack, Client, Hub};

static PROCESS_HUB: LazyLock<(Arc<Hub>, thread::ThreadId)> = LazyLock::new(|| {
(
Arc::new(Hub::new(None, Arc::new(Default::default()))),
thread::current().id(),
)
static PROCESS_HUB: LazyLock<ProcessHub> = LazyLock::new(|| ProcessHub {
hub: Arc::new(Hub::new(None, Arc::new(Default::default()))),
thread: thread::current().id(),
});

thread_local! {
static THREAD_HUB: (RefCell<Arc<Hub>>, Cell<bool>) = (
RefCell::new(Arc::new(Hub::new_from_top(&PROCESS_HUB.0))),
Cell::new(PROCESS_HUB.1 == thread::current().id())
RefCell::new(Arc::new(Hub::new_from_top(&PROCESS_HUB.hub))),
Cell::new(PROCESS_HUB.thread == thread::current().id())
);
}

Expand Down Expand Up @@ -154,7 +153,7 @@ impl Hub {
/// This is similar to [`Hub::current`] but instead of picking the
/// current thread's hub it returns the main thread's hub instead.
pub fn main() -> Arc<Hub> {
PROCESS_HUB.0.clone()
PROCESS_HUB.hub.clone()
}

/// Invokes the callback with the default hub.
Expand All @@ -167,7 +166,7 @@ impl Hub {
{
THREAD_HUB.with(|(hub, is_process_hub)| {
if is_process_hub.get() {
f(&PROCESS_HUB.0)
f(&PROCESS_HUB.hub)
} else {
// Bind `hub` as `hub.borrow().clone()`.
// It is essential we drop `hub.borrow()` before the callback, otherwise we will
Expand Down Expand Up @@ -217,6 +216,14 @@ impl Hub {
}
}

/// Helper struct for storing the [`PROCESS_HUB`].
struct ProcessHub {
/// The process's main hub.
hub: Arc<Hub>,
/// The thread on which the main hub was initialized.
thread: ThreadId,
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
Loading