Skip to content

Commit 7d74c53

Browse files
committed
Incorporate review comment
1 parent ecbd5fa commit 7d74c53

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

system/lib/pthread/library_pthread.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,8 @@ int pthread_mutexattr_setprioceiling(pthread_mutexattr_t *attr, int prioceiling)
8484
static uint32_t dummyZeroAddress = 0;
8585

8686
void emscripten_thread_sleep(double msecs) {
87-
double target = emscripten_get_now() + msecs;
87+
double now = emscripten_get_now();
88+
double target = now + msecs;
8889

8990
// If we have less than this many msecs left to wait, busy spin that instead.
9091
double min_ms_slice_to_sleep = 0.1;
@@ -95,20 +96,21 @@ void emscripten_thread_sleep(double msecs) {
9596
emscripten_conditional_set_current_thread_status(
9697
EM_THREAD_STATUS_RUNNING, EM_THREAD_STATUS_SLEEPING);
9798

98-
double ms_to_sleep;
9999
do {
100100
// Keep processing the main loop of the calling thread.
101101
__pthread_testcancel(); // pthreads spec: sleep is a cancellation point, so must test if this
102102
// thread is cancelled during the sleep.
103103
emscripten_current_thread_process_queued_calls();
104104

105-
ms_to_sleep = target - emscripten_get_now();
105+
now = emscripten_get_now();
106+
double ms_to_sleep = target - now;
106107
if (ms_to_sleep < min_ms_slice_to_sleep)
107108
continue;
108109
if (ms_to_sleep > max_ms_slice_to_sleep)
109110
ms_to_sleep = max_ms_slice_to_sleep;
110111
emscripten_futex_wait(&dummyZeroAddress, 0, ms_to_sleep);
111-
} while (ms_to_sleep > 0);
112+
now = emscripten_get_now();
113+
} while (now < target);
112114

113115
emscripten_conditional_set_current_thread_status(
114116
EM_THREAD_STATUS_SLEEPING, EM_THREAD_STATUS_RUNNING);

0 commit comments

Comments
 (0)