Skip to content

Move parts of pthread_create to native code. NFC - #14858

Merged
sbc100 merged 1 commit into
mainfrom
move_thread_init_to_native
Aug 13, 2021
Merged

sbc100 merged 1 commit into
mainfrom
move_thread_init_to_native

Conversation

@sbc100

@sbc100 sbc100 commented Aug 13, 2021 •

Copy link
Copy Markdown
Collaborator

Also update the proxying of pthread_create so it only proxies the native part.

@sbc100 sbc100 changed the title Move a part of pthread_create to native code. NFC Move parts of pthread_create to native code. NFC Aug 13, 2021
@sbc100
sbc100 requested a review from kleisauke August 13, 2021 04:29
@sbc100
sbc100 force-pushed the move_thread_init_to_native branch from 35ccda0 to b44d69e Compare August 13, 2021 05:31
q->returnValue.i =
emscripten_builtin_pthread_create(q->args[0].vp, q->args[1].vp, q->args[2].vp, q->args[3].vp);
__pthread_create_js(q->args[0].vp, q->args[1].vp, q->args[2].vp, q->args[3].vp);
#ifdef HAS_SANITIZER

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Note to myself: these __lsan_{disable,enable}(); calls can probably be removed if we also move the pthread stack allocation to native code.

// Allocate a stack if the user doesn't want to place the stack in a
// custom memory area.
stackBase = _memalign({{{ STACK_ALIGN }}}, stackSize);

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.

Interesting.. I didn't realize that is what they were for. I'll add a comment, thanks!

Comment thread system/lib/pthread/pthread_create.c Resolved
Comment thread system/lib/pthread/pthread_create.c Resolved
@sbc100
sbc100 force-pushed the move_thread_init_to_native branch from b44d69e to e082827 Compare August 13, 2021 14:53
@sbc100
sbc100 requested review from kleisauke and kripken August 13, 2021 15:01
Comment thread system/lib/pthread/pthread_create.c Resolved
Comment thread src/library_pthread.js
Comment on lines -167 to -169
var tlsMemory = {{{ makeGetValue('pthread.threadInfoStruct', C_STRUCTS.pthread.tsd, 'i32') }}};
{{{ makeSetValue('pthread.threadInfoStruct', C_STRUCTS.pthread.tsd, 0, 'i32') }}};
_free(tlsMemory);

@kleisauke kleisauke Aug 13, 2021 •

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I just realized that this move to _emscripten_thread_exit could potentially cause issues with pthread_cancel, pthread_kill or detached threads, because they will not end up calling pthread_exit (causing the thread-specific data associated with a thread not being free()'d).

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.

Yes I noticed that too.

I think for pthread_cancel we shouldn't have a problem because cancel now calls _emscripten_thread_exit:

Module['__emscripten_thread_exit'](-1/*PTHREAD_CANCELED*/);

However, for pthread_kill I don't think there is much we can do. I guess its reasonable that you can end up with leaks in that case. In fact I think we already have leaks because we don't call __pthread_tsd_run_dtors or anything that is registered via __cxa_thread_atexit (which means don't free the static TLS block:

__cxa_thread_atexit(free_tls, tls_block, &__dso_handle);
).
Perhaps we can address this one case, but in general I think pthread_kill can always leak because the thread doesn't get any chance to shut itself down. WDYT?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thanks for the clarification! I think we can't do much about pthread_kill then (since it doesn't run the thread-specific data dtors either, as you noticed). pthread_cancel does indeed no longer have this issue.

How about detached threads that are never joined and are shutdown during PThread.terminateAllThreads()? I think the proxied main thread (when linking with -sPROXY_TO_PTHREAD) is such a case, but perhaps that is also negligible.

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.

I think Thread.terminateAllThreads is kind of the same as the pthread_kill case.. but we care even less about leaks in that case because the program is basically over.

Its kind of shame we don't have a better way to force termination in the test itself. In them musl code they use signals to bring down a thread which means it gets some time to tidy itself up a little, but we don't any way to interrupt threads on the web or in node.

Perhaps we should just implement pthread_kill in terms of pthread_cancel and note that it won't work for threads that are busy-looping. Either that or document that it leads to memory-leaks.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I'm not sure about making pthread_kill non-compliant, I think documenting that it may leak memory is a better approach (or perhaps document that pthread_cancel is the preferred/safest way to terminate a thread).

@sbc100
sbc100 force-pushed the move_thread_init_to_native branch from e082827 to 7268008 Compare August 13, 2021 17:22
@sbc100
sbc100 enabled auto-merge (squash) August 13, 2021 18:08
@sbc100
sbc100 disabled auto-merge August 13, 2021 18:08
@sbc100
sbc100 enabled auto-merge (squash) August 13, 2021 18:11
@sbc100
sbc100 disabled auto-merge August 13, 2021 18:41
@sbc100
sbc100 force-pushed the move_thread_init_to_native branch from 7268008 to 9b72875 Compare August 13, 2021 19:20
@sbc100
sbc100 enabled auto-merge (squash) August 13, 2021 19:34
@sbc100
sbc100 merged commit 7c8be3d into main Aug 13, 2021
@sbc100
sbc100 deleted the move_thread_init_to_native branch August 13, 2021 20:12
thyrrestrup pushed a commit to thyrrestrup/emscripten that referenced this pull request Aug 18, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants