Skip to content

Commit 30b5029

Browse files
committed
Move pthread_barrier_wait logic to __futexwait. NFC
1 parent 82e24d4 commit 30b5029

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
@@ -203,7 +203,18 @@ static inline void __wake(volatile void *addr, int cnt, int priv)
203203
static inline void __futexwait(volatile void *addr, int val, int priv)
204204
{
205205
#ifdef __EMSCRIPTEN__
206-
__wait(addr, NULL, val, priv);
206+
(void)priv;
207+
const int is_runtime_thread = emscripten_is_main_runtime_thread();
208+
if (is_runtime_thread) {
209+
int e;
210+
do {
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+
}
207218
#else
208219
if (priv) priv = FUTEX_PRIVATE;
209220
__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)