Repository navigation
Move parts of pthread_create to native code. NFC - #14858
Conversation
35ccda0 to
b44d69e
Compare
| 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 |
There was a problem hiding this comment.
Note to myself: these __lsan_{disable,enable}(); calls can probably be removed if we also move the pthread stack allocation to native code.
emscripten/src/library_pthread.js
Lines 703 to 705 in b44d69e
There was a problem hiding this comment.
Interesting.. I didn't realize that is what they were for. I'll add a comment, thanks!
b44d69e to
e082827
Compare
| var tlsMemory = {{{ makeGetValue('pthread.threadInfoStruct', C_STRUCTS.pthread.tsd, 'i32') }}}; | ||
| {{{ makeSetValue('pthread.threadInfoStruct', C_STRUCTS.pthread.tsd, 0, 'i32') }}}; | ||
| _free(tlsMemory); |
There was a problem hiding this comment.
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).
There was a problem hiding this comment.
Yes I noticed that too.
I think for pthread_cancel we shouldn't have a problem because cancel now calls _emscripten_thread_exit:
Line 276 in 58647ae
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:
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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).
e082827 to
7268008
Compare
7268008 to
9b72875
Compare
Also update the proxying of pthread_create so it only proxies the native part.