Skip to content

Commit fb18236

Browse files
authored
Move emscripten_wasm_worker_self_id to native code. NFC (#26468)
This will speed up this function significantly which is going to be important for hybrid pthreads + Wasm Workers builds where we need to be able to distinguish between Wasm Workers and non-Wasm Workers sometimes at the very local level. Note that this function returns 0 for all non-Wasm Workers, which includes both the main thread and pthreads. Also, move emscripten_current_thread_is_wasm_worker since that is trivially implemented on top of `_self_id()`
1 parent 6d9d7d4 commit fb18236

File tree

10 files changed

+49
-39
lines changed

10 files changed

+49
-39
lines changed

src/lib/libsigs.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -619,7 +619,6 @@ sigs = {
619619
emscripten_create_wasm_audio_worklet_processor_async__sig: 'vippp',
620620
emscripten_create_worker__sig: 'ip',
621621
emscripten_current_thread_is_audio_worklet__sig: 'i',
622-
emscripten_current_thread_is_wasm_worker__sig: 'i',
623622
emscripten_date_now__sig: 'd',
624623
emscripten_dbg__sig: 'vp',
625624
emscripten_dbg_backtrace__sig: 'vp',
@@ -853,7 +852,6 @@ sigs = {
853852
emscripten_wasm_worker_post_function_vi__sig: 'vipi',
854853
emscripten_wasm_worker_post_function_vii__sig: 'vipii',
855854
emscripten_wasm_worker_post_function_viii__sig: 'vipiii',
856-
emscripten_wasm_worker_self_id__sig: 'i',
857855
emscripten_webgl_commit_frame__sig: 'i',
858856
emscripten_webgl_create_context__sig: 'ppp',
859857
emscripten_webgl_destroy_context__sig: 'ip',

src/lib/libwasm_worker.js

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ addToLibrary({
126126
___set_stack_limits(wwParams.stackLowestAddress + wwParams.stackSize, wwParams.stackLowestAddress);
127127
#endif
128128
// Run the C side Worker initialization for stack and TLS.
129-
__emscripten_wasm_worker_initialize(wwParams.stackLowestAddress, wwParams.stackSize);
129+
__emscripten_wasm_worker_initialize(wwParams.wwID, wwParams.stackLowestAddress, wwParams.stackSize);
130130
#if PTHREADS
131131
// Record the pthread configuration, and whether this Wasm Worker supports synchronous blocking in emscripten_futex_wait().
132132
// (regular Wasm Workers do, AudioWorklets don't)
@@ -244,16 +244,6 @@ if (ENVIRONMENT_IS_WASM_WORKER
244244
_wasmWorkers = {};
245245
},
246246

247-
emscripten_current_thread_is_wasm_worker: () => {
248-
#if WASM_WORKERS
249-
return ENVIRONMENT_IS_WASM_WORKER;
250-
#else
251-
// implicit return 0;
252-
#endif
253-
},
254-
255-
emscripten_wasm_worker_self_id: () => wwParams?.wwID,
256-
257247
emscripten_wasm_worker_post_function_v: (id, funcPtr) => {
258248
_wasmWorkers[id].postMessage({'_wsc': funcPtr, 'x': [] }); // "WaSm Call"
259249
},

system/include/emscripten/wasm_worker.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,13 +67,15 @@ void emscripten_terminate_wasm_worker(emscripten_wasm_worker_t id);
6767
void emscripten_terminate_all_wasm_workers(void);
6868

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

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

7981
// emscripten_wasm_worker_post_function_*: Post a pointer to a C/C++ function to

system/lib/wasm_worker/library_wasm_worker.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,3 +76,7 @@ void emscripten_wasm_worker_sleep(int64_t nsecs) {
7676
int32_t addr = 0;
7777
emscripten_atomic_wait_u32(&addr, 0, nsecs);
7878
}
79+
80+
bool emscripten_current_thread_is_wasm_worker() {
81+
return emscripten_wasm_worker_self_id() != 0;
82+
}

system/lib/wasm_worker/wasm_worker_initialize.S

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,32 +14,47 @@
1414
.globaltype __tls_size, PTR, immutable
1515
.functype emscripten_stack_set_limits (PTR /*base*/, PTR /*end*/) -> ()
1616

17+
.section .__self_id,"",@
18+
.globaltype __self_id, i32
19+
__self_id:
20+
21+
.section .text,"",@
22+
23+
.globl emscripten_wasm_worker_self_id
24+
emscripten_wasm_worker_self_id:
25+
.functype emscripten_wasm_worker_self_id () -> (i32)
26+
global.get __self_id
27+
end_function
28+
1729
.globl _emscripten_wasm_worker_initialize
1830
_emscripten_wasm_worker_initialize:
19-
.functype _emscripten_wasm_worker_initialize (PTR /*stackLowestAddress*/, i32 /*stackSize*/) -> ()
31+
.functype _emscripten_wasm_worker_initialize (i32 /*selfID*/, PTR /*stackLowestAddress*/, i32 /*stackSize*/) -> ()
2032
.local PTR, PTR
2133

22-
// stack_end = stackLowestAddress + (__builtin_wasm_tls_size() + 15) & -16;
2334
local.get 0
35+
global.set __self_id
36+
37+
// stack_end = stackLowestAddress + (__builtin_wasm_tls_size() + 15) & -16;
38+
local.get 1
2439
global.get __tls_size
2540
PTR.add
2641
PTR.const 0xf
2742
PTR.add
2843
PTR.const -0x10
2944
PTR.and
30-
local.set 2
45+
local.set 3
3146

3247
// stack_base = stackLowestAddress + stackSize;
33-
local.get 0
3448
local.get 1
49+
local.get 2
3550
#ifdef __wasm64__
3651
i64.extend_i32_u
3752
#endif
3853
PTR.add
39-
local.set 3
54+
local.set 4
4055

56+
local.get 4
4157
local.get 3
42-
local.get 2
4358
// emscripten_stack_set_limits(stack_base, stack_end);
4459
call emscripten_stack_set_limits
4560

@@ -53,7 +68,7 @@ _emscripten_wasm_worker_initialize:
5368

5469

5570
// __wasm_init_tls(stackLowestAddress);
56-
local.get 0
71+
local.get 1
5772
.functype __wasm_init_tls (PTR) -> ()
5873
call __wasm_init_tls
5974

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

6479
// __stack_pointer = stack_base;
65-
local.get 3
80+
local.get 4
6681
global.set __stack_pointer
6782

6883
end_function

test/codesize/audio_worklet_wasm.expected.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ var K = [], L = a => {
134134
a = {
135135
latencyHint: c,
136136
sampleRate: G[a + 4 >> 2] || void 0,
137-
M: 0 > b ? "hardware" : b || "default"
137+
N: 0 > b ? "hardware" : b || "default"
138138
};
139139
} else a = void 0;
140140
a = new AudioContext(a);
@@ -204,7 +204,7 @@ var K = [], L = a => {
204204
});
205205
e.port.postMessage({
206206
_boot: 1,
207-
N: ba++,
207+
M: ba++,
208208
G: m.wasm,
209209
H: w,
210210
K: c,
@@ -246,7 +246,8 @@ function y() {
246246
C = a.n;
247247
Y = a.o;
248248
A = a.k;
249-
t ? (Y(u.K, u.F), p || (removeEventListener("message", M), K = K.forEach(L), addEventListener("message", L))) : a.i();
249+
t ? (Y(u.M, u.K, u.F), p || (removeEventListener("message", M), K = K.forEach(L),
250+
addEventListener("message", L))) : a.i();
250251
t || X();
251252
}));
252253
}

test/codesize/hello_wasm_worker_wasm.expected.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ function h() {
6767
y = b.g;
6868
z = b.i;
6969
l = b.h;
70-
d ? (z(e.m, e.o), removeEventListener("message", p), k = k.forEach(n), addEventListener("message", n)) : b.f();
70+
d ? (z(e.s, e.m, e.o), removeEventListener("message", p), k = k.forEach(n), addEventListener("message", n)) : b.f();
7171
d || y();
7272
}));
7373
}
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
{
22
"a.html": 515,
33
"a.html.gz": 355,
4-
"a.js": 4309,
5-
"a.js.gz": 2217,
6-
"a.wasm": 1329,
4+
"a.js": 4313,
5+
"a.js.gz": 2220,
6+
"a.wasm": 1324,
77
"a.wasm.gz": 892,
8-
"total": 6153,
9-
"total_gz": 3464
8+
"total": 6152,
9+
"total_gz": 3467
1010
}
Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
{
22
"a.html": 515,
33
"a.html.gz": 355,
4-
"a.js": 855,
5-
"a.js.gz": 543,
6-
"a.wasm": 1841,
7-
"a.wasm.gz": 1067,
8-
"total": 3211,
9-
"total_gz": 1965
4+
"a.js": 859,
5+
"a.js.gz": 545,
6+
"a.wasm": 1847,
7+
"a.wasm.gz": 1071,
8+
"total": 3221,
9+
"total_gz": 1971
1010
}

tools/emscripten.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1184,7 +1184,7 @@ def create_pointer_conversion_wrappers(metadata):
11841184
'_emscripten_thread_free_data': '_p',
11851185
'_emscripten_dlsync_self_async': '_p',
11861186
'_emscripten_proxy_dlsync_async': '_pp',
1187-
'_emscripten_wasm_worker_initialize': '_p_',
1187+
'_emscripten_wasm_worker_initialize': '__p_',
11881188
'_emscripten_proxy_poll_finish': '_pp_',
11891189
'_wasmfs_rename': '_pp',
11901190
'_wasmfs_readlink': '_pp',

0 commit comments

Comments
 (0)