Skip to content

Commit f641d74

Browse files
authored
Rename emscripten_main_browser_thread_id to emscripten_main_runtime_thread_id. NFC (#18872)
The better reflects that is actually being returned here. In fact, there may be no main browser thread at all, and we have never tracked the thread ID of the main browser thread, only the main runtime thread. Use a macro to continue to support the old name, but mark is as deprecated so folks will get a warning. Also, update the corresponding EM_CALLBACK_THREAD_CONTEXT_MAIN_BROWSER_THREAD macro.
1 parent 5763b39 commit f641d74

20 files changed

+65
-35
lines changed

emcc.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1682,7 +1682,7 @@ def setup_pthreads(target):
16821682
settings.REQUIRED_EXPORTS += [
16831683
'emscripten_dispatch_to_thread_',
16841684
'_emscripten_thread_free_data',
1685-
'emscripten_main_browser_thread_id',
1685+
'emscripten_main_runtime_thread_id',
16861686
'emscripten_main_thread_process_queued_calls',
16871687
'_emscripten_run_in_main_runtime_thread_js',
16881688
'emscripten_stack_set_limits',

src/library_html5.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ var LibraryHTML5 = {
202202
#if USE_PTHREADS
203203
getTargetThreadForEventCallback: function(targetThread) {
204204
switch (targetThread) {
205-
case {{{ cDefine('EM_CALLBACK_THREAD_CONTEXT_MAIN_BROWSER_THREAD') }}}: return 0; // The event callback for the current event should be called on the main browser thread. (0 == don't proxy)
205+
case {{{ cDefine('EM_CALLBACK_THREAD_CONTEXT_MAIN_RUNTIME_THREAD') }}}: return 0; // The event callback for the current event should be called on the main browser thread. (0 == don't proxy)
206206
case {{{ cDefine('EM_CALLBACK_THREAD_CONTEXT_CALLING_THREAD') }}}: return PThread.currentProxiedOperationCallerThread; // The event callback for the current event should be backproxied to the thread that is registering the event.
207207
default: return targetThread; // The event callback for the current event should be proxied to the given specific thread.
208208
}
@@ -2311,7 +2311,7 @@ var LibraryHTML5 = {
23112311
if (typeof onbeforeunload == 'undefined') return {{{ cDefine('EMSCRIPTEN_RESULT_NOT_SUPPORTED') }}};
23122312
// beforeunload callback can only be registered on the main browser thread, because the page will go away immediately after returning from the handler,
23132313
// and there is no time to start proxying it anywhere.
2314-
if (targetThread !== {{{ cDefine('EM_CALLBACK_THREAD_CONTEXT_MAIN_BROWSER_THREAD') }}}) return {{{ cDefine('EMSCRIPTEN_RESULT_INVALID_PARAM') }}};
2314+
if (targetThread !== {{{ cDefine('EM_CALLBACK_THREAD_CONTEXT_MAIN_RUNTIME_THREAD') }}}) return {{{ cDefine('EMSCRIPTEN_RESULT_INVALID_PARAM') }}};
23152315
registerBeforeUnloadEventCallback({{{ cDefine('EMSCRIPTEN_EVENT_TARGET_WINDOW') }}}, userData, true, callbackfunc, {{{ cDefine('EMSCRIPTEN_EVENT_BEFOREUNLOAD') }}}, "beforeunload");
23162316
return {{{ cDefine('EMSCRIPTEN_RESULT_SUCCESS') }}};
23172317
},

src/library_pthread.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -916,7 +916,6 @@ var LibraryPThread = {
916916
#endif
917917
},
918918

919-
__pthread_kill_js__deps: ['emscripten_main_browser_thread_id'],
920919
__pthread_kill_js: function(thread, signal) {
921920
if (signal === {{{ cDefine('SIGCANCEL') }}}) { // Used by pthread_cancel in musl
922921
if (!ENVIRONMENT_IS_PTHREAD) cancelThread(thread);

src/struct_info.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -738,7 +738,7 @@
738738
"EMSCRIPTEN_WEBGL_CONTEXT_PROXY_FALLBACK",
739739
"EMSCRIPTEN_WEBGL_CONTEXT_PROXY_ALWAYS",
740740

741-
"EM_CALLBACK_THREAD_CONTEXT_MAIN_BROWSER_THREAD",
741+
"EM_CALLBACK_THREAD_CONTEXT_MAIN_RUNTIME_THREAD",
742742
"EM_CALLBACK_THREAD_CONTEXT_CALLING_THREAD",
743743

744744
"EMSCRIPTEN_DEVICE_MOTION_EVENT_SUPPORTS_ACCELERATION",

src/threadprofiler.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ var emscriptenThreadProfiler = {
3333
},
3434

3535
dumpState: function dumpState() {
36-
var mainThread = _emscripten_main_browser_thread_id();
36+
var mainThread = _emscripten_main_runtime_thread_id();
3737

3838
var threads = [mainThread];
3939
for (var i in PThread.pthreads) {
@@ -60,7 +60,7 @@ var emscriptenThreadProfiler = {
6060
return;
6161
}
6262
var str = '';
63-
var mainThread = _emscripten_main_browser_thread_id();
63+
var mainThread = _emscripten_main_runtime_thread_id();
6464

6565
var threads = [mainThread];
6666
for (var i in PThread.pthreads) {

system/include/emscripten/html5.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -434,9 +434,12 @@ EMSCRIPTEN_RESULT emscripten_get_element_css_size(const char *target, double *wi
434434

435435
void emscripten_html5_remove_all_event_listeners(void);
436436

437-
#define EM_CALLBACK_THREAD_CONTEXT_MAIN_BROWSER_THREAD ((pthread_t)0x1)
437+
#define EM_CALLBACK_THREAD_CONTEXT_MAIN_RUNTIME_THREAD ((pthread_t)0x1)
438438
#define EM_CALLBACK_THREAD_CONTEXT_CALLING_THREAD ((pthread_t)0x2)
439439

440+
// Legacy name for EM_CALLBACK_THREAD_CONTEXT_MAIN_RUNTIME_THREAD
441+
#define EM_CALLBACK_THREAD_CONTEXT_MAIN_BROWSER_THREAD EM_CALLBACK_THREAD_CONTEXT_MAIN_RUNTIME_THREAD
442+
440443
#define emscripten_set_keypress_callback(target, userData, useCapture, callback) emscripten_set_keypress_callback_on_thread( (target), (userData), (useCapture), (callback), EM_CALLBACK_THREAD_CONTEXT_CALLING_THREAD)
441444
#define emscripten_set_keydown_callback(target, userData, useCapture, callback) emscripten_set_keydown_callback_on_thread( (target), (userData), (useCapture), (callback), EM_CALLBACK_THREAD_CONTEXT_CALLING_THREAD)
442445
#define emscripten_set_keyup_callback(target, userData, useCapture, callback) emscripten_set_keyup_callback_on_thread( (target), (userData), (useCapture), (callback), EM_CALLBACK_THREAD_CONTEXT_CALLING_THREAD)
@@ -471,7 +474,7 @@ void emscripten_html5_remove_all_event_listeners(void);
471474
#define emscripten_set_gamepaddisconnected_callback(userData, useCapture, callback) emscripten_set_gamepaddisconnected_callback_on_thread( (userData), (useCapture), (callback), EM_CALLBACK_THREAD_CONTEXT_CALLING_THREAD)
472475
#define emscripten_set_batterychargingchange_callback(userData, callback) emscripten_set_batterychargingchange_callback_on_thread( (userData), (callback), EM_CALLBACK_THREAD_CONTEXT_CALLING_THREAD)
473476
#define emscripten_set_batterylevelchange_callback(userData, callback) emscripten_set_batterylevelchange_callback_on_thread( (userData), (callback), EM_CALLBACK_THREAD_CONTEXT_CALLING_THREAD)
474-
#define emscripten_set_beforeunload_callback(userData, callback) emscripten_set_beforeunload_callback_on_thread( (userData), (callback), EM_CALLBACK_THREAD_CONTEXT_MAIN_BROWSER_THREAD)
477+
#define emscripten_set_beforeunload_callback(userData, callback) emscripten_set_beforeunload_callback_on_thread( (userData), (callback), EM_CALLBACK_THREAD_CONTEXT_MAIN_RUNTIME_THREAD)
475478

476479
long emscripten_request_animation_frame(EM_BOOL (*cb)(double time, void *userData), void *userData);
477480
void emscripten_cancel_animation_frame(long requestAnimationFrameId);

system/include/emscripten/threading.h

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -218,10 +218,13 @@ int emscripten_dispatch_to_thread_async_(pthread_t target_thread,
218218
emscripten_dispatch_to_thread_async_( \
219219
(target_thread), (sig), (void*)(func_ptr), (satellite), ##__VA_ARGS__)
220220

221-
// Returns 1 if the current thread is the thread that hosts the Emscripten runtime.
221+
// Returns 1 if the current thread is the thread that hosts the Emscripten
222+
// runtime.
222223
int emscripten_is_main_runtime_thread(void);
223224

224-
// Returns 1 if the current thread is the main browser thread.
225+
// Returns 1 if the current thread is the main browser thread. In the case that
226+
// the emscripten module is run in a worker there may be no pthread for which
227+
// this returns 1.
225228
int emscripten_is_main_browser_thread(void);
226229

227230
// A temporary workaround to issue
@@ -233,7 +236,11 @@ void emscripten_main_thread_process_queued_calls(void);
233236

234237
void emscripten_current_thread_process_queued_calls(void);
235238

236-
pthread_t emscripten_main_browser_thread_id(void);
239+
// Returns the thread ID of the thread that hosts the Emscripten runtime.
240+
pthread_t emscripten_main_runtime_thread_id(void);
241+
242+
#define emscripten_main_browser_thread_id() emscripten_main_runtime_thread_id()
243+
#pragma clang deprecated(emscripten_main_browser_thread_id, "use emscripten_main_runtime_thread_id instead")
237244

238245
// Synchronously sleeps the calling thread for the given number of milliseconds.
239246
// Note: Calling this on the main browser thread is _very_ _very_ bad for

system/lib/libc/dynlink.c

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -400,9 +400,8 @@ static void run_dlsync_async(em_proxying_ctx* ctx, void* arg) {
400400
static void dlsync() {
401401
// Call dlsync process. This call will block until all threads are in sync.
402402
// This gets called after a shared library is loaded by a worker.
403-
pthread_t main_thread = emscripten_main_browser_thread_id();
404-
dbg("dlsync main=%p", main_thread);
405-
if (pthread_self() == main_thread) {
403+
dbg("dlsync main=%p", emscripten_main_runtime_thread_id());
404+
if (emscripten_is_main_runtime_thread()) {
406405
// dlsync was called on the main thread. In this case we have no choice by
407406
// to run the blocking version of emscripten_dlsync_threads.
408407
_emscripten_dlsync_threads();
@@ -411,7 +410,7 @@ static void dlsync() {
411410
// thread.
412411
em_proxying_queue* q = emscripten_proxy_get_system_queue();
413412
int success = emscripten_proxy_sync_with_ctx(
414-
q, main_thread, run_dlsync_async, pthread_self());
413+
q, emscripten_main_runtime_thread_id(), run_dlsync_async, pthread_self());
415414
assert(success);
416415
}
417416
}

system/lib/libc/musl/src/thread/pthread_cond_timedwait.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ int __pthread_cond_timedwait(pthread_cond_t *restrict c, pthread_mutex_t *restri
7777

7878
#ifdef __EMSCRIPTEN__
7979
// TODO: Optimize this away in MINIMAL_RUNTIME.
80-
if (pthread_self() == emscripten_main_browser_thread_id()) {
80+
if (emscripten_is_main_browser_thread()) {
8181
emscripten_check_blocking_allowed();
8282
}
8383
#endif

system/lib/pthread/em_task_queue.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ void em_task_queue_notify(em_task_queue* queue) {
124124
if (previous != NOTIFICATION_PENDING) {
125125
_emscripten_notify_task_queue(queue->thread,
126126
pthread_self(),
127-
emscripten_main_browser_thread_id(),
127+
emscripten_main_runtime_thread_id(),
128128
queue);
129129
}
130130
}

system/lib/pthread/library_pthread.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -352,14 +352,14 @@ EMSCRIPTEN_RESULT emscripten_wait_for_call_i(
352352

353353
static struct pthread __main_pthread;
354354

355-
pthread_t emscripten_main_browser_thread_id() {
355+
pthread_t emscripten_main_runtime_thread_id() {
356356
return &__main_pthread;
357357
}
358358

359359
static pthread_t normalize_thread(pthread_t target_thread) {
360360
assert(target_thread);
361-
if (target_thread == EM_CALLBACK_THREAD_CONTEXT_MAIN_BROWSER_THREAD) {
362-
return emscripten_main_browser_thread_id();
361+
if (target_thread == EM_CALLBACK_THREAD_CONTEXT_MAIN_RUNTIME_THREAD) {
362+
return emscripten_main_runtime_thread_id();
363363
}
364364
if (target_thread == EM_CALLBACK_THREAD_CONTEXT_CALLING_THREAD) {
365365
return pthread_self();
@@ -392,7 +392,7 @@ static int do_dispatch_to_thread(pthread_t target_thread,
392392
}
393393

394394
void emscripten_async_run_in_main_thread(em_queued_call* call) {
395-
do_dispatch_to_thread(emscripten_main_browser_thread_id(), call);
395+
do_dispatch_to_thread(emscripten_main_runtime_thread_id(), call);
396396
}
397397

398398
static void sync_run_in_main_thread(em_queued_call* call) {

system/lib/pthread/pthread_create.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,7 @@ void _emscripten_thread_exit(void* result) {
319319

320320
__tl_unlock();
321321

322-
if (self == emscripten_main_browser_thread_id()) {
322+
if (emscripten_is_main_runtime_thread()) {
323323
exit(0);
324324
return;
325325
}

system/lib/pthread/pthread_kill.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ int pthread_kill(pthread_t t, int sig) {
1616
if (sig < 0 || sig >= _NSIG) {
1717
return EINVAL;
1818
}
19-
if (t == emscripten_main_browser_thread_id()) {
19+
if (t == emscripten_main_runtime_thread_id()) {
2020
if (sig == 0) return 0; // signal == 0 is a no-op.
2121
return ESRCH;
2222
}

system/lib/pthread/pthread_self_stub.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ static int dummy_getpid() {
2020
}
2121
weak_alias(dummy_getpid, __syscall_getpid);
2222

23-
pthread_t emscripten_main_browser_thread_id() {
23+
pthread_t emscripten_main_runtime_thread_id() {
2424
return &__main_pthread;
2525
}
2626

system/lib/websocket/websocket_to_posix_socket.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ EMSCRIPTEN_WEBSOCKET_T emscripten_init_websocket_to_posix_socket_bridge(const ch
216216
emscripten_websocket_init_create_attributes(&attr);
217217
attr.url = bridgeUrl;
218218
bridgeSocket = emscripten_websocket_new(&attr);
219-
emscripten_websocket_set_onmessage_callback_on_thread(bridgeSocket, 0, bridge_socket_on_message, EM_CALLBACK_THREAD_CONTEXT_MAIN_BROWSER_THREAD);
219+
emscripten_websocket_set_onmessage_callback_on_thread(bridgeSocket, 0, bridge_socket_on_message, EM_CALLBACK_THREAD_CONTEXT_MAIN_RUNTIME_THREAD);
220220

221221
pthread_mutex_unlock(&bridgeLock);
222222
return bridgeSocket;

test/html5_callbacks_on_calling_thread.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,13 @@ EM_BOOL mouse_callback(int eventType, const EmscriptenMouseEvent *e, void *userD
1717
static int once;
1818

1919
void *threadId = pthread_self();
20-
if (!once) printf("pthread_self()=%p, registeringThreadId=%p, mainRuntimeThreadId=%p, emscripten_main_browser_thread_id()=%p\n", threadId, registeringThreadId, mainRuntimeThreadId, emscripten_main_browser_thread_id());
20+
if (!once)
21+
printf("pthread_self()=%p, registeringThreadId=%p, mainRuntimeThreadId=%p, "
22+
"emscripten_main_runtime_thread_id()=%p\n",
23+
threadId,
24+
registeringThreadId,
25+
mainRuntimeThreadId,
26+
emscripten_main_runtime_thread_id());
2127
printf("eventType: %d, mouseEvent: %p, userData: %p, screen: (%ld,%ld), client: (%ld,%ld),%s%s%s%s button: %hu, buttons: %hu, movement: (%ld,%ld), canvas: (%ld,%ld)\n",
2228
eventType, e, userData,
2329
e->screenX, e->screenY, e->clientX, e->clientY,

test/html5_event_callback_in_two_threads.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -99,14 +99,14 @@ EM_BOOL keyup_callback_on_application_main_thread(int eventType, const Emscripte
9999

100100
int main()
101101
{
102-
main_browser_thread_id = emscripten_main_browser_thread_id();
103-
assert(main_browser_thread_id);
102+
main_runtime_thread_id = emscripten_main_runtime_thread_id();
103+
assert(main_runtime_thread_id);
104104
application_main_thread_id = (void*)pthread_self();
105105
assert(application_main_thread_id);
106106

107-
printf("Main browser thread ID: %p, application main thread ID: %p\n", main_browser_thread_id, application_main_thread_id);
107+
printf("Main runtime thread ID: %p, application main thread ID: %p\n", main_runtime_thread_id, application_main_thread_id);
108108

109-
emscripten_set_keydown_callback_on_thread(0, 0, 1, keydown_callback_on_main_browser_thread, EM_CALLBACK_THREAD_CONTEXT_MAIN_BROWSER_THREAD);
109+
emscripten_set_keydown_callback_on_thread(0, 0, 1, keydown_callback_on_main_browser_thread, EM_CALLBACK_THREAD_CONTEXT_MAIN_RUNTIME_THREAD);
110110
emscripten_set_keypress_callback_on_thread(0, 0, 1, keypress_callback_on_application_main_thread, EM_CALLBACK_THREAD_CONTEXT_CALLING_THREAD);
111111
emscripten_set_keyup_callback_on_thread(0, 0, 1, keyup_callback_on_application_main_thread, EM_CALLBACK_THREAD_CONTEXT_CALLING_THREAD);
112112

test/pthread/call_async.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,13 @@ int main() {
2525
assert(state == 0);
2626
// This dispatch_to_thread call will be synchronous since we are on the right
2727
// thread already.
28-
int called_now = emscripten_dispatch_to_thread(emscripten_main_browser_thread_id(), EM_FUNC_SIG_V, &increment, 0);
28+
int called_now = emscripten_dispatch_to_thread(emscripten_main_runtime_thread_id(), EM_FUNC_SIG_V, &increment, 0);
2929
assert(called_now);
3030
assert(state == 1);
3131
// This async call will actually be async.
32-
emscripten_dispatch_to_thread_async(emscripten_main_browser_thread_id(), EM_FUNC_SIG_V, &increment, 0);
32+
emscripten_dispatch_to_thread_async(emscripten_main_runtime_thread_id(), EM_FUNC_SIG_V, &increment, 0);
3333
assert(state == 1);
34-
emscripten_dispatch_to_thread_async(emscripten_main_browser_thread_id(), EM_FUNC_SIG_V, &finish, 0);
34+
emscripten_dispatch_to_thread_async(emscripten_main_runtime_thread_id(), EM_FUNC_SIG_V, &finish, 0);
3535
emscripten_exit_with_live_runtime();
3636
__builtin_trap();
3737
}

test/reference_struct_info.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@
186186
"EMSGSIZE": 35,
187187
"EMULTIHOP": 36,
188188
"EM_CALLBACK_THREAD_CONTEXT_CALLING_THREAD": 2,
189-
"EM_CALLBACK_THREAD_CONTEXT_MAIN_BROWSER_THREAD": 1,
189+
"EM_CALLBACK_THREAD_CONTEXT_MAIN_RUNTIME_THREAD": 1,
190190
"EM_FUNC_SIG_I": 536870912,
191191
"EM_FUNC_SIG_II": 570425344,
192192
"EM_FUNC_SIG_III": 603979776,

test/test_other.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13059,3 +13059,19 @@ def test_min_node_version(self):
1305913059
self.set_setting('MIN_NODE_VERSION', 210000)
1306013060
expected = 'This emscripten-generated code requires node v21.0.0 (detected v%s)' % node_version
1306113061
self.do_runf(test_file('hello_world.c'), expected, assert_returncode=NON_ZERO)
13062+
13063+
def test_deprecated_macros(self):
13064+
create_file('main.c', '''
13065+
#include <assert.h>
13066+
#include <stdio.h>
13067+
#include <emscripten/threading.h>
13068+
13069+
int main() {
13070+
printf("%d\\n", emscripten_main_browser_thread_id());
13071+
assert(emscripten_main_browser_thread_id());
13072+
return 0;
13073+
}
13074+
''')
13075+
err = self.run_process([EMCC, 'main.c'], stderr=PIPE).stderr
13076+
expected = "warning: macro 'emscripten_main_browser_thread_id' has been marked as deprecated: use emscripten_main_runtime_thread_id instead [-Wdeprecated-pragma]"
13077+
self.assertContained(expected, err)

0 commit comments

Comments
 (0)