Skip to content

Commit a26bc33

Browse files
authored
Remove PThread.mainThreadFutex from JavaScript. NFC (#12887)
This variable is initialized from `_main_thread_futex` which is a data address exported from native code. Rather than copy it we can simply use that variable directly. I think it made sense to have a separate JS variable in the past when this address was allocated at runtime, but since #12318 landed it is no longer needed. Also rename the exported variable to avoid conflict with user symbols.
1 parent 201e048 commit a26bc33

File tree

2 files changed

+19
-23
lines changed

2 files changed

+19
-23
lines changed

src/library_pthread.js

+18-22
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,6 @@ var LibraryPThread = {
2525
// Points to a pthread_t structure in the Emscripten main heap, allocated on
2626
// demand if/when first needed.
2727
// mainThreadBlock: undefined,
28-
// Stores the memory address that the main thread is waiting on, if any. If
29-
// the main thread is waiting, we wake it up before waking up any workers.
30-
// mainThreadFutex: undefined,
3128
initMainThreadBlock: function() {
3229
#if ASSERTIONS
3330
assert(!ENVIRONMENT_IS_PTHREAD);
@@ -107,9 +104,8 @@ var LibraryPThread = {
107104
#endif
108105
},
109106
initShared: function() {
110-
PThread.mainThreadFutex = _main_thread_futex;
111107
#if ASSERTIONS
112-
assert(PThread.mainThreadFutex > 0);
108+
assert(__emscripten_main_thread_futex > 0);
113109
#endif
114110
},
115111
// Maps pthread_t to pthread info objects
@@ -1145,20 +1141,20 @@ var LibraryPThread = {
11451141
#endif
11461142
// Register globally which address the main thread is simulating to be
11471143
// waiting on. When zero, the main thread is not waiting on anything, and on
1148-
// nonzero, the contents of the address pointed by PThread.mainThreadFutex
1144+
// nonzero, the contents of the address pointed by __emscripten_main_thread_futex
11491145
// tell which address the main thread is simulating its wait on.
11501146
// We need to be careful of recursion here: If we wait on a futex, and
11511147
// then call _emscripten_main_thread_process_queued_calls() below, that
11521148
// will call code that takes the proxying mutex - which can once more
11531149
// reach this code in a nested call. To avoid interference between the
1154-
// two (there is just a single mainThreadFutex at a time), unmark
1150+
// two (there is just a single __emscripten_main_thread_futex at a time), unmark
11551151
// ourselves before calling the potentially-recursive call. See below for
11561152
// how we handle the case of our futex being notified during the time in
1157-
// between when we are not set as the value of mainThreadFutex.
1153+
// between when we are not set as the value of __emscripten_main_thread_futex.
11581154
#if ASSERTIONS
1159-
assert(PThread.mainThreadFutex > 0);
1155+
assert(__emscripten_main_thread_futex > 0);
11601156
#endif
1161-
var lastAddr = Atomics.exchange(HEAP32, PThread.mainThreadFutex >> 2, addr);
1157+
var lastAddr = Atomics.exchange(HEAP32, __emscripten_main_thread_futex >> 2, addr);
11621158
#if ASSERTIONS
11631159
// We must not have already been waiting.
11641160
assert(lastAddr == 0);
@@ -1172,7 +1168,7 @@ var LibraryPThread = {
11721168
PThread.setThreadStatusConditional(_pthread_self(), {{{ cDefine('EM_THREAD_STATUS_RUNNING') }}}, {{{ cDefine('EM_THREAD_STATUS_WAITFUTEX') }}});
11731169
#endif
11741170
// We timed out, so stop marking ourselves as waiting.
1175-
lastAddr = Atomics.exchange(HEAP32, PThread.mainThreadFutex >> 2, 0);
1171+
lastAddr = Atomics.exchange(HEAP32, __emscripten_main_thread_futex >> 2, 0);
11761172
#if ASSERTIONS
11771173
// The current value must have been our address which we set, or
11781174
// in a race it was set to 0 which means another thread just allowed
@@ -1186,7 +1182,7 @@ var LibraryPThread = {
11861182
// Note that we have to do so carefully, as we may take a lock while
11871183
// doing so, which can recurse into this function; stop marking
11881184
// ourselves as waiting while we do so.
1189-
lastAddr = Atomics.exchange(HEAP32, PThread.mainThreadFutex >> 2, 0);
1185+
lastAddr = Atomics.exchange(HEAP32, __emscripten_main_thread_futex >> 2, 0);
11901186
#if ASSERTIONS
11911187
assert(lastAddr == addr || lastAddr == 0);
11921188
#endif
@@ -1201,20 +1197,20 @@ var LibraryPThread = {
12011197
//
12021198
// * wait on futex A
12031199
// * recurse into emscripten_main_thread_process_queued_calls(),
1204-
// which waits on futex B. that sets the mainThreadFutex address to
1200+
// which waits on futex B. that sets the __emscripten_main_thread_futex address to
12051201
// futex B, and there is no longer any mention of futex A.
1206-
// * a worker is done with futex A. it checks mainThreadFutex but does
1202+
// * a worker is done with futex A. it checks __emscripten_main_thread_futex but does
12071203
// not see A, so it does nothing special for the main thread.
12081204
// * a worker is done with futex B. it flips mainThreadMutex from B
12091205
// to 0, ending the wait on futex B.
1210-
// * we return to the wait on futex A. mainThreadFutex is 0, but that
1206+
// * we return to the wait on futex A. __emscripten_main_thread_futex is 0, but that
12111207
// is because of futex B being done - we can't tell from
1212-
// mainThreadFutex whether A is done or not. therefore, check the
1208+
// __emscripten_main_thread_futex whether A is done or not. therefore, check the
12131209
// memory value of the futex.
12141210
//
12151211
// That case motivates the design here. Given that, checking the memory
12161212
// address is also necessary for other reasons: we unset and re-set our
1217-
// address in mainThreadFutex around calls to
1213+
// address in __emscripten_main_thread_futex around calls to
12181214
// emscripten_main_thread_process_queued_calls(), and a worker could
12191215
// attempt to wake us up right before/after such times.
12201216
//
@@ -1235,7 +1231,7 @@ var LibraryPThread = {
12351231
}
12361232

12371233
// Mark us as waiting once more, and continue the loop.
1238-
lastAddr = Atomics.exchange(HEAP32, PThread.mainThreadFutex >> 2, addr);
1234+
lastAddr = Atomics.exchange(HEAP32, __emscripten_main_thread_futex >> 2, addr);
12391235
#if ASSERTIONS
12401236
assert(lastAddr == 0);
12411237
#endif
@@ -1260,19 +1256,19 @@ var LibraryPThread = {
12601256
// Note that this is not a fair procedure, since we always wake main thread first before any workers, so
12611257
// this scheme does not adhere to real queue-based waiting.
12621258
#if ASSERTIONS
1263-
assert(PThread.mainThreadFutex > 0);
1259+
assert(__emscripten_main_thread_futex > 0);
12641260
#endif
1265-
var mainThreadWaitAddress = Atomics.load(HEAP32, PThread.mainThreadFutex >> 2);
1261+
var mainThreadWaitAddress = Atomics.load(HEAP32, __emscripten_main_thread_futex >> 2);
12661262
var mainThreadWoken = 0;
12671263
if (mainThreadWaitAddress == addr) {
12681264
#if ASSERTIONS
1269-
// We only use mainThreadFutex on the main browser thread, where we
1265+
// We only use __emscripten_main_thread_futex on the main browser thread, where we
12701266
// cannot block while we wait. Therefore we should only see it set from
12711267
// other threads, and not on the main thread itself. In other words, the
12721268
// main thread must never try to wake itself up!
12731269
assert(!ENVIRONMENT_IS_WEB);
12741270
#endif
1275-
var loadedAddr = Atomics.compareExchange(HEAP32, PThread.mainThreadFutex >> 2, mainThreadWaitAddress, 0);
1271+
var loadedAddr = Atomics.compareExchange(HEAP32, __emscripten_main_thread_futex >> 2, mainThreadWaitAddress, 0);
12761272
if (loadedAddr == mainThreadWaitAddress) {
12771273
--count;
12781274
mainThreadWoken = 1;

system/lib/pthread/library_pthread.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -904,7 +904,7 @@ int llvm_atomic_load_add_i32_p0i32(int* ptr, int delta) {
904904
// Stores the memory address that the main thread is waiting on, if any. If
905905
// the main thread is waiting, we wake it up before waking up any workers.
906906
EMSCRIPTEN_KEEPALIVE
907-
void* main_thread_futex;
907+
void* _emscripten_main_thread_futex;
908908

909909
typedef struct main_args {
910910
int argc;

0 commit comments

Comments
 (0)