diff --git a/src/lib/libsigs.js b/src/lib/libsigs.js index d7c8a2aac466e..49358dbb2a6e2 100644 --- a/src/lib/libsigs.js +++ b/src/lib/libsigs.js @@ -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', @@ -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', diff --git a/src/lib/libwasm_worker.js b/src/lib/libwasm_worker.js index 1cd0bc41c1e1f..974e5fa43d3d1 100644 --- a/src/lib/libwasm_worker.js +++ b/src/lib/libwasm_worker.js @@ -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) @@ -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" }, diff --git a/system/include/emscripten/wasm_worker.h b/system/include/emscripten/wasm_worker.h index 37d66c76a6584..c22aa582faf61 100644 --- a/system/include/emscripten/wasm_worker.h +++ b/system/include/emscripten/wasm_worker.h @@ -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 diff --git a/system/lib/wasm_worker/library_wasm_worker.c b/system/lib/wasm_worker/library_wasm_worker.c index cccad5494c8af..a1327c3700b13 100644 --- a/system/lib/wasm_worker/library_wasm_worker.c +++ b/system/lib/wasm_worker/library_wasm_worker.c @@ -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; +} diff --git a/system/lib/wasm_worker/wasm_worker_initialize.S b/system/lib/wasm_worker/wasm_worker_initialize.S index 59861b614892e..58b42de5371ab 100644 --- a/system/lib/wasm_worker/wasm_worker_initialize.S +++ b/system/lib/wasm_worker/wasm_worker_initialize.S @@ -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 @@ -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 @@ -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 diff --git a/test/codesize/audio_worklet_wasm.expected.js b/test/codesize/audio_worklet_wasm.expected.js index c68efe128e45e..4928bfccd9542 100644 --- a/test/codesize/audio_worklet_wasm.expected.js +++ b/test/codesize/audio_worklet_wasm.expected.js @@ -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); @@ -204,7 +204,7 @@ var K = [], L = a => { }); e.port.postMessage({ _boot: 1, - N: ba++, + M: ba++, G: m.wasm, H: w, K: c, @@ -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(); })); } diff --git a/test/codesize/hello_wasm_worker_wasm.expected.js b/test/codesize/hello_wasm_worker_wasm.expected.js index 377c6a7eb46e4..2a61cfc803f62 100644 --- a/test/codesize/hello_wasm_worker_wasm.expected.js +++ b/test/codesize/hello_wasm_worker_wasm.expected.js @@ -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(); })); } diff --git a/test/codesize/test_minimal_runtime_code_size_audio_worklet.json b/test/codesize/test_minimal_runtime_code_size_audio_worklet.json index 1e96b591a9c82..7fd4a41593664 100644 --- a/test/codesize/test_minimal_runtime_code_size_audio_worklet.json +++ b/test/codesize/test_minimal_runtime_code_size_audio_worklet.json @@ -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 } diff --git a/test/codesize/test_minimal_runtime_code_size_hello_wasm_worker.json b/test/codesize/test_minimal_runtime_code_size_hello_wasm_worker.json index e8e1006c442c7..9f2ca39bb3535 100644 --- a/test/codesize/test_minimal_runtime_code_size_hello_wasm_worker.json +++ b/test/codesize/test_minimal_runtime_code_size_hello_wasm_worker.json @@ -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 } diff --git a/tools/emscripten.py b/tools/emscripten.py index 500bb25355564..ddc4bd9747b96 100644 --- a/tools/emscripten.py +++ b/tools/emscripten.py @@ -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',