Skip to content

Commit 816ef7a

Browse files
committed
emscripten_thread_supports_atomics_wait() is now public
1 parent fcfe073 commit 816ef7a

File tree

5 files changed

+11
-16
lines changed

5 files changed

+11
-16
lines changed

system/include/emscripten/threading.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,11 @@ int emscripten_is_main_runtime_thread(void);
4848
// this returns 1.
4949
int emscripten_is_main_browser_thread(void);
5050

51+
// Return non-zero if the calling thread supports Atomic.wait (For example if called from
52+
// the main browser or audio worklet thread, this function will return zero since blocking
53+
// is not allowed there).
54+
int emscripten_thread_supports_atomics_wait(void);
55+
5156
// A temporary workaround to issue
5257
// https://github.com/emscripten-core/emscripten/issues/3495:
5358
// Call this in the body of all lock-free atomic (cas) loops that the main

system/lib/pthread/emscripten_futex_wait.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
#include <stdio.h>
1313
#include <sys/param.h>
1414
#include "atomic.h"
15-
#include "threading_internal.h"
1615
#include "pthread_impl.h"
1716

1817
extern void* _emscripten_main_thread_futex;
@@ -129,7 +128,7 @@ int emscripten_futex_wait(volatile void *addr, uint32_t val, double max_wait_ms)
129128

130129
// For the main browser thread and audio worklets we can't use
131130
// __builtin_wasm_memory_atomic_wait32 so we have busy wait instead.
132-
if (!_emscripten_thread_supports_atomics_wait()) {
131+
if (!emscripten_thread_supports_atomics_wait()) {
133132
ret = futex_wait_main_browser_thread(addr, val, max_wait_ms);
134133
emscripten_conditional_set_current_thread_status(EM_THREAD_STATUS_WAITFUTEX, EM_THREAD_STATUS_RUNNING);
135134
return ret;

system/lib/pthread/emscripten_thread_state.S

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,8 @@ emscripten_is_main_browser_thread:
5454
end_function
5555

5656
# Semantically the same as testing "!ENVIRONMENT_IS_WEB && !ENVIRONMENT_IS_AUDIO_WORKLET" in JS
57-
.globl _emscripten_thread_supports_atomics_wait
58-
_emscripten_thread_supports_atomics_wait:
59-
.functype _emscripten_thread_supports_atomics_wait () -> (i32)
57+
.globl emscripten_thread_supports_atomics_wait
58+
emscripten_thread_supports_atomics_wait:
59+
.functype emscripten_thread_supports_atomics_wait () -> (i32)
6060
global.get supports_wait
6161
end_function

system/lib/pthread/threading_internal.h

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,3 @@ int _emscripten_default_pthread_stack_size();
9797
void __set_thread_state(pthread_t ptr, int is_main, int is_runtime, int can_block);
9898

9999
double _emscripten_receive_on_main_thread_js(int funcIndex, void* emAsmAddr, pthread_t callingThread, int numCallArgs, double* args);
100-
101-
// Return non-zero if the calling thread supports Atomic.wait (For example
102-
// if called from the main browser thread, this function will return zero
103-
// since blocking is not allowed there).
104-
int _emscripten_thread_supports_atomics_wait(void);

test/webaudio/audioworklet_emscripten_locks.cpp

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#include <emscripten/threading.h>
12
#include <emscripten/wasm_worker.h>
23
#include <emscripten/webaudio.h>
34
#include <stdio.h>
@@ -12,16 +13,11 @@
1213
// - emscripten_lock_release()
1314
// - emscripten_get_now() does not crash in a Wasm Audio Worklet.
1415

15-
// Todo: add/find a way to verify atomic wait fails?
16-
17-
// In system/lib/pthread/emscripten_thread_state.S
18-
extern "C" int _emscripten_thread_supports_atomics_wait(void);
19-
2016
emscripten_lock_t testLock = EMSCRIPTEN_LOCK_T_STATIC_INITIALIZER;
2117
int testSuccess = 0;
2218

2319
bool ProcessAudio(int numInputs, const AudioSampleFrame *inputs, int numOutputs, AudioSampleFrame *outputs, int numParams, const AudioParamFrame *params, void *userData) {
24-
int supportsAtomicWait = _emscripten_thread_supports_atomics_wait();
20+
int supportsAtomicWait = emscripten_thread_supports_atomics_wait();
2521
printf("supportsAtomicWait: %d\n", supportsAtomicWait);
2622
assert(!supportsAtomicWait);
2723
//emscripten_futex_wake(&futexLocation, 1);

0 commit comments

Comments
 (0)