Skip to content

Ensure pthread key dtors run on main thread too #14512

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 1 commit into from
Jun 23, 2021
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
5 changes: 4 additions & 1 deletion src/library_pthread.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,10 @@ var LibraryPThread = {
}

// Call into the musl function that runs destructors of all thread-specific data.
if (ENVIRONMENT_IS_PTHREAD && _pthread_self()) ___pthread_tsd_run_dtors();
#if ASSERTIONS
assert(_pthread_self())
#endif
___pthread_tsd_run_dtors();
},

runExitHandlersAndDeinitThread: function(tb, exitCode) {
Expand Down
4 changes: 2 additions & 2 deletions src/postamble_minimal.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ function run() {
var ret = _main();

#if EXIT_RUNTIME
callRuntimeCallbacks(__ATEXIT__);
<<< ATEXITS >>>
#if USE_PTHREADS
PThread.runExitHandlers();
#endif
callRuntimeCallbacks(__ATEXIT__);
<<< ATEXITS >>>
#endif

#if IN_TEST_HARNESS
Expand Down
6 changes: 3 additions & 3 deletions src/preamble.js
Original file line number Diff line number Diff line change
Expand Up @@ -424,14 +424,14 @@ function exitRuntime() {
checkStackCookie();
#endif
#if USE_PTHREADS
#if EXIT_RUNTIME
PThread.runExitHandlers();
#endif
if (ENVIRONMENT_IS_PTHREAD) return; // PThreads reuse the runtime from the main thread.
#endif
#if EXIT_RUNTIME
callRuntimeCallbacks(__ATEXIT__);
<<< ATEXITS >>>
#if USE_PTHREADS
PThread.runExitHandlers();
#endif
#endif
runtimeExited = true;
}
Expand Down
24 changes: 24 additions & 0 deletions tests/pthread/test_pthread_setspecific_mainthread.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Copyright 2015 The Emscripten Authors. All rights reserved.
// Emscripten is available under two separate licenses, the MIT license and the
// University of Illinois/NCSA Open Source License. Both these licenses can be
// found in the LICENSE file.

#include <assert.h>
#include <stdio.h>
#include <pthread.h>

void destructor(void* arg) {
printf("destructor: %d\n", (int)arg);
assert(arg == (void*)42);
}

int main() {
pthread_key_t key;
pthread_key_create(&key, destructor);
void *val = pthread_getspecific(key);
assert(val == 0);
pthread_setspecific(key, (void*)42);
val = pthread_getspecific(key);
assert(val == (void*)42);
return 0;
}
22 changes: 0 additions & 22 deletions tests/pthread/test_pthread_setspecific_mainthread.cpp

This file was deleted.

1 change: 1 addition & 0 deletions tests/pthread/test_pthread_setspecific_mainthread.out
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
destructor: 42
2 changes: 1 addition & 1 deletion tests/test_browser.py
Original file line number Diff line number Diff line change
Expand Up @@ -3947,7 +3947,7 @@ def test_pthread_unistd_io_bigint(self):
# Test that the main thread is able to use pthread_set/getspecific.
@requires_threads
def test_pthread_setspecific_mainthread(self):
self.btest(test_file('pthread/test_pthread_setspecific_mainthread.cpp'), expected='0', args=['-s', 'INITIAL_MEMORY=64MB', '-O3', '-s', 'USE_PTHREADS'], also_asmjs=True)
self.btest_exit(test_file('pthread/test_pthread_setspecific_mainthread.c'), args=['-s', 'INITIAL_MEMORY=64MB', '-O3', '-s', 'USE_PTHREADS'], also_asmjs=True)

# Test that pthreads have access to filesystem.
@requires_threads
Expand Down
5 changes: 5 additions & 0 deletions tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -2342,6 +2342,11 @@ def test_pthread_cleanup(self):
self.set_setting('PTHREAD_POOL_SIZE', 4)
self.do_run_in_out_file_test('pthread/test_pthread_cleanup.cpp')

@node_pthreads
def test_pthread_setspecific_mainthread(self):
self.set_setting('EXIT_RUNTIME')
self.do_run_in_out_file_test('pthread/test_pthread_setspecific_mainthread.c')

def test_tcgetattr(self):
self.do_runf(test_file('termios/test_tcgetattr.c'), 'success')

Expand Down