Skip to content

Commit a18bbf7

Browse files
authored
Remove special case JS function proxying from library_pthread.c (#16405)
\The normal `__proxy` method for JS functions works fine in these cases and avoiding the special cases reduces the dependencies of `library_pthread.c` which is included in all threaded programs.
1 parent 48a1620 commit a18bbf7

15 files changed

+78
-87
lines changed

emcc.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2029,8 +2029,6 @@ def phase_linker_setup(options, state, newargs, user_settings):
20292029
'emscripten_main_thread_process_queued_calls',
20302030
'emscripten_run_in_main_runtime_thread_js',
20312031
'emscripten_stack_set_limits',
2032-
'emscripten_sync_run_in_main_thread_2',
2033-
'emscripten_sync_run_in_main_thread_4',
20342032
]
20352033

20362034
if settings.MAIN_MODULE:

src/library_html5_webgl.js

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,20 @@ var LibraryHtml5WebGL = {
4747

4848
_emscripten_webgl_power_preferences: "['default', 'low-power', 'high-performance']",
4949

50-
// In offscreen framebuffer mode, we implement these functions in C so that they enable
51-
// the proxying of GL commands. Otherwise, they are implemented here in JS.
52-
#if !(USE_PTHREADS && OFFSCREEN_FRAMEBUFFER)
50+
#if USE_PTHREADS && OFFSCREEN_FRAMEBUFFER
51+
// In offscreen framebuffer mode, we implement a proxied version of the
52+
// emscripten_webgl_create_context() function in JS.
53+
emscripten_webgl_create_context_proxied__proxy: 'sync',
54+
emscripten_webgl_create_context_proxied__deps: ['emscripten_webgl_do_create_context'],
55+
emscripten_webgl_create_context_proxied: function(target, attributes) {
56+
return _emscripten_webgl_do_create_context(target, attributes);
57+
},
58+
59+
// The other proxied GL commands are defined in C (guarded by the
60+
// __EMSCRIPTEN_OFFSCREEN_FRAMEBUFFER__ definition).
61+
#else
62+
// When not in offscreen framebuffer mode, these functions are implemented
63+
// in JS and forwarded without any proxying.
5364
emscripten_webgl_create_context__sig: 'iii',
5465
emscripten_webgl_create_context: 'emscripten_webgl_do_create_context',
5566

@@ -60,16 +71,16 @@ var LibraryHtml5WebGL = {
6071
emscripten_webgl_commit_frame: 'emscripten_webgl_do_commit_frame',
6172
#endif
6273

63-
// This code is called from the main proxying logic, which has a big switch
64-
// for all the messages, one of which is this GL-using one. This won't be
74+
// This code is called from emscripten_webgl_create_context() and proxied
75+
// to the main thread when in offscreen framebuffer mode. This won't be
6576
// called if GL is not linked in, but also make sure to not add a dep on
6677
// GL unnecessarily from here, as that would cause a linker error.
6778
emscripten_webgl_do_create_context__deps: [
6879
#if LibraryManager.has('library_webgl.js')
6980
'$GL',
7081
#endif
71-
#if (USE_PTHREADS && OFFSCREEN_FRAMEBUFFER)
72-
'emscripten_sync_run_in_main_thread_2',
82+
#if USE_PTHREADS && OFFSCREEN_FRAMEBUFFER
83+
'emscripten_webgl_create_context_proxied',
7384
#endif
7485
'$JSEvents', '_emscripten_webgl_power_preferences', '$findEventTarget', '$findCanvasEventTarget'],
7586
// This function performs proxying manually, depending on the style of context that is to be created.
@@ -104,7 +115,7 @@ var LibraryHtml5WebGL = {
104115
var targetStr = UTF8ToString(target);
105116
#endif
106117

107-
#if (USE_PTHREADS && OFFSCREEN_FRAMEBUFFER)
118+
#if USE_PTHREADS && OFFSCREEN_FRAMEBUFFER
108119
// Create a WebGL context that is proxied to main thread if canvas was not found on worker, or if explicitly requested to do so.
109120
if (ENVIRONMENT_IS_PTHREAD) {
110121
if (contextAttributes.proxyContextToMainThread === {{{ cDefine('EMSCRIPTEN_WEBGL_CONTEXT_PROXY_ALWAYS') }}} ||
@@ -122,7 +133,7 @@ var LibraryHtml5WebGL = {
122133
{{{ makeSetValue('attributes', C_STRUCTS.EmscriptenWebGLContextAttributes.renderViaOffscreenBackBuffer, '1', 'i32') }}};
123134
{{{ makeSetValue('attributes', C_STRUCTS.EmscriptenWebGLContextAttributes.preserveDrawingBuffer, '1', 'i32') }}};
124135
}
125-
return _emscripten_sync_run_in_main_thread_2({{{ cDefine('EM_PROXIED_CREATE_CONTEXT') }}}, target, attributes);
136+
return _emscripten_webgl_create_context_proxied(target, attributes);
126137
}
127138
}
128139
#endif
@@ -617,7 +628,7 @@ function handleWebGLProxying(funcs) {
617628
} else if (targetingOffscreenFramebuffer) {
618629
// When targeting only OFFSCREEN_FRAMEBUFFER, unconditionally proxy all GL calls to
619630
// main thread.
620-
funcs[i + '__proxy'] = 'sync';
631+
funcs[i + '__proxy'] = 'sync';
621632
} else {
622633
// Building without OFFSCREENCANVAS_SUPPORT or OFFSCREEN_FRAMEBUFFER; or building
623634
// with OFFSCREENCANVAS_SUPPORT and no OFFSCREEN_FRAMEBUFFER: the application

src/library_pthread.js

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ var LibraryPThread = {
3131
$PThread__deps: ['_emscripten_thread_init',
3232
'$killThread',
3333
'$cancelThread', '$cleanupThread', '$zeroMemory',
34-
'$ptrToString',
34+
'$ptrToString', '$spawnThread',
3535
'_emscripten_thread_free_data',
3636
'exit',
3737
#if !MINIMAL_RUNTIME
@@ -635,6 +635,13 @@ var LibraryPThread = {
635635
PThread.threadInit();
636636
},
637637

638+
$pthreadCreateProxied__internal: true,
639+
$pthreadCreateProxied__proxy: 'sync',
640+
$pthreadCreateProxied__deps: ['__pthread_create_js'],
641+
$pthreadCreateProxied: function(pthread_ptr, attr, start_routine, arg) {
642+
return ___pthread_create_js(pthread_ptr, attr, start_routine, arg);
643+
},
644+
638645
// ASan wraps the emscripten_builtin_pthread_create call in
639646
// __lsan::ScopedInterceptorDisabler. Unfortunately, that only disables it on
640647
// the thread that made the call. __pthread_create_js gets proxied to the
@@ -644,7 +651,7 @@ var LibraryPThread = {
644651
// allocations from __pthread_create_js we could also remove this.
645652
__pthread_create_js__noleakcheck: true,
646653
__pthread_create_js__sig: 'iiiii',
647-
__pthread_create_js__deps: ['$spawnThread', 'pthread_self', 'emscripten_sync_run_in_main_thread_4'],
654+
__pthread_create_js__deps: ['$spawnThread', 'pthread_self', '$pthreadCreateProxied'],
648655
__pthread_create_js: function(pthread_ptr, attr, start_routine, arg) {
649656
if (typeof SharedArrayBuffer == 'undefined') {
650657
err('Current environment does not support SharedArrayBuffer, pthreads are not available!');
@@ -759,7 +766,7 @@ var LibraryPThread = {
759766
// need to transfer ownership of objects, then proxy asynchronously via
760767
// postMessage.
761768
if (ENVIRONMENT_IS_PTHREAD && (transferList.length === 0 || error)) {
762-
return _emscripten_sync_run_in_main_thread_4({{{ cDefine('EM_PROXIED_PTHREAD_CREATE') }}}, pthread_ptr, attr, start_routine, arg);
769+
return pthreadCreateProxied(pthread_ptr, attr, start_routine, arg);
763770
}
764771

765772
// If on the main thread, and accessing Canvas/OffscreenCanvas failed, abort
@@ -892,7 +899,7 @@ var LibraryPThread = {
892899
#endif
893900
},
894901

895-
emscripten_proxy_to_main_thread_js__deps: ['emscripten_run_in_main_runtime_thread_js'],
902+
emscripten_proxy_to_main_thread_js__deps: ['$withStackSave', 'emscripten_run_in_main_runtime_thread_js'],
896903
emscripten_proxy_to_main_thread_js__docs: '/** @type{function(number, (number|boolean), ...(number|boolean))} */',
897904
emscripten_proxy_to_main_thread_js: function(index, sync) {
898905
// Additional arguments are passed after those two, which are the actual

src/modules.js

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -67,11 +67,6 @@ global.LibraryManager = {
6767
libraries.push('library_wget.js');
6868
}
6969

70-
if (USE_PTHREADS) { // TODO: Currently WebGL proxying makes pthreads library depend on WebGL.
71-
libraries.push('library_webgl.js');
72-
libraries.push('library_html5_webgl.js');
73-
}
74-
7570
if (EMSCRIPTEN_TRACING) {
7671
libraries.push('library_memoryprofiler.js');
7772
}

src/struct_info.json

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -997,8 +997,6 @@
997997
{
998998
"file": "emscripten/threading.h",
999999
"defines": [
1000-
"EM_PROXIED_PTHREAD_CREATE",
1001-
"EM_PROXIED_CREATE_CONTEXT",
10021000
"EM_PROXIED_RESIZE_OFFSCREENCANVAS",
10031001
"EM_QUEUED_JS_CALL_MAX_ARGS",
10041002
"EM_FUNC_SIG_V",

system/include/emscripten/threading.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -147,10 +147,8 @@ void *emscripten_sync_run_in_main_thread_7(int function, void *arg1, void *arg2,
147147
#define EM_FUNC_SIG_SPECIAL_INTERNAL (1 << 24)
148148
#define EM_PROXIED_FUNC_SPECIAL(x) (EM_FUNC_SIG_SPECIAL_INTERNAL | ((x) << 20))
149149

150-
#define EM_PROXIED_PTHREAD_CREATE (EM_PROXIED_FUNC_SPECIAL(0) | EM_FUNC_SIG_IIIII)
151-
#define EM_PROXIED_CREATE_CONTEXT (EM_PROXIED_FUNC_SPECIAL(2) | EM_FUNC_SIG_III)
152-
#define EM_PROXIED_RESIZE_OFFSCREENCANVAS (EM_PROXIED_FUNC_SPECIAL(3) | EM_FUNC_SIG_IIII)
153-
#define EM_PROXIED_JS_FUNCTION (EM_PROXIED_FUNC_SPECIAL(4) | EM_FUNC_SIG_D)
150+
#define EM_PROXIED_RESIZE_OFFSCREENCANVAS (EM_PROXIED_FUNC_SPECIAL(0) | EM_FUNC_SIG_IIII)
151+
#define EM_PROXIED_JS_FUNCTION (EM_PROXIED_FUNC_SPECIAL(1) | EM_FUNC_SIG_D)
154152

155153
// Runs the given function synchronously on the main Emscripten runtime thread.
156154
// If this thread is the main thread, the operation is immediately performed,

system/lib/pthread/library_pthread.c

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -177,20 +177,12 @@ void emscripten_async_waitable_close(em_queued_call* call) {
177177

178178
extern double emscripten_receive_on_main_thread_js(int functionIndex, int numCallArgs, double* args);
179179
extern int _emscripten_notify_thread_queue(pthread_t targetThreadId, pthread_t mainThreadId);
180-
extern int __pthread_create_js(struct pthread *thread, const pthread_attr_t *attr, void *(*start_routine) (void *), void *arg);
181180

182181
static void _do_call(void* arg) {
183182
em_queued_call* q = (em_queued_call*)arg;
184183
// C function pointer
185184
assert(EM_FUNC_SIG_NUM_FUNC_ARGUMENTS(q->functionEnum) <= EM_QUEUED_CALL_MAX_ARGS);
186185
switch (q->functionEnum) {
187-
case EM_PROXIED_PTHREAD_CREATE:
188-
q->returnValue.i =
189-
__pthread_create_js(q->args[0].vp, q->args[1].vp, q->args[2].vp, q->args[3].vp);
190-
break;
191-
case EM_PROXIED_CREATE_CONTEXT:
192-
q->returnValue.i = emscripten_webgl_create_context(q->args[0].cp, q->args[1].vp);
193-
break;
194186
case EM_PROXIED_RESIZE_OFFSCREENCANVAS:
195187
q->returnValue.i =
196188
emscripten_set_canvas_element_size(q->args[0].cp, q->args[1].i, q->args[2].i);

tests/other/metadce/minimal_main_Oz_USE_PTHREADS_PROXY_TO_PTHREAD.exports

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,7 @@ E
66
F
77
G
88
H
9-
I
10-
J
11-
K
9+
o
1210
p
1311
q
1412
r

tests/other/metadce/minimal_main_Oz_USE_PTHREADS_PROXY_TO_PTHREAD.funcs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,6 @@ $emscripten_main_thread_process_queued_calls
4343
$emscripten_proxy_main
4444
$emscripten_run_in_main_runtime_thread_js
4545
$emscripten_stack_set_limits
46-
$emscripten_sync_run_in_main_thread
47-
$emscripten_sync_run_in_main_thread
4846
$emscripten_tls_init
4947
$init_file_lock
5048
$init_mparams

tests/other/metadce/minimal_main_Oz_USE_PTHREADS_PROXY_TO_PTHREAD.imports

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,3 @@ a.k
1212
a.l
1313
a.m
1414
a.n
15-
a.o

0 commit comments

Comments
 (0)