@@ -84,7 +84,8 @@ int pthread_mutexattr_setprioceiling(pthread_mutexattr_t *attr, int prioceiling)
84
84
static uint32_t dummyZeroAddress = 0 ;
85
85
86
86
void emscripten_thread_sleep (double msecs ) {
87
- double target = emscripten_get_now () + msecs ;
87
+ double now = emscripten_get_now ();
88
+ double target = now + msecs ;
88
89
89
90
// If we have less than this many msecs left to wait, busy spin that instead.
90
91
double min_ms_slice_to_sleep = 0.1 ;
@@ -95,20 +96,21 @@ void emscripten_thread_sleep(double msecs) {
95
96
emscripten_conditional_set_current_thread_status (
96
97
EM_THREAD_STATUS_RUNNING , EM_THREAD_STATUS_SLEEPING );
97
98
98
- double ms_to_sleep ;
99
99
do {
100
100
// Keep processing the main loop of the calling thread.
101
101
__pthread_testcancel (); // pthreads spec: sleep is a cancellation point, so must test if this
102
102
// thread is cancelled during the sleep.
103
103
emscripten_current_thread_process_queued_calls ();
104
104
105
- ms_to_sleep = target - emscripten_get_now ();
105
+ now = emscripten_get_now ();
106
+ double ms_to_sleep = target - now ;
106
107
if (ms_to_sleep < min_ms_slice_to_sleep )
107
108
continue ;
108
109
if (ms_to_sleep > max_ms_slice_to_sleep )
109
110
ms_to_sleep = max_ms_slice_to_sleep ;
110
111
emscripten_futex_wait (& dummyZeroAddress , 0 , ms_to_sleep );
111
- } while (ms_to_sleep > 0 );
112
+ now = emscripten_get_now ();
113
+ } while (now < target );
112
114
113
115
emscripten_conditional_set_current_thread_status (
114
116
EM_THREAD_STATUS_SLEEPING , EM_THREAD_STATUS_RUNNING );
0 commit comments