Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 0 additions & 10 deletions src/library_pthread.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not leave this assertion?

Copy link
Copy Markdown
Collaborator Author

@sbc100 sbc100 Nov 17, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removing this means the JS has no more need to know about detach_state (keeping the logic all in native code simplifies the model IMHO). Also, detachedExit has exactly one callsite which is in emscripten_thread_exit so it seems little unnecessary. I think it will be good if in the long run we can move all the threadInfoStruct handling to native code and reduce the JS code to just worker handling.

PThread.returnWorkerToPool(worker);
} else if (cmd === 'cancelDone') {
Expand Down Expand Up @@ -575,15 +573,13 @@ 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
};
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);

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -818,7 +809,6 @@ var LibraryPThread = {
stackBase: stackBase,
stackSize: stackSize,
allocatedOwnStack: allocatedOwnStack,
initialState: initialState,
startRoutine: start_routine,
pthread_ptr: pthread_ptr,
arg: arg,
Expand Down
7 changes: 1 addition & 6 deletions src/struct_info_internal.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
"profilerBlock",
"self",
"tsd",
"detach_state",
"stack",
"stack_size",
"result",
Expand All @@ -32,11 +31,7 @@
},
"defines": [
"__ATTRP_C11_THREAD",
"EM_THREAD_NAME_MAX",
"DT_EXITED",
"DT_EXITING",
"DT_JOINABLE",
"DT_DETACHED"
"EM_THREAD_NAME_MAX"
]
},
{
Expand Down
6 changes: 6 additions & 0 deletions system/lib/pthread/pthread_create.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This logic looks different than before - it also checks attrp->_a_detach?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The old code is doing the same thing is just a little harder to read:

   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') }}}) {
      ...
      if ({{{ makeGetValue('attr', 12/*_a_detach*/, 'i32') }}}) {
        initialState = {{{ cDefine('DT_DETACHED') }}};
      }

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, right, I see it now.

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)
Expand Down
5 changes: 0 additions & 5 deletions tests/reference_struct_info.json
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -1331,7 +1327,6 @@
"cancel": 28,
"cancelasync": 33,
"canceldisable": 32,
"detach_state": 24,
"locale": 88,
"profilerBlock": 104,
"result": 56,
Expand Down