Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not panic in Application::try_global if Application is not created #1996

Merged
merged 3 commits into from
Oct 3, 2021
Merged
Show file tree
Hide file tree
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ You can find its changes [documented below](#070---2021-01-01).
- RichText: Invalidate layout on Env change ([#1907] by [@Maan2003])
- GTK: fix using gdk before initialising it ([#1946] by [@JAicewizard])
- `ListIter` implementations for `Vector<T>` and `(S, Vector<T>)` ([#1967] by [@xarvic])
- Do not panic in Application::try_global if Application is not created ([#1996] by [@Maan2003])

### Visual

Expand Down Expand Up @@ -802,6 +803,7 @@ Last release without a changelog :(
[#1976]: https://github.com/linebender/druid/pull/1976
[#1978]: https://github.com/linebender/druid/pull/1978
[#1993]: https://github.com/linebender/druid/pull/1993
[#1996]: https://github.com/linebender/druid/pull/1996

[Unreleased]: https://github.com/linebender/druid/compare/v0.7.0...master
[0.7.0]: https://github.com/linebender/druid/compare/v0.6.0...v0.7.0
Expand Down
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
12 changes: 6 additions & 6 deletions druid-shell/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,15 @@ fn current_thread_id() -> u64 {
unsafe { mem::transmute(thread::current().id()) }
}

/// Assert that the current thread is the registered 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.
pub fn assert_main_thread() {
/// Panics when called from a non-main thread and main thread is claimed.
pub(crate) 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 {
if thread_id != main_thread_id && main_thread_id != 0 {
panic!(
"Main thread assertion failed {} != {}",
thread_id, main_thread_id
Expand All @@ -48,7 +48,7 @@ pub fn assert_main_thread() {
/// # Panics
///
/// Panics if the main thread has already been claimed by another thread.
pub fn claim_main_thread() {
pub(crate) fn claim_main_thread() {
let thread_id = current_thread_id();
let old_thread_id =
MAIN_THREAD_ID.compare_exchange(0, thread_id, Ordering::AcqRel, Ordering::Acquire);
Expand All @@ -70,7 +70,7 @@ pub fn claim_main_thread() {
/// # Panics
///
/// Panics if the main thread status is owned by another thread.
pub fn release_main_thread() {
pub(crate) fn release_main_thread() {
let thread_id = current_thread_id();
let old_thread_id =
MAIN_THREAD_ID.compare_exchange(thread_id, 0, Ordering::AcqRel, Ordering::Acquire);
Expand Down