Skip to content

Commit

Permalink
Fixing Gooey compilation on MacOS
Browse files Browse the repository at this point in the history
After trying to run Gooey again on my Mac for the first time in a few
weeks, I found that I ran into the Condvar issue again. Rather than
pasting AssertUnwindSafe in those files, I've both reported the
discrepency in unwind safety (rust-lang/rust#118009) and moved the
workaround into a type that only uses AssertUnwindsafe when compiling
for Apple.
  • Loading branch information
ecton committed Nov 17, 2023
1 parent c39f8f3 commit 01d45a8
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 16 deletions.
6 changes: 3 additions & 3 deletions src/animation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ use std::fmt::{Debug, Display};
use std::ops::{ControlFlow, Deref, Div, Mul};
use std::panic::{RefUnwindSafe, UnwindSafe};
use std::str::FromStr;
use std::sync::{Arc, Condvar, Mutex, MutexGuard, OnceLock};
use std::sync::{Arc, Mutex, MutexGuard, OnceLock};
use std::thread;
use std::time::{Duration, Instant};

Expand All @@ -57,11 +57,11 @@ use kludgine::Color;

use crate::animation::easings::Linear;
use crate::styles::{Component, RequireInvalidation};
use crate::utils::IgnorePoison;
use crate::utils::{IgnorePoison, UnwindsafeCondvar};
use crate::value::Dynamic;

static ANIMATIONS: Mutex<Animating> = Mutex::new(Animating::new());
static NEW_ANIMATIONS: Condvar = Condvar::new();
static NEW_ANIMATIONS: UnwindsafeCondvar = UnwindsafeCondvar::new();

fn thread_state() -> MutexGuard<'static, Animating> {
static THREAD: OnceLock<()> = OnceLock::new();
Expand Down
8 changes: 4 additions & 4 deletions src/tick.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
use std::collections::HashSet;
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::{Arc, Condvar, Mutex, MutexGuard};
use std::sync::{Arc, Mutex, MutexGuard};
use std::time::{Duration, Instant};

use kludgine::app::winit::event::KeyEvent;
use kludgine::app::winit::keyboard::Key;

use crate::context::WidgetContext;
use crate::utils::IgnorePoison;
use crate::utils::{IgnorePoison, UnwindsafeCondvar};
use crate::value::Dynamic;
use crate::widget::{EventHandling, HANDLED, IGNORED};

Expand Down Expand Up @@ -65,7 +65,7 @@ impl Tick {
input: InputState::default(),
}),
period: tick_every,
sync: Condvar::new(),
sync: UnwindsafeCondvar::new(),
rendered_frame: AtomicUsize::new(0),
tick_number: Dynamic::default(),
});
Expand Down Expand Up @@ -117,7 +117,7 @@ pub struct InputState {
struct TickData {
state: Mutex<TickState>,
period: Duration,
sync: Condvar,
sync: UnwindsafeCondvar,
rendered_frame: AtomicUsize,
tick_number: Dynamic<u64>,
}
Expand Down
33 changes: 32 additions & 1 deletion src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,40 @@
use std::ops::Deref;
use std::sync::{OnceLock, PoisonError};
use std::sync::{Condvar, OnceLock, PoisonError};

use kludgine::app::winit::event::Modifiers;
use kludgine::app::winit::keyboard::ModifiersState;

/// This [`Condvar`] is a wrapper that on Mac OS/iOS asserts unwind safety. On
/// all other platforms, this is a transparent wrapper around `Condvar`. See
/// <https://github.com/rust-lang/rust/issues/118009> for more information.
#[derive(Debug, Default)]
pub struct UnwindsafeCondvar(
#[cfg(any(target_os = "ios", target_os = "macos"))] std::panic::AssertUnwindSafe<Condvar>,
#[cfg(not(any(target_os = "ios", target_os = "macos")))] Condvar,
);

impl Deref for UnwindsafeCondvar {
type Target = Condvar;

fn deref(&self) -> &Self::Target {
&self.0
}
}

impl UnwindsafeCondvar {
pub const fn new() -> Self {
#[cfg(any(target_os = "ios", target_os = "macos"))]
{
Self(AssertUnwindSafe(Condvar::new()))
}

#[cfg(not(any(target_os = "ios", target_os = "macos")))]
{
Self(Condvar::new())
}
}
}

/// Invokes the provided macro with a pattern that can be matched using this
/// `macro_rules!` expression: `$($type:ident $field:tt $var:ident),+`, where `$type` is an
/// identifier to use for the generic parameter and `$field` is the field index
Expand Down
12 changes: 4 additions & 8 deletions src/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@ use std::cell::Cell;
use std::fmt::{Debug, Display};
use std::future::Future;
use std::ops::{Deref, DerefMut};
use std::panic::AssertUnwindSafe;
use std::str::FromStr;
use std::sync::{Arc, Condvar, Mutex, MutexGuard, TryLockError};
use std::sync::{Arc, Mutex, MutexGuard, TryLockError};
use std::task::{Poll, Waker};
use std::thread::ThreadId;

Expand All @@ -15,7 +14,7 @@ use intentional::Assert;

use crate::animation::{DynamicTransition, LinearInterpolate};
use crate::context::{WidgetContext, WindowHandle};
use crate::utils::{IgnorePoison, WithClone};
use crate::utils::{IgnorePoison, UnwindsafeCondvar, WithClone};
use crate::widget::{WidgetId, WidgetInstance};
use crate::widgets::Switcher;

Expand All @@ -40,7 +39,7 @@ impl<T> Dynamic<T> {
widgets: AHashSet::new(),
}),
during_callback_state: Mutex::default(),
sync: AssertUnwindSafe(Condvar::new()),
sync: UnwindsafeCondvar::default(),
}))
}

Expand Down Expand Up @@ -553,10 +552,7 @@ struct LockState {
struct DynamicData<T> {
state: Mutex<State<T>>,
during_callback_state: Mutex<Option<LockState>>,

// The AssertUnwindSafe is only needed on Mac. For some reason on
// Mac OS, Condvar isn't RefUnwindSafe.
sync: AssertUnwindSafe<Condvar>,
sync: UnwindsafeCondvar,
}

impl<T> DynamicData<T> {
Expand Down

0 comments on commit 01d45a8

Please sign in to comment.