From 5d869ce985564ec6a467c0014cb82894325eb868 Mon Sep 17 00:00:00 2001 From: Sam Clegg Date: Mon, 15 Nov 2021 17:06:11 -0800 Subject: [PATCH] Move setting of initial thread state to native code. NFC --- src/library_pthread.js | 10 ---------- src/struct_info_internal.json | 7 +------ system/lib/pthread/pthread_create.c | 6 ++++++ tests/reference_struct_info.json | 5 ----- 4 files changed, 7 insertions(+), 21 deletions(-) diff --git a/src/library_pthread.js b/src/library_pthread.js index 54a46be18ee28..f23baf673c3e2 100644 --- a/src/library_pthread.js +++ b/src/library_pthread.js @@ -278,8 +278,6 @@ var LibraryPThread = { } else if (cmd === 'detachedExit') { #if ASSERTIONS assert(worker.pthread); - var detach_state = Atomics.load(HEAPU32, (worker.pthread.threadInfoStruct + {{{ C_STRUCTS.pthread.detach_state }}}) >> 2); - assert(detach_state == {{{ cDefine('DT_EXITED') }}}); #endif PThread.returnWorkerToPool(worker); } else if (cmd === 'cancelDone') { @@ -575,7 +573,6 @@ var LibraryPThread = { worker: worker, stackBase: threadParams.stackBase, stackSize: threadParams.stackSize, - initialState: {{{ cDefine('DT_JOINABLE') }}}, allocatedOwnStack: threadParams.allocatedOwnStack, // Info area for this thread in Emscripten HEAP (shared) threadInfoStruct: threadParams.pthread_ptr @@ -583,7 +580,6 @@ var LibraryPThread = { var tis = pthread.threadInfoStruct >> 2; // spawnThread is always called with a zero-initialized thread struct so // no need to set any valudes to zero here. - Atomics.store(HEAPU32, tis + ({{{ C_STRUCTS.pthread.detach_state }}} >> 2), threadParams.initialState); Atomics.store(HEAPU32, tis + ({{{ C_STRUCTS.pthread.stack_size }}} >> 2), threadParams.stackSize); Atomics.store(HEAPU32, tis + ({{{ C_STRUCTS.pthread.stack }}} >> 2), stackHigh); @@ -775,16 +771,11 @@ var LibraryPThread = { var stackSize = 0; var stackBase = 0; - // Default thread state is DT_JOINABLE, i.e. start as not detached. - var initialState = {{{ cDefine('DT_JOINABLE') }}}; // When musl creates C11 threads it passes __ATTRP_C11_THREAD (-1) which // treat as if it was NULL. if (attr && attr != {{{ cDefine('__ATTRP_C11_THREAD') }}}) { stackSize = {{{ makeGetValue('attr', 0/*_a_stacksize*/, 'i32') }}}; stackBase = {{{ makeGetValue('attr', 8/*_a_stackaddr*/, 'i32') }}}; - if ({{{ makeGetValue('attr', 12/*_a_detach*/, 'i32') }}}) { - initialState = {{{ cDefine('DT_DETACHED') }}}; - } } else { // According to // http://man7.org/linux/man-pages/man3/pthread_create.3.html, default @@ -818,7 +809,6 @@ var LibraryPThread = { stackBase: stackBase, stackSize: stackSize, allocatedOwnStack: allocatedOwnStack, - initialState: initialState, startRoutine: start_routine, pthread_ptr: pthread_ptr, arg: arg, diff --git a/src/struct_info_internal.json b/src/struct_info_internal.json index 939d8f70d2379..93a971d5fcb70 100644 --- a/src/struct_info_internal.json +++ b/src/struct_info_internal.json @@ -9,7 +9,6 @@ "profilerBlock", "self", "tsd", - "detach_state", "stack", "stack_size", "result", @@ -32,11 +31,7 @@ }, "defines": [ "__ATTRP_C11_THREAD", - "EM_THREAD_NAME_MAX", - "DT_EXITED", - "DT_EXITING", - "DT_JOINABLE", - "DT_DETACHED" + "EM_THREAD_NAME_MAX" ] }, { diff --git a/system/lib/pthread/pthread_create.c b/system/lib/pthread/pthread_create.c index 0038eeae9e34f..5cd0fe1cf7160 100644 --- a/system/lib/pthread/pthread_create.c +++ b/system/lib/pthread/pthread_create.c @@ -116,6 +116,12 @@ int __pthread_create(pthread_t* restrict res, new->tsd = malloc(PTHREAD_KEYS_MAX * sizeof(void*)); memset(new->tsd, 0, PTHREAD_KEYS_MAX * sizeof(void*)); + if (attrp && attrp != __ATTRP_C11_THREAD && attrp->_a_detach) { + new->detach_state = DT_DETACHED; + } else { + new->detach_state = DT_JOINABLE; + } + //printf("start __pthread_create: %p\n", self); int rtn = __pthread_create_js(new, attrp, entry, arg); if (rtn != 0) diff --git a/tests/reference_struct_info.json b/tests/reference_struct_info.json index 0786f571909fb..7b82033bc76a6 100644 --- a/tests/reference_struct_info.json +++ b/tests/reference_struct_info.json @@ -19,10 +19,6 @@ "CLOCK_MONOTONIC": 1, "CLOCK_MONOTONIC_RAW": 4, "CLOCK_REALTIME": 0, - "DT_DETACHED": 3, - "DT_EXITED": 0, - "DT_EXITING": 1, - "DT_JOINABLE": 2, "E2BIG": 1, "EACCES": 2, "EADDRINUSE": 3, @@ -1331,7 +1327,6 @@ "cancel": 28, "cancelasync": 33, "canceldisable": 32, - "detach_state": 24, "locale": 88, "profilerBlock": 104, "result": 56,