Skip to content

Commit 18e0fad

Browse files
authored
Auto-generate signatures for pthread-related functions. NFC (#19112)
- Enable threading in tools/gen_sig_info.py - Ensure all thread-related JS functions are declared in header files - Prefix JS-only functions with `$` so that are not native accessible - Run `tools/gen_sig_info.py --remove`
1 parent f188340 commit 18e0fad

16 files changed

+93
-76
lines changed

src/jsifier.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ function ${name}(${args}) {
168168
snippet = modifyFunction(snippet, (name, args, body) => `
169169
function ${name}(${args}) {
170170
if (ENVIRONMENT_IS_PTHREAD)
171-
return _emscripten_proxy_to_main_thread_js(${proxiedFunctionTable.length}, ${+sync}${args ? ', ' : ''}${args});
171+
return proxyToMainThread(${proxiedFunctionTable.length}, ${+sync}${args ? ', ' : ''}${args});
172172
${body}
173173
}\n`);
174174
} else if (WASM_WORKERS && ASSERTIONS) {

src/library.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3001,7 +3001,11 @@ mergeInto(LibraryManager.library, {
30013001
return runEmAsmFunction(code, sigPtr, argbuf);
30023002
},
30033003

3004-
$runMainThreadEmAsm__deps: ['$readEmAsmArgs'],
3004+
$runMainThreadEmAsm__deps: ['$readEmAsmArgs',
3005+
#if PTHREADS
3006+
'$proxyToMainThread'
3007+
#endif
3008+
],
30053009
$runMainThreadEmAsm: function(code, sigPtr, argbuf, sync) {
30063010
var args = readEmAsmArgs(sigPtr, argbuf);
30073011
#if PTHREADS
@@ -3017,7 +3021,7 @@ mergeInto(LibraryManager.library, {
30173021
// code paths as similar as possible on both sides.)
30183022
// -1 - code is the encoding of a proxied EM_ASM, as a negative number
30193023
// (positive numbers are non-EM_ASM calls).
3020-
return _emscripten_proxy_to_main_thread_js.apply(null, [-1 - code, sync].concat(args));
3024+
return proxyToMainThread.apply(null, [-1 - code, sync].concat(args));
30213025
}
30223026
#endif
30233027
#if ASSERTIONS

src/library_html5.js

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2393,7 +2393,6 @@ var LibraryHTML5 = {
23932393
return {{{ cDefs.EMSCRIPTEN_RESULT_SUCCESS }}};
23942394
},
23952395

2396-
_emscripten_set_offscreencanvas_size__sig: 'iiii',
23972396
#if OFFSCREENCANVAS_SUPPORT
23982397
_emscripten_set_offscreencanvas_size: 'emscripten_set_canvas_element_size',
23992398

@@ -2479,8 +2478,8 @@ var LibraryHTML5 = {
24792478
},
24802479

24812480
#if PTHREADS
2482-
emscripten_get_canvas_element_size_calling_thread__deps: ['$JSEvents', '$findCanvasEventTarget'],
2483-
emscripten_get_canvas_element_size_calling_thread: function(target, width, height) {
2481+
$getCanvasSizeCallingThread__deps: ['$JSEvents', '$findCanvasEventTarget'],
2482+
$getCanvasSizeCallingThread: function(target, width, height) {
24842483
var canvas = findCanvasEventTarget(target);
24852484
if (!canvas) return {{{ cDefs.EMSCRIPTEN_RESULT_UNKNOWN_TARGET }}};
24862485

@@ -2510,18 +2509,19 @@ var LibraryHTML5 = {
25102509
return {{{ cDefs.EMSCRIPTEN_RESULT_SUCCESS }}};
25112510
},
25122511

2513-
emscripten_get_canvas_element_size_main_thread__proxy: 'sync',
2514-
emscripten_get_canvas_element_size_main_thread__sig: 'iiii',
2515-
emscripten_get_canvas_element_size_main_thread__deps: ['emscripten_get_canvas_element_size_calling_thread'],
2516-
emscripten_get_canvas_element_size_main_thread: function(target, width, height) { return _emscripten_get_canvas_element_size_calling_thread(target, width, height); },
2512+
$getCanvasSizeMainThread__proxy: 'sync',
2513+
$getCanvasSizeMainThread__deps: ['$getCanvasSizeCallingThread'],
2514+
$getCanvasSizeMainThread: function(target, width, height) {
2515+
return getCanvasSizeCallingThread(target, width, height);
2516+
},
25172517

2518-
emscripten_get_canvas_element_size__deps: ['$JSEvents', 'emscripten_get_canvas_element_size_calling_thread', 'emscripten_get_canvas_element_size_main_thread', '$findCanvasEventTarget'],
2518+
emscripten_get_canvas_element_size__deps: ['$JSEvents', '$getCanvasSizeCallingThread', '$getCanvasSizeMainThread', '$findCanvasEventTarget'],
25192519
emscripten_get_canvas_element_size: function(target, width, height) {
25202520
var canvas = findCanvasEventTarget(target);
25212521
if (canvas) {
2522-
return _emscripten_get_canvas_element_size_calling_thread(target, width, height);
2522+
return getCanvasSizeCallingThread(target, width, height);
25232523
}
2524-
return _emscripten_get_canvas_element_size_main_thread(target, width, height);
2524+
return getCanvasSizeMainThread(target, width, height);
25252525
},
25262526
#else
25272527
emscripten_get_canvas_element_size__deps: ['$JSEvents', '$findCanvasEventTarget'],

src/library_html5_webgl.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -321,8 +321,8 @@ var LibraryHtml5WebGL = {
321321
#if PTHREADS
322322
// Special function that will be invoked on the thread calling emscripten_webgl_destroy_context(), before routing
323323
// the call over to the target thread.
324-
emscripten_webgl_destroy_context_before_on_calling_thread__deps: ['emscripten_webgl_get_current_context', 'emscripten_webgl_make_context_current'],
325-
emscripten_webgl_destroy_context_before_on_calling_thread: function(contextHandle) {
324+
$emscripten_webgl_destroy_context_before_on_calling_thread__deps: ['emscripten_webgl_get_current_context', 'emscripten_webgl_make_context_current'],
325+
$emscripten_webgl_destroy_context_before_on_calling_thread: function(contextHandle) {
326326
if (_emscripten_webgl_get_current_context() == contextHandle) _emscripten_webgl_make_context_current(0);
327327
},
328328
#endif
@@ -592,8 +592,8 @@ function handleWebGLProxying(funcs) {
592592
var contextCheck = proxyContextHandle ? 'GL.contexts[p0]' : 'GLctx';
593593
var funcBody = `${retStatement} ${contextCheck} ? _${i}_calling_thread(${funcArgsString}) : _${i}_main_thread(${funcArgsString});`;
594594
if (funcs[i + '_before_on_calling_thread']) {
595-
funcs[i + '__deps'].push(i + '_before_on_calling_thread');
596-
funcBody = `_${i}_before_on_calling_thread(${funcArgsString}); ` + funcBody;
595+
funcs[i + '__deps'].push('$' + i + '_before_on_calling_thread');
596+
funcBody = `${i}_before_on_calling_thread(${funcArgsString}); ` + funcBody;
597597
}
598598
funcArgs.push(funcBody);
599599
funcs[i] = new (Function.prototype.bind.apply(Function, [Function].concat(funcArgs)));

src/library_pthread.js

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -562,7 +562,6 @@ var LibraryPThread = {
562562
worker.pthread_ptr = 0;
563563
},
564564

565-
__emscripten_thread_cleanup__sig: 'vp',
566565
__emscripten_thread_cleanup: function(thread) {
567566
// Called when a thread needs to be cleaned up so it can be reused.
568567
// A thread is considered reusable when it either returns from its
@@ -735,7 +734,6 @@ var LibraryPThread = {
735734
// allocations from __pthread_create_js we could also remove this.
736735
__pthread_create_js__noleakcheck: true,
737736
#endif
738-
__pthread_create_js__sig: 'ipppp',
739737
__pthread_create_js__deps: ['$spawnThread', 'pthread_self', '$pthreadCreateProxied',
740738
#if OFFSCREENCANVAS_SUPPORT
741739
'malloc',
@@ -947,9 +945,9 @@ var LibraryPThread = {
947945
_exit(returnCode);
948946
},
949947

950-
emscripten_proxy_to_main_thread_js__deps: ['$withStackSave', '_emscripten_run_in_main_runtime_thread_js'],
951-
emscripten_proxy_to_main_thread_js__docs: '/** @type{function(number, (number|boolean), ...(number|boolean))} */',
952-
emscripten_proxy_to_main_thread_js: function(index, sync) {
948+
$proxyToMainThread__deps: ['$withStackSave', '_emscripten_run_in_main_runtime_thread_js'],
949+
$proxyToMainThread__docs: '/** @type{function(number, (number|boolean), ...(number|boolean))} */',
950+
$proxyToMainThread: function(index, sync) {
953951
// Additional arguments are passed after those two, which are the actual
954952
// function arguments.
955953
// The serialization buffer contains the number of call params, and then
@@ -960,7 +958,7 @@ var LibraryPThread = {
960958
#if ASSERTIONS
961959
var maxArgs = {{{ cDefs.EM_QUEUED_JS_CALL_MAX_ARGS - 1 }}};
962960
if (numCallArgs > maxArgs) {
963-
throw 'emscripten_proxy_to_main_thread_js: Too many arguments ' + numCallArgs + ' to proxied function idx=' + index + ', maximum supported is ' + maxArgs;
961+
throw 'proxyToMainThread: Too many arguments ' + numCallArgs + ' to proxied function idx=' + index + ', maximum supported is ' + maxArgs;
964962
}
965963
#endif
966964
// Allocate a buffer, which will be copied by the C code.
@@ -994,29 +992,28 @@ var LibraryPThread = {
994992
});
995993
},
996994

997-
emscripten_receive_on_main_thread_js_callArgs: '=[]',
995+
$emscripten_receive_on_main_thread_js_callArgs: '=[]',
998996

999-
emscripten_receive_on_main_thread_js__sig: 'diip',
1000997
emscripten_receive_on_main_thread_js__deps: [
1001-
'emscripten_proxy_to_main_thread_js',
1002-
'emscripten_receive_on_main_thread_js_callArgs'],
998+
'$proxyToMainThread',
999+
'$emscripten_receive_on_main_thread_js_callArgs'],
10031000
emscripten_receive_on_main_thread_js: function(index, numCallArgs, args) {
10041001
#if WASM_BIGINT
10051002
numCallArgs /= 2;
10061003
#endif
1007-
_emscripten_receive_on_main_thread_js_callArgs.length = numCallArgs;
1004+
emscripten_receive_on_main_thread_js_callArgs.length = numCallArgs;
10081005
var b = args >> 3;
10091006
for (var i = 0; i < numCallArgs; i++) {
10101007
#if WASM_BIGINT
10111008
if (HEAP64[b + 2*i]) {
10121009
// It's a BigInt.
1013-
_emscripten_receive_on_main_thread_js_callArgs[i] = HEAP64[b + 2*i + 1];
1010+
emscripten_receive_on_main_thread_js_callArgs[i] = HEAP64[b + 2*i + 1];
10141011
} else {
10151012
// It's a Number.
1016-
_emscripten_receive_on_main_thread_js_callArgs[i] = HEAPF64[b + 2*i + 1];
1013+
emscripten_receive_on_main_thread_js_callArgs[i] = HEAPF64[b + 2*i + 1];
10171014
}
10181015
#else
1019-
_emscripten_receive_on_main_thread_js_callArgs[i] = HEAPF64[b + i];
1016+
emscripten_receive_on_main_thread_js_callArgs[i] = HEAPF64[b + i];
10201017
#endif
10211018
}
10221019
// Proxied JS library funcs are encoded as positive values, and
@@ -1030,7 +1027,7 @@ var LibraryPThread = {
10301027
#if ASSERTIONS
10311028
assert(func.length == numCallArgs, 'Call args mismatch in emscripten_receive_on_main_thread_js');
10321029
#endif
1033-
return func.apply(null, _emscripten_receive_on_main_thread_js_callArgs);
1030+
return func.apply(null, emscripten_receive_on_main_thread_js_callArgs);
10341031
},
10351032

10361033
// TODO(sbc): Do we really need this to be dynamically settable from JS like this?
@@ -1126,7 +1123,6 @@ var LibraryPThread = {
11261123
},
11271124

11281125
#if MAIN_MODULE
1129-
_emscripten_thread_exit_joinable__sig: 'vp',
11301126
_emscripten_thread_exit_joinable: function(thread) {
11311127
// Called when a thread exits and is joinable. We mark these threads
11321128
// as finished, which means that are in state where are no longer actually
@@ -1152,7 +1148,6 @@ var LibraryPThread = {
11521148
// TODO(sbc): Should we make a new form of __proxy attribute for JS library
11531149
// function that run asynchronously like but blocks the caller until they are
11541150
// done. Perhaps "sync_with_ctx"?
1155-
_emscripten_dlsync_threads_async__sig: 'vppp',
11561151
_emscripten_dlsync_threads_async__deps: ['_emscripten_proxy_dlsync_async', 'emscripten_promise_create', '$getPromise'],
11571152
_emscripten_dlsync_threads_async: function(caller, callback, ctx) {
11581153
#if PTHREADS_DEBUG
@@ -1234,7 +1229,6 @@ var LibraryPThread = {
12341229
},
12351230

12361231
_emscripten_thread_mailbox_await__deps: ['$checkMailbox'],
1237-
_emscripten_thread_mailbox_await__sig: 'vp',
12381232
_emscripten_thread_mailbox_await: function(pthread_ptr) {
12391233
if (typeof Atomics.waitAsync === 'function') {
12401234
// TODO: How to make this work with wasm64?
@@ -1249,7 +1243,6 @@ var LibraryPThread = {
12491243
},
12501244

12511245
_emscripten_notify_mailbox_postmessage__deps: ['$checkMailbox'],
1252-
_emscripten_notify_mailbox_postmessage__sig: 'vppp',
12531246
_emscripten_notify_mailbox_postmessage: function(targetThreadId,
12541247
currThreadId,
12551248
mainThreadId) {

src/library_sigs.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,11 @@ sigs = {
212212
__assert_fail__sig: 'vppip',
213213
__call_sighandler__sig: 'vpi',
214214
__dlsym__sig: 'pppp',
215+
__emscripten_init_main_thread_js__sig: 'vp',
216+
__emscripten_thread_cleanup__sig: 'vp',
215217
__handle_stack_overflow__sig: 'vp',
218+
__pthread_create_js__sig: 'ipppp',
219+
__pthread_kill_js__sig: 'ipi',
216220
__syscall__newselect__sig: 'iipppp',
217221
__syscall_accept4__sig: 'iippiii',
218222
__syscall_bind__sig: 'iippiii',
@@ -269,17 +273,24 @@ sigs = {
269273
_dlsym_catchup_js__sig: 'ppi',
270274
_dlsym_js__sig: 'pppp',
271275
_emscripten_dbg__sig: 'vp',
276+
_emscripten_default_pthread_stack_size__sig: 'i',
272277
_emscripten_dlopen_js__sig: 'vpppp',
278+
_emscripten_dlsync_threads__sig: 'v',
279+
_emscripten_dlsync_threads_async__sig: 'vppp',
273280
_emscripten_err__sig: 'vp',
274281
_emscripten_fetch_free__sig: 'vi',
275282
_emscripten_fetch_get_response_headers__sig: 'pipp',
276283
_emscripten_fetch_get_response_headers_length__sig: 'pi',
277284
_emscripten_fs_load_embedded_files__sig: 'vp',
278285
_emscripten_get_now_is_monotonic__sig: 'i',
279286
_emscripten_get_progname__sig: 'vpi',
287+
_emscripten_notify_mailbox_postmessage__sig: 'vppp',
280288
_emscripten_out__sig: 'vp',
281289
_emscripten_push_main_loop_blocker__sig: 'vppp',
282290
_emscripten_push_uncounted_main_loop_blocker__sig: 'vppp',
291+
_emscripten_set_offscreencanvas_size__sig: 'ipii',
292+
_emscripten_thread_exit_joinable__sig: 'vp',
293+
_emscripten_thread_mailbox_await__sig: 'vp',
283294
_emscripten_throw_longjmp__sig: 'v',
284295
_gmtime_js__sig: 'vpp',
285296
_localtime_js__sig: 'vpp',
@@ -419,6 +430,7 @@ sigs = {
419430
emscripten_call_worker__sig: 'vippipp',
420431
emscripten_cancel_animation_frame__sig: 'vi',
421432
emscripten_cancel_main_loop__sig: 'v',
433+
emscripten_check_blocking_allowed__sig: 'v',
422434
emscripten_clear_immediate__sig: 'vi',
423435
emscripten_clear_interval__sig: 'vi',
424436
emscripten_clear_timeout__sig: 'vi',
@@ -470,6 +482,7 @@ sigs = {
470482
emscripten_get_window_title__sig: 'p',
471483
emscripten_get_worker_queue_size__sig: 'ii',
472484
emscripten_has_asyncify__sig: 'i',
485+
emscripten_has_threading_support__sig: 'i',
473486
emscripten_hide_mouse__sig: 'v',
474487
emscripten_html5_remove_all_event_listeners__sig: 'v',
475488
emscripten_idb_async_delete__sig: 'vppppp',
@@ -512,6 +525,7 @@ sigs = {
512525
emscripten_math_tanh__sig: 'dd',
513526
emscripten_memcpy_big__sig: 'vppp',
514527
emscripten_notify_memory_growth__sig: 'vp',
528+
emscripten_num_logical_cores__sig: 'i',
515529
emscripten_pause_main_loop__sig: 'v',
516530
emscripten_pc_get_column__sig: 'ip',
517531
emscripten_pc_get_file__sig: 'pp',
@@ -525,6 +539,7 @@ sigs = {
525539
emscripten_promise_resolve__sig: 'vpip',
526540
emscripten_promise_then__sig: 'ppppp',
527541
emscripten_random__sig: 'f',
542+
emscripten_receive_on_main_thread_js__sig: 'diip',
528543
emscripten_request_animation_frame__sig: 'ipp',
529544
emscripten_request_animation_frame_loop__sig: 'vpp',
530545
emscripten_request_fullscreen__sig: 'ipi',

system/lib/libc/dynlink.c

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -179,21 +179,6 @@ static struct dso* load_library_start(const char* name, int flags) {
179179
// the module.
180180
#define ABORT_ON_SYNC_FAILURE 1
181181

182-
// These functions are defined in JS.
183-
184-
// Synchronous version of "dlsync_threads". Called only on the main thread.
185-
// Runs _emscripten_dlsync_self on each of the threads that are running at
186-
// the time of the call.
187-
void _emscripten_dlsync_threads();
188-
189-
// Asynchronous version of "dlsync_threads". Called only on the main thread.
190-
// Runs _emscripten_dlsync_self on each of the threads that are running at
191-
// the time of the call. Once this is done the callback is called with the
192-
// given em_proxying_ctx.
193-
void _emscripten_dlsync_threads_async(pthread_t calling_thread,
194-
void (*callback)(em_proxying_ctx*),
195-
em_proxying_ctx* ctx);
196-
197182
static void dlsync_next(struct dlevent* dlevent, em_promise_t promise);
198183

199184
static void sync_one_onsuccess(struct dso* dso, void* user_data) {

system/lib/libc/emscripten_internal.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
*/
1313

1414
#include <emscripten/em_macros.h>
15+
#include <emscripten/proxying.h>
16+
#include <emscripten/html5.h>
1517

1618
#include <signal.h> // for `sighandler_t`
1719
#include <stdbool.h> // for `bool`
@@ -93,6 +95,19 @@ void* _dlsym_catchup_js(struct dso* handle, int sym_index);
9395

9496
int _setitimer_js(int which, double timeout);
9597

98+
// Synchronous version of "dlsync_threads". Called only on the main thread.
99+
// Runs _emscripten_dlsync_self on each of the threads that are running at
100+
// the time of the call.
101+
void _emscripten_dlsync_threads();
102+
103+
// Asynchronous version of "dlsync_threads". Called only on the main thread.
104+
// Runs _emscripten_dlsync_self on each of the threads that are running at
105+
// the time of the call. Once this is done the callback is called with the
106+
// given em_proxying_ctx.
107+
void _emscripten_dlsync_threads_async(pthread_t calling_thread,
108+
void (*callback)(em_proxying_ctx*),
109+
em_proxying_ctx* ctx);
110+
96111
#ifdef _GNU_SOURCE
97112
void __call_sighandler(sighandler_t handler, int sig);
98113
#endif
@@ -114,6 +129,8 @@ size_t _emscripten_fetch_get_response_headers_length(int32_t fetchID);
114129
size_t _emscripten_fetch_get_response_headers(int32_t fetchID, char *dst, size_t dstSizeBytes);
115130
void _emscripten_fetch_free(unsigned int);
116131

132+
EMSCRIPTEN_RESULT _emscripten_set_offscreencanvas_size(const char *target, int width, int height);
133+
117134
#ifdef __cplusplus
118135
}
119136
#endif

system/lib/pthread/emscripten_thread_init.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88
#include "emscripten/threading.h"
99
#include "threading_internal.h"
1010

11-
void __set_thread_state(pthread_t ptr, int is_main, int is_runtime, int can_block);
12-
1311
void _emscripten_thread_init(pthread_t ptr,
1412
int is_main,
1513
int is_runtime,

system/lib/pthread/library_pthread.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
#include <emscripten/threading.h>
3737

3838
#include "threading_internal.h"
39+
#include "emscripten_internal.h"
3940

4041
int emscripten_pthread_attr_gettransferredcanvases(const pthread_attr_t* a, const char** str) {
4142
*str = a->_a_transferredcanvases;
@@ -176,9 +177,6 @@ void emscripten_async_waitable_close(em_queued_call* call) {
176177
em_queued_call_free(call);
177178
}
178179

179-
extern EMSCRIPTEN_RESULT _emscripten_set_offscreencanvas_size(const char *target, int width, int height);
180-
extern double emscripten_receive_on_main_thread_js(int functionIndex, int numCallArgs, double* args);
181-
182180
static void _do_call(void* arg) {
183181
em_queued_call* q = (em_queued_call*)arg;
184182
// C function pointer

0 commit comments

Comments
 (0)