Skip to content

Commit 1cfdb43

Browse files
authored
Remove unneeded code from library_pthread.js. NFC (#17017)
The `runWithoutMainThreadQueuedCalls` helper had exactly one callsite, and a little refactoring makes this unnecessary.
1 parent 6759e95 commit 1cfdb43

File tree

6 files changed

+19
-46
lines changed

6 files changed

+19
-46
lines changed

emcc.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1492,7 +1492,6 @@ def setup_pthreads(target):
14921492
settings.REQUIRED_EXPORTS += [
14931493
'emscripten_dispatch_to_thread_',
14941494
'_emscripten_thread_free_data',
1495-
'_emscripten_allow_main_runtime_queued_calls',
14961495
'emscripten_main_browser_thread_id',
14971496
'emscripten_main_thread_process_queued_calls',
14981497
'emscripten_run_in_main_runtime_thread_js',

src/library_pthread.js

Lines changed: 16 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -183,41 +183,22 @@ var LibraryPThread = {
183183
// some operations that leave the worker queue in an invalid state until
184184
// we are completely done (it would be bad if free() ends up calling a
185185
// queued pthread_create which looks at the global data structures we are
186-
// modifying).
187-
PThread.runWithoutMainThreadQueuedCalls(function() {
188-
delete PThread.pthreads[worker.pthread.threadInfoStruct];
189-
// Note: worker is intentionally not terminated so the pool can
190-
// dynamically grow.
191-
PThread.unusedWorkers.push(worker);
192-
PThread.runningWorkers.splice(PThread.runningWorkers.indexOf(worker), 1);
193-
// Not a running Worker anymore
194-
__emscripten_thread_free_data(worker.pthread.threadInfoStruct);
195-
// Detach the worker from the pthread object, and return it to the
196-
// worker pool as an unused worker.
197-
worker.pthread = undefined;
198-
});
199-
},
200-
// Runs a function with processing of queued calls to the main thread
201-
// disabled. This is useful to avoid something like free() ending up waiting
202-
// for a lock, then running processing events, and those events can end up
203-
// doing things that interfere with what we were doing before (for example,
204-
// if we are tearing down a thread, calling free to erase its data could
205-
// end up calling a proxied pthread_create, which gets a free worker, and
206-
// can interfere).
207-
// This is only safe to call if we do not need queued calls to run. That is
208-
// the case when doing something like free(), which just needs the malloc
209-
// lock to be released.
210-
runWithoutMainThreadQueuedCalls: function(func) {
211-
#if ASSERTIONS
212-
assert(PThread.mainRuntimeThread, 'runWithoutMainThreadQueuedCalls must be done on the main runtime thread');
213-
assert(__emscripten_allow_main_runtime_queued_calls);
214-
#endif
215-
HEAP32[__emscripten_allow_main_runtime_queued_calls >> 2] = 0;
216-
try {
217-
func();
218-
} finally {
219-
HEAP32[__emscripten_allow_main_runtime_queued_calls >> 2] = 1;
220-
}
186+
// modifying). To achieve that, defer the free() til the very end, when
187+
// we are all done.
188+
var pthread_ptr = worker.pthread.threadInfoStruct;
189+
delete PThread.pthreads[pthread_ptr];
190+
// Note: worker is intentionally not terminated so the pool can
191+
// dynamically grow.
192+
PThread.unusedWorkers.push(worker);
193+
PThread.runningWorkers.splice(PThread.runningWorkers.indexOf(worker), 1);
194+
// Not a running Worker anymore
195+
// Detach the worker from the pthread object, and return it to the
196+
// worker pool as an unused worker.
197+
worker.pthread = undefined;
198+
199+
// Finally, free the underlying (and now-unused) pthread structure in
200+
// linear memory.
201+
__emscripten_thread_free_data(pthread_ptr);
221202
},
222203
receiveObjectTransfer: function(data) {
223204
#if OFFSCREENCANVAS_SUPPORT

system/lib/pthread/library_pthread.c

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -496,15 +496,8 @@ void emscripten_current_thread_process_queued_calls() {
496496
emscripten_proxy_execute_queue(emscripten_proxy_get_system_queue());
497497
}
498498

499-
// At times when we disallow the main thread to process queued calls, this will
500-
// be set to 0.
501-
int _emscripten_allow_main_runtime_queued_calls = 1;
502-
503499
void emscripten_main_thread_process_queued_calls() {
504500
assert(emscripten_is_main_runtime_thread());
505-
if (!_emscripten_allow_main_runtime_queued_calls)
506-
return;
507-
508501
emscripten_current_thread_process_queued_calls();
509502
}
510503

tests/other/metadce/test_metadce_minimal_pthreads.funcs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,9 @@ $dlmemalign
3636
$do_dispatch_to_thread
3737
$em_queued_call_malloc
3838
$emscripten_async_run_in_main_thread
39+
$emscripten_current_thread_process_queued_calls
3940
$emscripten_futex_wait
4041
$emscripten_futex_wake
41-
$emscripten_main_thread_process_queued_calls
4242
$emscripten_proxy_main
4343
$emscripten_run_in_main_runtime_thread_js
4444
$emscripten_stack_set_limits
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
13257
1+
13129
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
18717
1+
18700

0 commit comments

Comments
 (0)