forked from emscripten-core/emscripten
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwait32_notify.c
More file actions
72 lines (56 loc) · 2.3 KB
/
Copy pathwait32_notify.c
File metadata and controls
72 lines (56 loc) · 2.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include <emscripten/html5.h>
#include <emscripten/wasm_worker.h>
#include <emscripten/threading.h>
#include <assert.h>
// Test emscripten_wasm_wait_i32() and emscripten_wasm_notify() functions.
volatile int32_t addr = 0;
void worker_main()
{
emscripten_console_log("worker_main");
emscripten_atomic_store_u32((void*)&addr, 1);
emscripten_console_log("Waiting on address with unexpected value should return 'not-equal'");
ATOMICS_WAIT_RESULT_T ret = emscripten_wasm_wait_i32((int32_t*)&addr, 2, /*timeout=*/-1);
assert(ret == ATOMICS_WAIT_NOT_EQUAL);
emscripten_console_log("Waiting on address with unexpected value should return 'not-equal' also if timeout==0");
ret = emscripten_wasm_wait_i32((int32_t*)&addr, 2, /*timeout=*/0);
assert(ret == ATOMICS_WAIT_NOT_EQUAL);
emscripten_console_log("Waiting for 0 nanoseconds should return 'timed-out'");
ret = emscripten_wasm_wait_i32((int32_t*)&addr, 1, /*timeout=*/0);
assert(ret == ATOMICS_WAIT_TIMED_OUT);
emscripten_console_log("Waiting for >0 nanoseconds should return 'timed-out'");
ret = emscripten_wasm_wait_i32((int32_t*)&addr, 1, /*timeout=*/1000);
assert(ret == ATOMICS_WAIT_TIMED_OUT);
emscripten_console_log("Waiting for infinitely long should return 'ok'");
ret = emscripten_wasm_wait_i32((int32_t*)&addr, 1, /*timeout=*/-1);
assert(ret == ATOMICS_WAIT_OK);
emscripten_console_log("Test finished");
#ifdef REPORT_RESULT
REPORT_RESULT(addr);
#endif
}
char stack[1024];
EM_BOOL main_loop(double time, void *userData)
{
if (addr == 1)
{
// Burn one second to make sure worker finishes its test.
emscripten_console_log("main: seen worker running");
double t0 = emscripten_performance_now();
while(emscripten_performance_now() < t0 + 1000);
// Wake the waiter
emscripten_console_log("main: waking worker");
addr = 2;
emscripten_wasm_notify((int32_t*)&addr, 1);
return EM_FALSE;
}
return EM_TRUE;
}
int main()
{
emscripten_console_log("main: creating worker");
emscripten_wasm_worker_t worker = emscripten_create_wasm_worker(stack, sizeof(stack));
emscripten_console_log("main: posting function");
emscripten_wasm_worker_post_function_v(worker, worker_main);
emscripten_console_log("main: entering timeout loop to wait for wasm worker to run");
emscripten_set_timeout_loop(main_loop, 1000, 0);
}