Skip to content

Commit

Permalink
add atomic flag used for simulator target only
Browse files Browse the repository at this point in the history
  • Loading branch information
boozook committed Jun 4, 2024
1 parent b9fab63 commit c5138c1
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 3 deletions.
5 changes: 5 additions & 0 deletions api/sys/src/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ const USE_BUILT_BINDINGS: &str = "PD_BUILD_BINDINGS_ONCE";
fn main() {
println!("cargo:rerun-if-env-changed={SDK_PATH_ENV_VAR}");

println!("cargo::rustc-check-cfg=cfg(playdate)");
if matches!(Target::from_env_target(), Ok(Target::Playdate)) {
println!("cargo::rustc-cfg=playdate")
}

let mut cfg = Cfg::default();
cfg.derive.default = feature_derive_default();
cfg.derive.eq = feature_derive_eq();
Expand Down
11 changes: 11 additions & 0 deletions api/sys/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,21 @@ pub extern "C" fn eventHandlerShim(api: *const ffi::PlaydateAPI,
-> core::ffi::c_int {
extern "Rust" {
fn event_handler(api: *const ffi::PlaydateAPI, event: ffi::PDSystemEvent, arg: u32) -> EventLoopCtrl;

#[cfg(not(playdate))]
// This is atomic in the `sys::proc::error`.
static END_WITH_ERR: core::sync::atomic::AtomicBool;
}

if let ffi::PDSystemEvent::kEventInit = event {
unsafe { API = api }
}

#[cfg(not(playdate))]
if unsafe { END_WITH_ERR.load(core::sync::atomic::Ordering::Relaxed) } {
return EventLoopCtrl::Stop.into();
}

unsafe { event_handler(api, event, arg) }.into()
}

Expand Down
20 changes: 17 additions & 3 deletions api/sys/src/sys/proc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,23 @@ pub fn abort() -> ! { core::intrinsics::abort() }
/// In case of missed [`crate::sys::API`] (doesn't set) uses [`abort`].
#[track_caller]
pub fn error<S: AsRef<str>>(text: S) -> ! {
#[cfg(not(playdate))]
{
use core::sync::atomic::AtomicBool;
use core::sync::atomic::Ordering;

// This is unreachable on the device,
// `API.system.error` interrupts the execution.
// But simulator doesn't stops.
// So, instead of spin-loop just save the flag and use later in the event-handler.

#[no_mangle]
static END_WITH_ERR: AtomicBool = AtomicBool::new(false);

END_WITH_ERR.store(true, Ordering::Relaxed);
}


if let Some(f) = unsafe { (*(*crate::sys::API).system).error } {
// Casting fn->void to fn->!
// This ugly cast is safe because of `!` is a magic compile-time marker, not a type.
Expand All @@ -28,7 +45,4 @@ pub fn error<S: AsRef<str>>(text: S) -> ! {
// In case of `crate::sys::API` is missed (doesn't set) just abort the process.
abort()
}
// This is unreachable or the device,
// `API.system.error` interrupts the execution.
// But simulator doesn't stops on `error`.
}

0 comments on commit c5138c1

Please sign in to comment.