diff --git a/src/core/capi.c b/src/core/capi.c index 498ffa97..0f82910b 100644 --- a/src/core/capi.c +++ b/src/core/capi.c @@ -509,6 +509,14 @@ JanetAtomicInt janet_atomic_dec(JanetAtomicInt volatile *x) { #endif } +JanetAtomicInt janet_atomic_load(JanetAtomicInt volatile *x) { +#ifdef JANET_WINDOWS + return InterlockedOr(x, 0); +#else + return __atomic_load_n(x, __ATOMIC_ACQUIRE); +#endif +} + /* Some definitions for function-like macros */ JANET_API JanetStructHead *(janet_struct_head)(JanetStruct st) { diff --git a/src/core/ev.c b/src/core/ev.c index 2523809a..0fe1bf7f 100644 --- a/src/core/ev.c +++ b/src/core/ev.c @@ -1284,7 +1284,7 @@ void janet_loop1_impl(int has_timeout, JanetTimestamp timeout); int janet_loop_done(void) { return !((janet_vm.spawn.head != janet_vm.spawn.tail) || janet_vm.tq_count || - janet_vm.listener_count); + janet_atomic_load(&janet_vm.listener_count)); } JanetFiber *janet_loop1(void) { @@ -1346,7 +1346,7 @@ JanetFiber *janet_loop1(void) { } /* Poll for events */ - if (janet_vm.tq_count || janet_vm.listener_count) { + if (janet_vm.tq_count || janet_atomic_load(&janet_vm.listener_count)) { JanetTimeout to; memset(&to, 0, sizeof(to)); int has_timeout; @@ -1365,7 +1365,7 @@ JanetFiber *janet_loop1(void) { break; } /* Run polling implementation only if pending timeouts or pending events */ - if (janet_vm.tq_count || janet_vm.listener_count) { + if (janet_vm.tq_count || janet_atomic_load(&janet_vm.listener_count)) { janet_loop1_impl(has_timeout, to.when); } } diff --git a/src/include/janet.h b/src/include/janet.h index ea7d34db..5724af2b 100644 --- a/src/include/janet.h +++ b/src/include/janet.h @@ -647,6 +647,7 @@ typedef int32_t JanetAtomicInt; #endif JANET_API JanetAtomicInt janet_atomic_inc(JanetAtomicInt volatile *x); JANET_API JanetAtomicInt janet_atomic_dec(JanetAtomicInt volatile *x); +JANET_API JanetAtomicInt janet_atomic_load(JanetAtomicInt volatile *x); /* We provide three possible implementations of Janets. The preferred * nanboxing approach, for 32 or 64 bits, and the standard C version. Code in the rest of the