Skip to content

Commit 72fa23a

Browse files
committed
Remove loop from emscripten_thread_sleep. NFC
We have code in the lower level emscrpeten_futex_wait that take care of splitting up the wait for the main runtime thread. For normal pthreads I don't see any need to run the message queue while the thread is sleeping like this. Instead, it can run from the event loop, or explicitly run. Followup to emscripten-core#26471
1 parent f8e6e4b commit 72fa23a

1 file changed

Lines changed: 12 additions & 37 deletions

File tree

system/lib/pthread/library_pthread.c

Lines changed: 12 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -65,54 +65,29 @@ int sched_get_priority_min(int policy) {
6565
return 0;
6666
}
6767

68-
int pthread_mutexattr_getprioceiling(const pthread_mutexattr_t *restrict attr, int *restrict prioceiling)
69-
{
68+
int pthread_mutexattr_getprioceiling(const pthread_mutexattr_t *restrict attr, int *restrict prioceiling) {
7069
// Not supported either in Emscripten or musl, return a faked value.
7170
if (prioceiling) *prioceiling = 99;
7271
return 0;
7372
}
7473

75-
int pthread_mutexattr_setprioceiling(pthread_mutexattr_t *attr, int prioceiling)
76-
{
74+
int pthread_mutexattr_setprioceiling(pthread_mutexattr_t *attr, int prioceiling) {
7775
// Not supported either in Emscripten or musl, return an error.
7876
return EPERM;
7977
}
8078

81-
static uint32_t dummyZeroAddress = 0;
8279

8380
void emscripten_thread_sleep(double msecs) {
84-
double now = emscripten_get_now();
85-
double target = now + msecs;
86-
87-
// If we have less than this many msecs left to wait, busy spin that instead.
88-
double min_ms_slice_to_sleep = 0.1;
89-
90-
// Break up sleeping so that we process proxied work at regular intervals.
91-
// TODO(sbc): This should be removed and/or moved down into
92-
// `emscripten_futex_wait`.
93-
double max_ms_slice_to_sleep = 100;
94-
95-
emscripten_conditional_set_current_thread_status(
96-
EM_THREAD_STATUS_RUNNING, EM_THREAD_STATUS_SLEEPING);
97-
98-
do {
99-
// Keep processing the main loop of the calling thread.
100-
__pthread_testcancel(); // pthreads spec: sleep is a cancellation point, so must test if this
101-
// thread is cancelled during the sleep.
102-
emscripten_current_thread_process_queued_calls();
103-
104-
now = emscripten_get_now();
105-
double ms_to_sleep = target - now;
106-
if (ms_to_sleep < min_ms_slice_to_sleep)
107-
continue;
108-
if (ms_to_sleep > max_ms_slice_to_sleep)
109-
ms_to_sleep = max_ms_slice_to_sleep;
110-
emscripten_futex_wait(&dummyZeroAddress, 0, ms_to_sleep);
111-
now = emscripten_get_now();
112-
} while (now < target);
113-
114-
emscripten_conditional_set_current_thread_status(
115-
EM_THREAD_STATUS_SLEEPING, EM_THREAD_STATUS_RUNNING);
81+
__pthread_testcancel(); // pthreads spec: `clock_nanosleep` is a cancellation point
82+
emscripten_current_thread_process_queued_calls();
83+
emscripten_conditional_set_current_thread_status(EM_THREAD_STATUS_RUNNING,
84+
EM_THREAD_STATUS_SLEEPING);
85+
uint32_t dummyZeroAddress = 0;
86+
emscripten_futex_wait(&dummyZeroAddress, 0, msecs);
87+
emscripten_conditional_set_current_thread_status(EM_THREAD_STATUS_SLEEPING,
88+
EM_THREAD_STATUS_RUNNING);
89+
__pthread_testcancel();
90+
emscripten_current_thread_process_queued_calls();
11691
}
11792

11893
static struct pthread __main_pthread;

0 commit comments

Comments
 (0)