Skip to content

Commit f580800

Browse files
committed
Minor cleanup of setitimer. NFC
These minor cleanups were split out from my larger work on precise wakeups.
1 parent 9a755c0 commit f580800

4 files changed

Lines changed: 15 additions & 9 deletions

File tree

system/lib/libc/musl/src/signal/setitimer.c

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
#ifdef __EMSCRIPTEN__
88
#include <emscripten/emscripten.h>
9+
#include <emscripten/threading.h>
10+
#include <assert.h>
911
#include <signal.h>
1012
#include <stdint.h>
1113
#include <stdio.h>
@@ -63,6 +65,9 @@ void _emscripten_timeout(int which, double now)
6365

6466
void _emscripten_check_timers(double now)
6567
{
68+
// Timers always run on the main runtime thread. They are registered with
69+
// _setitimer_js which is proxied to the main runtime thread.
70+
assert(emscripten_is_main_runtime_thread());
6671
for (int which = 0; which < 3; which++) {
6772
if (current_timeout_ms[which]) {
6873
// Only call out to JS to get the current time if it was not passed in
@@ -84,14 +89,16 @@ int setitimer(int which, const struct itimerval *restrict new, struct itimerval
8489
if (old) {
8590
__getitimer(which, old, now);
8691
}
92+
double timeout_ms = new->it_value.tv_sec * 1000 + new->it_value.tv_usec / 1000.0;
93+
double interval_ms = new->it_interval.tv_sec * 1000 + new->it_interval.tv_usec / 1000.0;
8794
if (new->it_value.tv_sec || new->it_value.tv_usec) {
88-
current_timeout_ms[which] = now + new->it_value.tv_sec * 1000 + new->it_value.tv_usec / 1000;
89-
current_intervals_ms[which] = new->it_interval.tv_sec * 1000 + new->it_interval.tv_usec / 1000;
95+
current_timeout_ms[which] = now + timeout_ms;
96+
current_intervals_ms[which] = interval_ms;
9097
} else {
9198
current_timeout_ms[which] = 0;
9299
current_intervals_ms[which] = 0;
93100
}
94-
return _setitimer_js(which, new->it_value.tv_sec * 1000 + new->it_value.tv_usec / 1000);
101+
return _setitimer_js(which, timeout_ms);
95102
#else
96103
if (sizeof(time_t) > sizeof(long)) {
97104
time_t is = new->it_interval.tv_sec, vs = new->it_value.tv_sec;

system/lib/pthread/emscripten_futex_wait.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include <emscripten/threading.h>
1414
#include <errno.h>
1515
#include <math.h>
16+
#include <stdatomic.h>
1617
#include <stdio.h>
1718
#include <stdlib.h>
1819
#include <sys/param.h>
@@ -111,7 +112,7 @@ static int futex_wait_main_browser_thread(volatile void* addr,
111112
// here and return, it's the same is if what happened on the main thread
112113
// was the same as calling _emscripten_yield()
113114
// a few times before calling emscripten_futex_wait().
114-
if (__c11_atomic_load((_Atomic uint32_t*)addr, __ATOMIC_SEQ_CST) != val) {
115+
if (atomic_load((_Atomic uint32_t*)addr) != val) {
115116
return -EWOULDBLOCK;
116117
}
117118

system/lib/pthread/emscripten_yield.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,12 @@ static void dummy(double now)
2020
weak_alias(dummy, _emscripten_check_timers);
2121

2222
void _emscripten_yield(double now) {
23-
int is_runtime_thread = emscripten_is_main_runtime_thread();
24-
2523
// When a secondary thread crashes, we need to be able to interrupt the main
2624
// thread even if it's in a blocking/looping on a mutex. We want to avoid
2725
// using the normal proxying mechanism to send this message since it can
2826
// allocate (or otherwise itself crash) so use a low level atomic primitive
2927
// for this signal.
30-
if (is_runtime_thread) {
28+
if (emscripten_is_main_runtime_thread()) {
3129
if (crashed_thread_id) {
3230
// Mark the crashed thread as strongly referenced so that Node.js doesn't
3331
// exit while the pthread is propagating the uncaught exception back to

test/codesize/test_codesize_hello_dylink_all.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"a.out.js": 244343,
3-
"a.out.nodebug.wasm": 577447,
4-
"total": 821790,
3+
"a.out.nodebug.wasm": 577432,
4+
"total": 821775,
55
"sent": [
66
"IMG_Init",
77
"IMG_Load",

0 commit comments

Comments
 (0)