Skip to content

Commit 724b8be

Browse files
committed
Simplify
1 parent 3008280 commit 724b8be

3 files changed

Lines changed: 19 additions & 21 deletions

File tree

system/lib/pthread/pthread_create.c

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -315,24 +315,18 @@ void _emscripten_thread_exit(void* result) {
315315
// Call into the musl function that runs destructors of all thread-specific data.
316316
__pthread_tsd_run_dtors();
317317

318-
// This atomic potentially competes with a concurrent pthread_detach
319-
// call; the loser is responsible for freeing thread resources.
320-
int state = a_cas(&self->detach_state, DT_JOINABLE, DT_EXITING);
321-
322-
// The thread list lock must be AS-safe.
323-
__tl_lock();
324-
325-
// If this is the only thread in the list, don't proceed with
326-
// termination of the thread, but restore the previous lock to
327-
// prepare for exit to call atexit handlers.
328-
if (self->next == self) {
329-
__tl_unlock();
330-
self->detach_state = state;
318+
// If this is the main runtime thread, don't proceed with
319+
// termination of the thread, but prepare for exit to call
320+
// atexit handlers.
321+
if (emscripten_is_main_runtime_thread()) {
331322
exit(0);
332323
}
333324

334325
// At this point we are committed to thread termination.
335326

327+
// The thread list lock must be AS-safe.
328+
__tl_lock();
329+
336330
// Process robust list in userspace to handle non-pshared mutexes
337331
// and the detached thread case where the robust list head will
338332
// be invalid when the kernel would process it.
@@ -367,6 +361,10 @@ void _emscripten_thread_exit(void* result) {
367361
// Not hosting a pthread anymore in this worker set __pthread_self to NULL
368362
__set_thread_state(NULL, 0, 0, 1);
369363

364+
// This atomic potentially competes with a concurrent pthread_detach
365+
// call; the loser is responsible for freeing thread resources.
366+
int state = a_cas(&self->detach_state, DT_JOINABLE, DT_EXITING);
367+
370368
if (state == DT_DETACHED) {
371369
_emscripten_thread_cleanup(self);
372370
} else {

test/codesize/test_codesize_minimal_pthreads.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
{
22
"a.out.js": 7372,
33
"a.out.js.gz": 3594,
4-
"a.out.nodebug.wasm": 19055,
5-
"a.out.nodebug.wasm.gz": 8798,
6-
"total": 26427,
7-
"total_gz": 12392,
4+
"a.out.nodebug.wasm": 19037,
5+
"a.out.nodebug.wasm.gz": 8787,
6+
"total": 26409,
7+
"total_gz": 12381,
88
"sent": [
99
"a (memory)",
1010
"b (exit)",

test/codesize/test_codesize_minimal_pthreads_memgrowth.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
{
22
"a.out.js": 7781,
33
"a.out.js.gz": 3797,
4-
"a.out.nodebug.wasm": 19056,
5-
"a.out.nodebug.wasm.gz": 8799,
6-
"total": 26837,
7-
"total_gz": 12596,
4+
"a.out.nodebug.wasm": 19038,
5+
"a.out.nodebug.wasm.gz": 8789,
6+
"total": 26819,
7+
"total_gz": 12586,
88
"sent": [
99
"a (memory)",
1010
"b (exit)",

0 commit comments

Comments
 (0)