Skip to content

Commit 4bf115a

Browse files
committed
Move pthread_barrier_wait logic to __futexwait. NFC.
1 parent 884365e commit 4bf115a

File tree

2 files changed

+15
-16
lines changed

2 files changed

+15
-16
lines changed

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

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,18 @@ 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+
// Main runtime thread may need to run proxied calls, so sleep in very small slices to be responsive.
210+
e = emscripten_futex_wait(addr, val, 1);
211+
} while (e == -ETIMEDOUT);
212+
} else {
213+
// Can wait in one go.
214+
emscripten_futex_wait(addr, val, INFINITY);
215+
}
205216
#else
206217
if (priv) priv = FUTEX_PRIVATE;
207218
__syscall(SYS_futex, addr, FUTEX_WAIT|priv, val, 0) != -ENOSYS ||

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

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -83,26 +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-
// Main runtime thread may need to run proxied calls, so sleep in very small slices to be responsive.
93-
e = emscripten_futex_wait(&inst->finished, 1, 1);
94-
} while (e == -ETIMEDOUT);
95-
} else {
96-
// Can wait in one go.
97-
emscripten_futex_wait(&inst->finished, 1, INFINITY);
98-
}
99-
}
87+
#ifdef __EMSCRIPTEN__
88+
__futexwait(&inst->finished, 1, 1);
10089
#else
101-
while (inst->finished == 1) {
10290
__syscall(SYS_futex,&inst->finished,FUTEX_WAIT|FUTEX_PRIVATE,1,0) != -ENOSYS
10391
|| __syscall(SYS_futex,&inst->finished,FUTEX_WAIT,1,0);
104-
}
10592
#endif
93+
}
10694
return PTHREAD_BARRIER_SERIAL_THREAD;
10795
}
10896

0 commit comments

Comments
 (0)