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
2 changes: 0 additions & 2 deletions src/lib/libsigs.js
Original file line number Diff line number Diff line change
Expand Up @@ -619,7 +619,6 @@ sigs = {
emscripten_create_wasm_audio_worklet_processor_async__sig: 'vippp',
emscripten_create_worker__sig: 'ip',
emscripten_current_thread_is_audio_worklet__sig: 'i',
emscripten_current_thread_is_wasm_worker__sig: 'i',
emscripten_date_now__sig: 'd',
emscripten_dbg__sig: 'vp',
emscripten_dbg_backtrace__sig: 'vp',
Expand Down Expand Up @@ -853,7 +852,6 @@ sigs = {
emscripten_wasm_worker_post_function_vi__sig: 'vipi',
emscripten_wasm_worker_post_function_vii__sig: 'vipii',
emscripten_wasm_worker_post_function_viii__sig: 'vipiii',
emscripten_wasm_worker_self_id__sig: 'i',
emscripten_webgl_commit_frame__sig: 'i',
emscripten_webgl_create_context__sig: 'ppp',
emscripten_webgl_destroy_context__sig: 'ip',
Expand Down
12 changes: 1 addition & 11 deletions src/lib/libwasm_worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ addToLibrary({
___set_stack_limits(wwParams.stackLowestAddress + wwParams.stackSize, wwParams.stackLowestAddress);
#endif
// Run the C side Worker initialization for stack and TLS.
__emscripten_wasm_worker_initialize(wwParams.stackLowestAddress, wwParams.stackSize);
__emscripten_wasm_worker_initialize(wwParams.wwID, wwParams.stackLowestAddress, wwParams.stackSize);
#if PTHREADS
// Record the pthread configuration, and whether this Wasm Worker supports synchronous blocking in emscripten_futex_wait().
// (regular Wasm Workers do, AudioWorklets don't)
Expand Down Expand Up @@ -241,16 +241,6 @@ if (ENVIRONMENT_IS_WASM_WORKER
_wasmWorkers = {};
},

emscripten_current_thread_is_wasm_worker: () => {
#if WASM_WORKERS
return ENVIRONMENT_IS_WASM_WORKER;
#else
// implicit return 0;
#endif
},

emscripten_wasm_worker_self_id: () => wwParams?.wwID,

emscripten_wasm_worker_post_function_v: (id, funcPtr) => {
_wasmWorkers[id].postMessage({'_wsc': funcPtr, 'x': [] }); // "WaSm Call"
},
Expand Down
6 changes: 4 additions & 2 deletions system/include/emscripten/wasm_worker.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,15 @@ void emscripten_terminate_wasm_worker(emscripten_wasm_worker_t id);
void emscripten_terminate_all_wasm_workers(void);

// Returns true if the current thread is executing a Wasm Worker, false
// otherwise. Note that calling this function can be relatively slow as it
// incurs a Wasm->JS transition, so avoid calling it in hot paths.
// otherwise.
bool emscripten_current_thread_is_wasm_worker(void);

// Returns a unique ID that identifies the calling Wasm Worker. Similar to
// pthread_self(). The main browser thread will return 0 as the ID. First Wasm
// Worker will return 1, and so on.
// Note: This function also returns 0 when called from other non-Wasm Worker
// contexts, such as pthreads in a program built with both pthread and Wasm
// Worker support.
uint32_t emscripten_wasm_worker_self_id(void);

// emscripten_wasm_worker_post_function_*: Post a pointer to a C/C++ function to
Expand Down
4 changes: 4 additions & 0 deletions system/lib/wasm_worker/library_wasm_worker.c
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,7 @@ void emscripten_wasm_worker_sleep(int64_t nsecs) {
int32_t addr = 0;
emscripten_atomic_wait_u32(&addr, 0, nsecs);
}

bool emscripten_current_thread_is_wasm_worker() {
return emscripten_wasm_worker_self_id() != 0;
}
31 changes: 23 additions & 8 deletions system/lib/wasm_worker/wasm_worker_initialize.S
Original file line number Diff line number Diff line change
Expand Up @@ -14,32 +14,47 @@
.globaltype __tls_size, PTR, immutable
.functype emscripten_stack_set_limits (PTR /*base*/, PTR /*end*/) -> ()

.section .__self_id,"",@
.globaltype __self_id, i32
__self_id:

.section .text,"",@

.globl emscripten_wasm_worker_self_id
emscripten_wasm_worker_self_id:
.functype emscripten_wasm_worker_self_id () -> (i32)
global.get __self_id
end_function

.globl _emscripten_wasm_worker_initialize
_emscripten_wasm_worker_initialize:
.functype _emscripten_wasm_worker_initialize (PTR /*stackLowestAddress*/, i32 /*stackSize*/) -> ()
.functype _emscripten_wasm_worker_initialize (i32 /*selfID*/, PTR /*stackLowestAddress*/, i32 /*stackSize*/) -> ()
.local PTR, PTR

// stack_end = stackLowestAddress + (__builtin_wasm_tls_size() + 15) & -16;
local.get 0
global.set __self_id

// stack_end = stackLowestAddress + (__builtin_wasm_tls_size() + 15) & -16;
local.get 1
global.get __tls_size
PTR.add
PTR.const 0xf
PTR.add
PTR.const -0x10
PTR.and
local.set 2
local.set 3

// stack_base = stackLowestAddress + stackSize;
local.get 0
local.get 1
local.get 2
#ifdef __wasm64__
i64.extend_i32_u
#endif
PTR.add
local.set 3
local.set 4

local.get 4
local.get 3
local.get 2
// emscripten_stack_set_limits(stack_base, stack_end);
call emscripten_stack_set_limits

Expand All @@ -53,7 +68,7 @@ _emscripten_wasm_worker_initialize:


// __wasm_init_tls(stackLowestAddress);
local.get 0
local.get 1
.functype __wasm_init_tls (PTR) -> ()
call __wasm_init_tls

Expand All @@ -62,7 +77,7 @@ _emscripten_wasm_worker_initialize:
// So we must initialize __stack_pointer only *after* completing __wasm_init_tls:

// __stack_pointer = stack_base;
local.get 3
local.get 4
global.set __stack_pointer

end_function
7 changes: 4 additions & 3 deletions test/codesize/audio_worklet_wasm.expected.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ var K = [], L = a => {
a = {
latencyHint: c,
sampleRate: G[a + 4 >> 2] || void 0,
M: 0 > b ? "hardware" : b || "default"
N: 0 > b ? "hardware" : b || "default"
};
} else a = void 0;
a = new AudioContext(a);
Expand Down Expand Up @@ -204,7 +204,7 @@ var K = [], L = a => {
});
e.port.postMessage({
_boot: 1,
N: ba++,
M: ba++,
G: m.wasm,
H: w,
K: c,
Expand Down Expand Up @@ -246,7 +246,8 @@ function y() {
C = a.n;
Y = a.o;
A = a.k;
t ? (Y(u.K, u.F), p || (removeEventListener("message", M), K = K.forEach(L), addEventListener("message", L))) : a.i();
t ? (Y(u.M, u.K, u.F), p || (removeEventListener("message", M), K = K.forEach(L),
addEventListener("message", L))) : a.i();
t || X();
}));
}
Expand Down
2 changes: 1 addition & 1 deletion test/codesize/hello_wasm_worker_wasm.expected.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ function h() {
y = b.g;
z = b.i;
l = b.h;
d ? (z(e.m, e.o), removeEventListener("message", p), k = k.forEach(n), addEventListener("message", n)) : b.f();
d ? (z(e.s, e.m, e.o), removeEventListener("message", p), k = k.forEach(n), addEventListener("message", n)) : b.f();
d || y();
}));
}
Expand Down
10 changes: 5 additions & 5 deletions test/codesize/test_minimal_runtime_code_size_audio_worklet.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"a.html": 515,
"a.html.gz": 355,
"a.js": 4309,
"a.js.gz": 2217,
"a.wasm": 1329,
"a.js": 4313,
"a.js.gz": 2220,
"a.wasm": 1324,
"a.wasm.gz": 892,
"total": 6153,
"total_gz": 3464
"total": 6152,
"total_gz": 3467
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"a.html": 515,
"a.html.gz": 355,
"a.js": 855,
"a.js.gz": 543,
"a.wasm": 1841,
"a.wasm.gz": 1067,
"total": 3211,
"total_gz": 1965
"a.js": 859,
"a.js.gz": 545,
"a.wasm": 1847,
"a.wasm.gz": 1071,
"total": 3221,
"total_gz": 1971
}
2 changes: 1 addition & 1 deletion tools/emscripten.py
Original file line number Diff line number Diff line change
Expand Up @@ -1184,7 +1184,7 @@ def create_pointer_conversion_wrappers(metadata):
'_emscripten_thread_free_data': '_p',
'_emscripten_dlsync_self_async': '_p',
'_emscripten_proxy_dlsync_async': '_pp',
'_emscripten_wasm_worker_initialize': '_p_',
'_emscripten_wasm_worker_initialize': '__p_',
'_emscripten_proxy_poll_finish': '_pp_',
'_wasmfs_rename': '_pp',
'_wasmfs_readlink': '_pp',
Expand Down
Loading