Skip to content

Commit c5138c1

Browse files
committed
add atomic flag used for simulator target only
1 parent b9fab63 commit c5138c1

File tree

3 files changed

+33
-3
lines changed

3 files changed

+33
-3
lines changed

api/sys/src/build.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@ const USE_BUILT_BINDINGS: &str = "PD_BUILD_BINDINGS_ONCE";
3030
fn main() {
3131
println!("cargo:rerun-if-env-changed={SDK_PATH_ENV_VAR}");
3232

33+
println!("cargo::rustc-check-cfg=cfg(playdate)");
34+
if matches!(Target::from_env_target(), Ok(Target::Playdate)) {
35+
println!("cargo::rustc-cfg=playdate")
36+
}
37+
3338
let mut cfg = Cfg::default();
3439
cfg.derive.default = feature_derive_default();
3540
cfg.derive.eq = feature_derive_eq();

api/sys/src/lib.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,21 @@ pub extern "C" fn eventHandlerShim(api: *const ffi::PlaydateAPI,
7070
-> core::ffi::c_int {
7171
extern "Rust" {
7272
fn event_handler(api: *const ffi::PlaydateAPI, event: ffi::PDSystemEvent, arg: u32) -> EventLoopCtrl;
73+
74+
#[cfg(not(playdate))]
75+
// This is atomic in the `sys::proc::error`.
76+
static END_WITH_ERR: core::sync::atomic::AtomicBool;
7377
}
78+
7479
if let ffi::PDSystemEvent::kEventInit = event {
7580
unsafe { API = api }
7681
}
82+
83+
#[cfg(not(playdate))]
84+
if unsafe { END_WITH_ERR.load(core::sync::atomic::Ordering::Relaxed) } {
85+
return EventLoopCtrl::Stop.into();
86+
}
87+
7788
unsafe { event_handler(api, event, arg) }.into()
7889
}
7990

api/sys/src/sys/proc.rs

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,23 @@ pub fn abort() -> ! { core::intrinsics::abort() }
1212
/// In case of missed [`crate::sys::API`] (doesn't set) uses [`abort`].
1313
#[track_caller]
1414
pub fn error<S: AsRef<str>>(text: S) -> ! {
15+
#[cfg(not(playdate))]
16+
{
17+
use core::sync::atomic::AtomicBool;
18+
use core::sync::atomic::Ordering;
19+
20+
// This is unreachable on the device,
21+
// `API.system.error` interrupts the execution.
22+
// But simulator doesn't stops.
23+
// So, instead of spin-loop just save the flag and use later in the event-handler.
24+
25+
#[no_mangle]
26+
static END_WITH_ERR: AtomicBool = AtomicBool::new(false);
27+
28+
END_WITH_ERR.store(true, Ordering::Relaxed);
29+
}
30+
31+
1532
if let Some(f) = unsafe { (*(*crate::sys::API).system).error } {
1633
// Casting fn->void to fn->!
1734
// This ugly cast is safe because of `!` is a magic compile-time marker, not a type.
@@ -28,7 +45,4 @@ pub fn error<S: AsRef<str>>(text: S) -> ! {
2845
// In case of `crate::sys::API` is missed (doesn't set) just abort the process.
2946
abort()
3047
}
31-
// This is unreachable or the device,
32-
// `API.system.error` interrupts the execution.
33-
// But simulator doesn't stops on `error`.
3448
}

0 commit comments

Comments
 (0)