Skip to content

Commit 57ad420

Browse files
committed
Move pthread_barrier_wait logic to __futexwait. NFC.
1 parent daf73df commit 57ad420

File tree

2 files changed

+17
-18
lines changed

2 files changed

+17
-18
lines changed

system/lib/libc/musl/src/internal/pthread_impl.h

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,20 @@ static inline void __wake(volatile void *addr, int cnt, int priv)
201201
static inline void __futexwait(volatile void *addr, int val, int priv)
202202
{
203203
#ifdef __EMSCRIPTEN__
204-
__wait(addr, NULL, val, priv);
204+
(void)priv;
205+
const int is_runtime_thread = emscripten_is_main_runtime_thread();
206+
if (is_runtime_thread) {
207+
int e;
208+
do {
209+
// Assist other threads by executing proxied operations that are effectively singlethreaded.
210+
emscripten_main_thread_process_queued_calls();
211+
// Main runtime thread may need to run proxied calls, so sleep in very small slices to be responsive.
212+
e = emscripten_futex_wait(addr, val, 1);
213+
} while (e == -ETIMEDOUT);
214+
} else {
215+
// Can wait in one go.
216+
emscripten_futex_wait(addr, val, INFINITY);
217+
}
205218
#else
206219
if (priv) priv = FUTEX_PRIVATE;
207220
__syscall(SYS_futex, addr, FUTEX_WAIT|priv, val, 0) != -ENOSYS ||

system/lib/libc/musl/src/thread/pthread_barrier_wait.c

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -83,28 +83,14 @@ int pthread_barrier_wait(pthread_barrier_t *b)
8383
while (spins-- && !inst->finished)
8484
a_spin();
8585
a_inc(&inst->finished);
86-
#ifdef __EMSCRIPTEN__
87-
int is_runtime_thread = emscripten_is_main_runtime_thread();
8886
while (inst->finished == 1) {
89-
if (is_runtime_thread) {
90-
int e;
91-
do {
92-
// Assist other threads by executing proxied operations that are effectively singlethreaded.
93-
emscripten_main_thread_process_queued_calls();
94-
// Main runtime thread may need to run proxied calls, so sleep in very small slices to be responsive.
95-
e = emscripten_futex_wait(&inst->finished, 1, 1);
96-
} while (e == -ETIMEDOUT);
97-
} else {
98-
// Can wait in one go.
99-
emscripten_futex_wait(&inst->finished, 1, INFINITY);
100-
}
101-
}
87+
#ifdef __EMSCRIPTEN__
88+
__futexwait(&inst->finished, 1, 1);
10289
#else
103-
while (inst->finished == 1) {
10490
__syscall(SYS_futex,&inst->finished,FUTEX_WAIT|FUTEX_PRIVATE,1,0) != -ENOSYS
10591
|| __syscall(SYS_futex,&inst->finished,FUTEX_WAIT,1,0);
106-
}
10792
#endif
93+
}
10894
return PTHREAD_BARRIER_SERIAL_THREAD;
10995
}
11096

0 commit comments

Comments
 (0)