Skip to content

Remove __pthread_detached_exit in favor of __emscripten_thread_cleanup. NFC. #15605

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jan 11, 2022
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
11 changes: 0 additions & 11 deletions src/library_pthread.js
Original file line number Diff line number Diff line change
Expand Up @@ -275,11 +275,6 @@ var LibraryPThread = {
err('Thread ' + d['threadId'] + ': ' + d['text']);
} else if (cmd === 'alert') {
alert('Thread ' + d['threadId'] + ': ' + d['text']);
} else if (cmd === 'detachedExit') {
#if ASSERTIONS
assert(worker.pthread);
#endif
PThread.returnWorkerToPool(worker);
} else if (d.target === 'setimmediate') {
// Worker wants to postMessage() to itself to implement setImmediate()
// emulation.
Expand Down Expand Up @@ -834,12 +829,6 @@ var LibraryPThread = {
return 0;
},

__pthread_detached_exit: function() {
// Called at the end of pthread_exit (which occurs also when leaving the
// thread main function) if an only if the thread is in a detached state.
postMessage({ 'cmd': 'detachedExit' });
},

// Returns 0 on success, or one of the values -ETIMEDOUT, -EWOULDBLOCK or -EINVAL on error.
emscripten_futex_wait__deps: ['emscripten_main_thread_process_queued_calls'],
emscripten_futex_wait: function(addr, val, timeout) {
Expand Down
4 changes: 2 additions & 2 deletions system/lib/pthread/pthread_create.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
extern int __pthread_create_js(struct pthread *thread, const pthread_attr_t *attr, void *(*start_routine) (void *), void *arg);
extern void _emscripten_thread_init(int, int, int);
extern int _emscripten_default_pthread_stack_size();
extern void __pthread_detached_exit();
extern void __emscripten_thread_cleanup(pthread_t thread);
extern void* _emscripten_tls_base();
extern int8_t __dso_handle;

Expand Down Expand Up @@ -236,7 +236,7 @@ void _emscripten_thread_exit(void* result) {
// object and we are done.
if (state == DT_DETACHED) {
self->detach_state = DT_EXITED;
__pthread_detached_exit();
__emscripten_thread_cleanup(self);
Copy link
Collaborator

Choose a reason for hiding this comment

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

I was thinking about doing this .. but I was slightly worried about free'ing the stack being used by the current thread.

Seems a little dangerous. I don't come up with better solution yet but I was thinking maybe we could instead call this from worker.js? (i.e. once the stack is unwound all the way)?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

IIUC, __emscripten_thread_free_data was also called by the detachedExit/__pthread_detached_exit, so this PR should be completely non-functional (except that it now sends the cleanupThread command, which will also eventually call returnWorkerToPool/__emscripten_thread_free_data).

Copy link
Collaborator

@sbc100 sbc100 Nov 23, 2021

Choose a reason for hiding this comment

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

IIUC, In the current code, when __pthread_detached_exit is called the detachedExit command is sent back to the main thread which ends up freeing the data, so the data is not freed on the exit'ing thread at all.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Oh wait, I see that __emscripten_thread_cleanup also posts a message back to the main thread.. so I agree this should be and NFC. Thanks for clarifying!

} else {
self->detach_state = DT_EXITING;
// wake any threads that might be waiting for us to exit
Expand Down
2 changes: 1 addition & 1 deletion system/lib/pthread/pthread_join.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#include <pthread.h>

extern int __pthread_join_js(pthread_t t, void **res, int tryjoin);
extern int __emscripten_thread_cleanup(pthread_t t);
extern void __emscripten_thread_cleanup(pthread_t t);

static int __pthread_join_internal(pthread_t t, void **res) {
if (t->self != t) {
Expand Down