Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions system/lib/pthread/library_pthread_stub.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,16 @@ bool emscripten_has_threading_support() { return false; }

int emscripten_num_logical_cores() { return 1; }

int emscripten_futex_wait(
volatile void /*uint32_t*/* addr, uint32_t val, double maxWaitMilliseconds) {
// nop
return 0; // success
int emscripten_futex_wait(volatile void /*uint32_t*/* addr,
uint32_t val,
double maxWaitMilliseconds) {
if (!addr) {
return -EINVAL;
}
if (*(uint32_t*)addr != val) {
return -EWOULDBLOCK;
}
return -ENOTSUP;
}

int emscripten_futex_wake(volatile void /*uint32_t*/* addr, int count) {
Expand Down
4 changes: 2 additions & 2 deletions test/codesize/test_codesize_hello_dylink_all.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"a.out.js": 244691,
"a.out.nodebug.wasm": 577678,
"total": 822369,
"a.out.nodebug.wasm": 577699,
"total": 822390,
"sent": [
"IMG_Init",
"IMG_Load",
Expand Down
6 changes: 4 additions & 2 deletions test/test_browser.py
Original file line number Diff line number Diff line change
Expand Up @@ -5039,9 +5039,11 @@ def test_wasm_worker_hello_wasm2js(self):
def test_wasm_worker_hello_embedded(self):
self.btest_exit('wasm_worker/hello_wasm_worker.c', cflags=['-sWASM_WORKERS=2'])

# Tests that it is possible to call emscripten_futex_wait() in Wasm Workers.
# Tests that it is possible to call emscripten_futex_wait() in Wasm Workers when pthreads
# are also enabled.
@parameterized({
'': ([],),
# Without pthreads we expect the stub version of the futex API
'': (['-DEXPECT_STUB'],),
'pthread': (['-pthread'],),
})
def test_wasm_worker_futex_wait(self, args):
Expand Down
30 changes: 24 additions & 6 deletions test/wasm_worker/wasm_worker_futex_wait.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// Test that emscripten_futex_wait() works in a Wasm Worker.

#include <assert.h>
#include <errno.h>
#include <emscripten.h>
#include <emscripten/console.h>
#include <emscripten/threading.h>
Expand All @@ -12,21 +14,37 @@ _Atomic uint32_t futex_value = 0;

void wake_worker_after_delay(void *user_data) {
futex_value = 1;
emscripten_futex_wake(&futex_value, INT_MAX);
emscripten_futex_wake(&futex_value, INT_MAX);
}

void wake_worker() {
printf("Waking worker thread from futex wait.\n");
printf("Waking worker thread from futex wait...\n");
emscripten_set_timeout(wake_worker_after_delay, 500, 0);
}

void worker_main() {
printf("Worker sleeping for futex wait.\n");
printf("Worker sleeping on futex...\n");
double start = emscripten_performance_now();
int rc = emscripten_futex_wait(&futex_value, 0, 100);
double end = emscripten_performance_now();
printf("emscripten_futex_wait returned: %d after %.2fms\n", rc, end - start);
#if EXPECT_STUB
// The stub implemenation returns -ENOTSUP
assert(rc == -ENOTSUP);
#else
assert(rc == -ETIMEDOUT);
assert((end - start) >= 100);

printf("Worker sleeping on futex with wakeup...\n");
emscripten_wasm_worker_post_function_v(0, wake_worker);
int rc = emscripten_futex_wait(&futex_value, 0, INFINITY);
printf("emscripten_futex_wait returned with code %d.\n", rc);
rc = emscripten_futex_wait(&futex_value, 0, INFINITY);
printf("emscripten_futex_wait returned: %d\n", rc);
assert(rc == 0);
assert(futex_value == 1);
#endif

#ifdef REPORT_RESULT
REPORT_RESULT(rc);
REPORT_RESULT(0);
#endif
}

Expand Down
16 changes: 9 additions & 7 deletions test/webaudio/audioworklet_worker.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
#include <emscripten/wasm_worker.h>
#include <emscripten/threading.h>
#include <assert.h>
#include <stdbool.h>

// Tests that
// - audioworklets and workers can be used at the same time.
// - an audioworklet can emscripten_futex_wake() a waiting worker.
// - global values can be shared between audioworklets and workers.

int workletToWorkerFutexLocation = 0;
int workletToWorkerFlag = 0;
_Atomic bool workletToWorkerFlag = false;
EMSCRIPTEN_WEBAUDIO_T context;

void do_exit() {
Expand All @@ -19,9 +19,12 @@ void do_exit() {
}

void run_in_worker() {
while (0 == emscripten_futex_wait(&workletToWorkerFutexLocation, 0, 30000)) {
if (workletToWorkerFlag == 1) {
emscripten_out("Test success");
// TODO: Convert to emscripten_futex_wait once it becomes available in Wasm
// Workers.
double start = emscripten_performance_now();
while (1) {
if (workletToWorkerFlag == true) {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How long does this test run? This could be quite a CPU burner overhead for the test.

Maybe the while() part could be gated behind a

#ifndef __EMSCRIPTEN_WASM_WORKERS__
while (0 == emscripten_futex_wait(&workletToWorkerFutexLocation, 0, 30000))
#endif
{
   ...
}

so in the Wasm Workers mode only it would avoid the futex wait.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On my machine this busy looks runs for 2 or 3 ms.

I added some log information to record how long the busy wait is.

Note that the code busy waits too because emscripten_futex_wait is a no-op on wasm workers.

This test only ever run in wasm workers only mode.

emscripten_outf("Test success (waited %.fms)", emscripten_performance_now() - start);
emscripten_wasm_worker_post_function_v(EMSCRIPTEN_WASM_WORKER_ID_PARENT, &do_exit);
break;
}
Expand All @@ -31,8 +34,7 @@ void run_in_worker() {
// This event will fire on the audio worklet thread.
void MessageReceivedInAudioWorkletThread() {
assert(emscripten_current_thread_is_audio_worklet());
workletToWorkerFlag = 1;
emscripten_futex_wake(&workletToWorkerFutexLocation, 1);
workletToWorkerFlag = true;
}

void WebAudioWorkletThreadInitialized(EMSCRIPTEN_WEBAUDIO_T audioContext, bool success, void *userData) {
Expand Down