Skip to content

Commit 24fdc1a

Browse files
authored
Explicitly create the proxied main thread as detached (#14880)
Allowing musl to know whether it can immediately dispose of the thread resources once the thread is exited, instead of keeping it around (for a possible `pthread_join` call, which will never occur for this thread). This change can probably be considered non-functional, because the proxied main thread was previously disposed anyhow during `PThread.terminateAllThreads`. Resolves: #9467.
1 parent 5c6f08f commit 24fdc1a

File tree

4 files changed

+39
-0
lines changed

4 files changed

+39
-0
lines changed

system/lib/pthread/emscripten_proxy_main.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ static void* _main_thread(void* param) {
2525
int emscripten_proxy_main(int argc, char** argv) {
2626
pthread_attr_t attr;
2727
pthread_attr_init(&attr);
28+
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
2829
// Use the size of the current stack, which is the normal size of the stack
2930
// that main() would have without PROXY_TO_PTHREAD.
3031
pthread_attr_setstacksize(&attr, emscripten_stack_get_base() - emscripten_stack_get_end());
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#include <assert.h>
2+
#include <errno.h>
3+
#include <pthread.h>
4+
#include <stdio.h>
5+
6+
#include <emscripten/threading.h>
7+
8+
int main() {
9+
pthread_t self = pthread_self();
10+
11+
/* Join the calling thread. This should fail with EDEADLK. */
12+
assert(pthread_join(self, NULL) == EDEADLK);
13+
14+
/* We should be able to detach the main thread,
15+
* but not the proxied main thread (as that one
16+
* is already detached).
17+
*/
18+
int ret = emscripten_is_main_browser_thread() ? 0 : EINVAL;
19+
assert(pthread_detach(self) == ret);
20+
21+
puts("passed");
22+
23+
return 0;
24+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
passed

tests/test_other.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9759,6 +9759,19 @@ def test_pthread_stub(self):
97599759
# libpthread_stub.a
97609760
self.do_other_test('test_pthread_stub.c')
97619761

9762+
@node_pthreads
9763+
def test_main_pthread_join_detach(self):
9764+
# Verify that we're unable to join the main thread
9765+
self.set_setting('EXIT_RUNTIME')
9766+
self.do_run_in_out_file_test('other/test_pthread_self_join_detach.c')
9767+
9768+
@node_pthreads
9769+
def test_proxy_pthread_join_detach(self):
9770+
# Verify that we're unable to detach or join the proxied main thread
9771+
self.set_setting('PROXY_TO_PTHREAD')
9772+
self.set_setting('EXIT_RUNTIME')
9773+
self.do_run_in_out_file_test('other/test_pthread_self_join_detach.c')
9774+
97629775
def test_stdin_preprocess(self):
97639776
create_file('temp.h', '#include <string>')
97649777
outputStdin = self.run_process([EMCC, '-x', 'c++', '-dM', '-E', '-'], input="#include <string>", stdout=PIPE).stdout

0 commit comments

Comments
 (0)