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
6466void _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;
0 commit comments