Skip to content

Commit

Permalink
Use emscripten_out/err in wasm worker tests. NFC (#22761)
Browse files Browse the repository at this point in the history
Also replace some EM_ASM that only existed to call `out` or `err`.

For multi-threads tests that run under node its better to use `out` /
`err` than `console.log` / `console.error` since the former get
redirected to stdout/stderr directly whereas it seem the later requires
some sort of messaging with the main thread.
  • Loading branch information
sbc100 authored Oct 17, 2024
1 parent ac83519 commit d5a6c39
Show file tree
Hide file tree
Showing 15 changed files with 109 additions and 114 deletions.
6 changes: 3 additions & 3 deletions test/wasm_worker/c11__Thread_local.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include <emscripten.h>
#include <emscripten/console.h>
#include <emscripten/wasm_worker.h>
#include <assert.h>
#include <threads.h>
Expand All @@ -7,7 +7,7 @@ _Thread_local int __attribute__((aligned(64))) tls = 1;

void main_thread_func() {
assert(!emscripten_current_thread_is_wasm_worker());
EM_ASM(out($0), tls);
emscripten_outf("%d", tls);
#ifdef REPORT_RESULT
REPORT_RESULT(tls);
#endif
Expand All @@ -26,7 +26,7 @@ void worker_main() {
char stack[1024];

int main() {
EM_ASM(out($0), tls);
emscripten_outf("%d", tls);
assert(((intptr_t)&tls % 64) == 0);
assert(!emscripten_current_thread_is_wasm_worker());
tls = 42;
Expand Down
30 changes: 15 additions & 15 deletions test/wasm_worker/cancel_all_wait_asyncs.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include <emscripten.h>
#include <emscripten/console.h>
#include <emscripten/wasm_worker.h>
#include <emscripten/threading.h>
#include <assert.h>
Expand All @@ -10,64 +10,64 @@ volatile int32_t addr = 1;
bool testSucceeded = 1;

void asyncWaitFinishedShouldNotBeCalled(int32_t *ptr, uint32_t val, ATOMICS_WAIT_RESULT_T waitResult, void *userData) {
emscripten_console_log("asyncWaitFinishedShouldNotBeCalled");
emscripten_out("asyncWaitFinishedShouldNotBeCalled");
testSucceeded = 0;
assert(0); // We should not reach here
}

void asyncWaitFinishedShouldBeCalled(int32_t *ptr, uint32_t val, ATOMICS_WAIT_RESULT_T waitResult, void *userData) {
emscripten_console_log("asyncWaitFinishedShouldBeCalled");
emscripten_out("asyncWaitFinishedShouldBeCalled");
#ifdef REPORT_RESULT
REPORT_RESULT(testSucceeded);
#endif
}

int main() {
emscripten_console_log("Async waiting on address should give a wait token");
emscripten_out("Async waiting on address should give a wait token");
ATOMICS_WAIT_TOKEN_T ret = emscripten_atomic_wait_async((int32_t*)&addr, 1, asyncWaitFinishedShouldNotBeCalled, (void*)42, EMSCRIPTEN_WAIT_ASYNC_INFINITY);
assert(EMSCRIPTEN_IS_VALID_WAIT_TOKEN(ret));

emscripten_console_log("Async waiting on address should give a wait token");
emscripten_out("Async waiting on address should give a wait token");
ATOMICS_WAIT_TOKEN_T ret2 = emscripten_atomic_wait_async((int32_t*)&addr, 1, asyncWaitFinishedShouldNotBeCalled, (void*)42, EMSCRIPTEN_WAIT_ASYNC_INFINITY);
assert(EMSCRIPTEN_IS_VALID_WAIT_TOKEN(ret2));

emscripten_console_log("Canceling all async waits should return the number of waits cancelled");
emscripten_out("Canceling all async waits should return the number of waits cancelled");
int numCancelled = emscripten_atomic_cancel_all_wait_asyncs();
assert(numCancelled == 2);

emscripten_console_log("Canceling an async wait that has already been cancelled should give an invalid param");
emscripten_out("Canceling an async wait that has already been cancelled should give an invalid param");
EMSCRIPTEN_RESULT r = emscripten_atomic_cancel_wait_async(ret);
assert(r == EMSCRIPTEN_RESULT_INVALID_PARAM);

emscripten_console_log("Canceling an async wait that has already been cancelled should give an invalid param");
emscripten_out("Canceling an async wait that has already been cancelled should give an invalid param");
r = emscripten_atomic_cancel_wait_async(ret2);
assert(r == EMSCRIPTEN_RESULT_INVALID_PARAM);

emscripten_console_log("Notifying an async wait should not trigger the callback function");
emscripten_out("Notifying an async wait should not trigger the callback function");
int64_t numWoken = emscripten_atomic_notify((int32_t*)&addr, EMSCRIPTEN_NOTIFY_ALL_WAITERS);

emscripten_console_log("Notifying an async wait should return 0 threads woken");
emscripten_out("Notifying an async wait should return 0 threads woken");
assert(numWoken == 0);

addr = 2;
emscripten_console_log("Notifying an async wait even after changed value should not trigger the callback function");
emscripten_out("Notifying an async wait even after changed value should not trigger the callback function");
numWoken = emscripten_atomic_notify((int32_t*)&addr, EMSCRIPTEN_NOTIFY_ALL_WAITERS);

emscripten_console_log("Notifying an async wait should return 0 threads woken");
emscripten_out("Notifying an async wait should return 0 threads woken");
assert(numWoken == 0);

emscripten_console_log("Async waiting on address should give a wait token");
emscripten_out("Async waiting on address should give a wait token");
ret = emscripten_atomic_wait_async((int32_t*)&addr, 2, asyncWaitFinishedShouldBeCalled, (void*)42, EMSCRIPTEN_WAIT_ASYNC_INFINITY);
assert(EMSCRIPTEN_IS_VALID_WAIT_TOKEN(ret));

#if 0
emscripten_console_log("Notifying an async wait without value changing should still trigger the callback");
emscripten_out("Notifying an async wait without value changing should still trigger the callback");
numWoken = emscripten_atomic_notify((int32_t*)&addr, EMSCRIPTEN_NOTIFY_ALL_WAITERS);
assert(numWoken == 1);
#else
// TODO: Switch to the above test instead after the Atomics.waitAsync() polyfill is dropped.
addr = 3;
emscripten_console_log("Notifying an async wait after value changing should trigger the callback");
emscripten_out("Notifying an async wait after value changing should trigger the callback");
numWoken = emscripten_atomic_notify((int32_t*)&addr, EMSCRIPTEN_NOTIFY_ALL_WAITERS);
#endif
}
32 changes: 16 additions & 16 deletions test/wasm_worker/cancel_all_wait_asyncs_at_address.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include <emscripten.h>
#include <emscripten/console.h>
#include <emscripten/wasm_worker.h>
#include <emscripten/threading.h>
#include <assert.h>
Expand All @@ -10,68 +10,68 @@ volatile int32_t addr = 1;
bool testSucceeded = 1;

void asyncWaitFinishedShouldNotBeCalled(int32_t *ptr, uint32_t val, ATOMICS_WAIT_RESULT_T waitResult, void *userData) {
emscripten_console_log("asyncWaitFinishedShouldNotBeCalled");
emscripten_out("asyncWaitFinishedShouldNotBeCalled");
testSucceeded = 0;
assert(0); // We should not reach here
}

void asyncWaitFinishedShouldBeCalled(int32_t *ptr, uint32_t val, ATOMICS_WAIT_RESULT_T waitResult, void *userData) {
emscripten_console_log("asyncWaitFinishedShouldBeCalled");
emscripten_out("asyncWaitFinishedShouldBeCalled");
#ifdef REPORT_RESULT
REPORT_RESULT(testSucceeded);
#endif
}

int main() {
emscripten_console_log("Async waiting on address should give a wait token");
emscripten_out("Async waiting on address should give a wait token");
ATOMICS_WAIT_TOKEN_T ret = emscripten_atomic_wait_async((int32_t*)&addr, 1, asyncWaitFinishedShouldNotBeCalled, (void*)42, EMSCRIPTEN_WAIT_ASYNC_INFINITY);
assert(EMSCRIPTEN_IS_VALID_WAIT_TOKEN(ret));

emscripten_console_log("Async waiting on address should give a wait token");
emscripten_out("Async waiting on address should give a wait token");
ATOMICS_WAIT_TOKEN_T ret2 = emscripten_atomic_wait_async((int32_t*)&addr, 1, asyncWaitFinishedShouldNotBeCalled, (void*)42, EMSCRIPTEN_WAIT_ASYNC_INFINITY);
assert(EMSCRIPTEN_IS_VALID_WAIT_TOKEN(ret2));

emscripten_console_log("Canceling all async waits at wait address should return the number of waits cancelled");
emscripten_out("Canceling all async waits at wait address should return the number of waits cancelled");
int numCancelled = emscripten_atomic_cancel_all_wait_asyncs_at_address((int32_t*)&addr);
assert(numCancelled == 2);

emscripten_console_log("Canceling all async waits at some other address should return 0");
emscripten_out("Canceling all async waits at some other address should return 0");
numCancelled = emscripten_atomic_cancel_all_wait_asyncs_at_address((int32_t*)0xbad);
assert(numCancelled == 0);

emscripten_console_log("Canceling an async wait that has already been cancelled should give an invalid param");
emscripten_out("Canceling an async wait that has already been cancelled should give an invalid param");
EMSCRIPTEN_RESULT r = emscripten_atomic_cancel_wait_async(ret);
assert(r == EMSCRIPTEN_RESULT_INVALID_PARAM);

emscripten_console_log("Canceling an async wait that has already been cancelled should give an invalid param");
emscripten_out("Canceling an async wait that has already been cancelled should give an invalid param");
r = emscripten_atomic_cancel_wait_async(ret2);
assert(r == EMSCRIPTEN_RESULT_INVALID_PARAM);

emscripten_console_log("Notifying an async wait should not trigger the callback function");
emscripten_out("Notifying an async wait should not trigger the callback function");
int64_t numWoken = emscripten_atomic_notify((int32_t*)&addr, EMSCRIPTEN_NOTIFY_ALL_WAITERS);

emscripten_console_log("Notifying an async wait should return 0 threads woken");
emscripten_out("Notifying an async wait should return 0 threads woken");
assert(numWoken == 0);

addr = 2;
emscripten_console_log("Notifying an async wait even after changed value should not trigger the callback function");
emscripten_out("Notifying an async wait even after changed value should not trigger the callback function");
numWoken = emscripten_atomic_notify((int32_t*)&addr, EMSCRIPTEN_NOTIFY_ALL_WAITERS);

emscripten_console_log("Notifying an async wait should return 0 threads woken");
emscripten_out("Notifying an async wait should return 0 threads woken");
assert(numWoken == 0);

emscripten_console_log("Async waiting on address should give a wait token");
emscripten_out("Async waiting on address should give a wait token");
ret = emscripten_atomic_wait_async((int32_t*)&addr, 2, asyncWaitFinishedShouldBeCalled, (void*)42, EMSCRIPTEN_WAIT_ASYNC_INFINITY);
assert(EMSCRIPTEN_IS_VALID_WAIT_TOKEN(ret));

#if 0
emscripten_console_log("Notifying an async wait without value changing should still trigger the callback");
emscripten_out("Notifying an async wait without value changing should still trigger the callback");
numWoken = emscripten_atomic_notify((int32_t*)&addr, EMSCRIPTEN_NOTIFY_ALL_WAITERS);
assert(numWoken == 1);
#else
// TODO: Switch to the above test instead after the Atomics.waitAsync() polyfill is dropped.
addr = 3;
emscripten_console_log("Notifying an async wait after value changing should trigger the callback");
emscripten_out("Notifying an async wait after value changing should trigger the callback");
numWoken = emscripten_atomic_notify((int32_t*)&addr, EMSCRIPTEN_NOTIFY_ALL_WAITERS);
#endif
}
26 changes: 13 additions & 13 deletions test/wasm_worker/cancel_wait_async.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include <emscripten.h>
#include <emscripten/console.h>
#include <emscripten/wasm_worker.h>
#include <emscripten/threading.h>
#include <assert.h>
Expand All @@ -10,56 +10,56 @@ volatile int32_t addr = 1;
bool testSucceeded = 1;

void asyncWaitFinishedShouldNotBeCalled(int32_t *ptr, uint32_t val, ATOMICS_WAIT_RESULT_T waitResult, void *userData) {
emscripten_console_log("asyncWaitFinishedShouldNotBeCalled");
emscripten_out("asyncWaitFinishedShouldNotBeCalled");
testSucceeded = 0;
assert(0); // We should not reach here
}

void asyncWaitFinishedShouldBeCalled(int32_t *ptr, uint32_t val, ATOMICS_WAIT_RESULT_T waitResult, void *userData) {
emscripten_console_log("asyncWaitFinishedShouldBeCalled");
emscripten_out("asyncWaitFinishedShouldBeCalled");
#ifdef REPORT_RESULT
REPORT_RESULT(testSucceeded);
#endif
}

int main() {
emscripten_console_log("Async waiting on address should give a wait token");
emscripten_out("Async waiting on address should give a wait token");
ATOMICS_WAIT_TOKEN_T ret = emscripten_atomic_wait_async((int32_t*)&addr, 1, asyncWaitFinishedShouldNotBeCalled, (void*)42, EMSCRIPTEN_WAIT_ASYNC_INFINITY);
assert(EMSCRIPTEN_IS_VALID_WAIT_TOKEN(ret));

emscripten_console_log("Canceling an async wait should succeed");
emscripten_out("Canceling an async wait should succeed");
EMSCRIPTEN_RESULT r = emscripten_atomic_cancel_wait_async(ret);
assert(r == EMSCRIPTEN_RESULT_SUCCESS);

emscripten_console_log("Canceling an async wait a second time should give invalid param");
emscripten_out("Canceling an async wait a second time should give invalid param");
r = emscripten_atomic_cancel_wait_async(ret);
assert(r == EMSCRIPTEN_RESULT_INVALID_PARAM);

emscripten_console_log("Notifying an async wait should not trigger the callback function");
emscripten_out("Notifying an async wait should not trigger the callback function");
int64_t numWoken = emscripten_atomic_notify((int32_t*)&addr, EMSCRIPTEN_NOTIFY_ALL_WAITERS);

emscripten_console_log("Notifying an async wait should return 0 threads woken");
emscripten_out("Notifying an async wait should return 0 threads woken");
assert(numWoken == 0);

addr = 2;
emscripten_console_log("Notifying an async wait even after changed value should not trigger the callback function");
emscripten_out("Notifying an async wait even after changed value should not trigger the callback function");
numWoken = emscripten_atomic_notify((int32_t*)&addr, EMSCRIPTEN_NOTIFY_ALL_WAITERS);

emscripten_console_log("Notifying an async wait should return 0 threads woken");
emscripten_out("Notifying an async wait should return 0 threads woken");
assert(numWoken == 0);

emscripten_console_log("Async waiting on address should give a wait token");
emscripten_out("Async waiting on address should give a wait token");
ret = emscripten_atomic_wait_async((int32_t*)&addr, 2, asyncWaitFinishedShouldBeCalled, (void*)42, EMSCRIPTEN_WAIT_ASYNC_INFINITY);
assert(EMSCRIPTEN_IS_VALID_WAIT_TOKEN(ret));

#if 0
emscripten_console_log("Notifying an async wait without value changing should still trigger the callback");
emscripten_out("Notifying an async wait without value changing should still trigger the callback");
numWoken = emscripten_atomic_notify((int32_t*)&addr, EMSCRIPTEN_NOTIFY_ALL_WAITERS);
assert(numWoken == 1);
#else
// TODO: Switch to the above test instead after the Atomics.waitAsync() polyfill is dropped.
addr = 3;
emscripten_console_log("Notifying an async wait after value changing should trigger the callback");
emscripten_out("Notifying an async wait after value changing should trigger the callback");
numWoken = emscripten_atomic_notify((int32_t*)&addr, EMSCRIPTEN_NOTIFY_ALL_WAITERS);
#endif
}
15 changes: 6 additions & 9 deletions test/wasm_worker/cpp11_thread_local.cpp
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
#include <emscripten.h>
#include <emscripten/console.h>
#include <emscripten/wasm_worker.h>
#include <assert.h>

thread_local int tls = 1;

void main_thread_func()
{
void main_thread_func() {
assert(!emscripten_current_thread_is_wasm_worker());
EM_ASM(out($0), tls);
emscripten_outf("%d", tls);
#ifdef REPORT_RESULT
REPORT_RESULT(tls);
#endif
}

void worker_main()
{
void worker_main() {
assert(emscripten_current_thread_is_wasm_worker());
assert(tls != 42);
assert(tls != 0);
Expand All @@ -25,9 +23,8 @@ void worker_main()

char stack[1024];

int main()
{
EM_ASM(out($0), tls);
int main() {
emscripten_outf("%d", tls);
assert(!emscripten_current_thread_is_wasm_worker());
tls = 42;
emscripten_wasm_worker_t worker = emscripten_create_wasm_worker(stack, sizeof(stack));
Expand Down
6 changes: 3 additions & 3 deletions test/wasm_worker/gcc___Thread.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include <emscripten.h>
#include <emscripten/console.h>
#include <emscripten/wasm_worker.h>
#include <assert.h>
#include <threads.h>
Expand All @@ -7,7 +7,7 @@ __thread int tls = 1;

void main_thread_func() {
assert(!emscripten_current_thread_is_wasm_worker());
EM_ASM(out($0), tls);
emscripten_outf("%d", tls);
#ifdef REPORT_RESULT
REPORT_RESULT(tls);
#endif
Expand All @@ -25,7 +25,7 @@ void worker_main() {
char stack[1024];

int main() {
EM_ASM(out($0), tls);
emscripten_outf("%d", tls);
assert(!emscripten_current_thread_is_wasm_worker());
tls = 42;
emscripten_wasm_worker_t worker = emscripten_create_wasm_worker(stack, sizeof(stack));
Expand Down
4 changes: 2 additions & 2 deletions test/wasm_worker/hello_wasm_worker.c
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
#include <emscripten.h>
#include <emscripten/console.h>
#include <emscripten/em_asm.h>
#include <emscripten/wasm_worker.h>
#include <assert.h>

// This is the code example in site/source/docs/api_reference/wasm_workers.rst

void run_in_worker() {
emscripten_console_log("Hello from wasm worker!\n");
emscripten_out("Hello from wasm worker!\n");
EM_ASM(typeof checkStackCookie == 'function' && checkStackCookie());
#ifdef REPORT_RESULT
REPORT_RESULT(0);
Expand Down
10 changes: 5 additions & 5 deletions test/wasm_worker/lock_async_acquire.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include <emscripten.h>
#include <emscripten/console.h>
#include <emscripten/wasm_worker.h>
#include <emscripten/threading.h>
#include <stdlib.h>
Expand All @@ -18,7 +18,7 @@ int numTimesMainThreadAcquiredLock = 0;
int numTimesWasmWorkerAcquiredLock = 0;

void work() {
// emscripten_console_log("work");
// emscripten_out("work");
volatile int x = sharedState0;
volatile int y = sharedState1;
assert(x == y+1 || y == x+1);
Expand All @@ -43,7 +43,7 @@ void work() {

if (y > 100 && numTimesMainThreadAcquiredLock && numTimesWasmWorkerAcquiredLock) {
if (!testFinished) {
emscripten_console_log("test finished");
emscripten_out("test finished");
#ifdef REPORT_RESULT
REPORT_RESULT(0);
#endif
Expand All @@ -56,7 +56,7 @@ void work() {
void schedule_work(void *userData);

void lock_async_acquired(volatile void *addr, uint32_t val, ATOMICS_WAIT_RESULT_T waitResult, void *userData) {
// emscripten_console_log("async lock acquired");
// emscripten_out("async lock acquired");
assert(addr == &lock);
assert(val == 0 || val == 1);
assert(waitResult == ATOMICS_WAIT_OK);
Expand All @@ -72,7 +72,7 @@ void lock_async_acquired(volatile void *addr, uint32_t val, ATOMICS_WAIT_RESULT_
void schedule_work(void *userData) {
if (emscripten_current_thread_is_wasm_worker() && emscripten_random() > 0.5) {
emscripten_lock_waitinf_acquire(&lock);
// emscripten_console_log("sync lock acquired");
// emscripten_out("sync lock acquired");
work();
emscripten_lock_release(&lock);
if (!testFinished)
Expand Down
Loading

0 comments on commit d5a6c39

Please sign in to comment.