Skip to content

Commit

Permalink
Do not panic in Application::try_global if main thread is unclaimed
Browse files Browse the repository at this point in the history
`Application::try_global` called `assert_main_thread` but before
Application is running the main thread is unclaimed. I think there was
no way that `Application::try_global` to return None.

Added `assert_main_thread_or_main_unclaimed` that does not panic
if main thread is unclaimed.
  • Loading branch information
maan2003 committed Oct 1, 2021
1 parent 90a7ab9 commit 193b30f
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
2 changes: 1 addition & 1 deletion druid-shell/src/application.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ impl Application {
/// [`new`]: #method.new
/// [`run`]: #method.run
pub fn try_global() -> Option<Application> {
util::assert_main_thread();
util::assert_main_thread_or_main_unclaimed();
GLOBAL_APP.with(|global_app| global_app.borrow().clone())
}

Expand Down
17 changes: 17 additions & 0 deletions druid-shell/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ fn current_thread_id() -> u64 {
/// # Panics
///
/// Panics when called from a non-main thread.
#[allow(dead_code)]
pub fn assert_main_thread() {
let thread_id = current_thread_id();
let main_thread_id = MAIN_THREAD_ID.load(Ordering::Acquire);
Expand All @@ -43,6 +44,22 @@ pub fn assert_main_thread() {
}
}

/// Assert that the current thread is the registered main thread or main thread is not claimed.
///
/// # Panics
///
/// Panics when called from a non-main thread and main thread is claimed.
pub fn assert_main_thread_or_main_unclaimed() {
let thread_id = current_thread_id();
let main_thread_id = MAIN_THREAD_ID.load(Ordering::Acquire);
if thread_id != main_thread_id && main_thread_id != 0 {
panic!(
"Main thread assertion failed {} != {}",
thread_id, main_thread_id
);
}
}

/// Register the current thread as the main thread.
///
/// # Panics
Expand Down

0 comments on commit 193b30f

Please sign in to comment.