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
}

0 commit comments

Comments
 (0)