Skip to content

Commit 4ba2f31

Browse files
committed
Ensure pthread key dtors run on main thread too
1 parent 764d67c commit 4ba2f31

8 files changed

+39
-29
lines changed

src/library_pthread.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,10 @@ var LibraryPThread = {
170170
}
171171

172172
// Call into the musl function that runs destructors of all thread-specific data.
173-
if (ENVIRONMENT_IS_PTHREAD && _pthread_self()) ___pthread_tsd_run_dtors();
173+
#if ASSERTIONS
174+
assert(_pthread_self())
175+
#endif
176+
___pthread_tsd_run_dtors();
174177
},
175178

176179
runExitHandlersAndDeinitThread: function(tb, exitCode) {

src/postamble_minimal.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,11 @@ function run() {
2424
var ret = _main();
2525

2626
#if EXIT_RUNTIME
27-
callRuntimeCallbacks(__ATEXIT__);
28-
<<< ATEXITS >>>
2927
#if USE_PTHREADS
3028
PThread.runExitHandlers();
3129
#endif
30+
callRuntimeCallbacks(__ATEXIT__);
31+
<<< ATEXITS >>>
3232
#endif
3333

3434
#if IN_TEST_HARNESS

src/preamble.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -424,14 +424,12 @@ function exitRuntime() {
424424
checkStackCookie();
425425
#endif
426426
#if USE_PTHREADS
427+
PThread.runExitHandlers();
427428
if (ENVIRONMENT_IS_PTHREAD) return; // PThreads reuse the runtime from the main thread.
428429
#endif
429430
#if EXIT_RUNTIME
430431
callRuntimeCallbacks(__ATEXIT__);
431432
<<< ATEXITS >>>
432-
#if USE_PTHREADS
433-
PThread.runExitHandlers();
434-
#endif
435433
#endif
436434
runtimeExited = true;
437435
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Copyright 2015 The Emscripten Authors. All rights reserved.
2+
// Emscripten is available under two separate licenses, the MIT license and the
3+
// University of Illinois/NCSA Open Source License. Both these licenses can be
4+
// found in the LICENSE file.
5+
6+
#include <assert.h>
7+
#include <stdio.h>
8+
#include <pthread.h>
9+
10+
11+
void destructor(void* arg) {
12+
printf("destructor: %d\n", (int)arg);
13+
assert(arg == (void*)42);
14+
}
15+
16+
int main() {
17+
pthread_key_t key;
18+
pthread_key_create(&key, destructor);
19+
void *val = pthread_getspecific(key);
20+
assert(val == 0);
21+
pthread_setspecific(key, (void*)42);
22+
val = pthread_getspecific(key);
23+
assert(val == (void*)42);
24+
return 0;
25+
}

tests/pthread/test_pthread_setspecific_mainthread.cpp

Lines changed: 0 additions & 22 deletions
This file was deleted.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
destructor: 42

tests/test_browser.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3947,7 +3947,7 @@ def test_pthread_unistd_io_bigint(self):
39473947
# Test that the main thread is able to use pthread_set/getspecific.
39483948
@requires_threads
39493949
def test_pthread_setspecific_mainthread(self):
3950-
self.btest(test_file('pthread/test_pthread_setspecific_mainthread.cpp'), expected='0', args=['-s', 'INITIAL_MEMORY=64MB', '-O3', '-s', 'USE_PTHREADS'], also_asmjs=True)
3950+
self.btest_exit(test_file('pthread/test_pthread_setspecific_mainthread.c'), args=['-s', 'INITIAL_MEMORY=64MB', '-O3', '-s', 'USE_PTHREADS'], also_asmjs=True)
39513951

39523952
# Test that pthreads have access to filesystem.
39533953
@requires_threads

tests/test_core.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2342,6 +2342,11 @@ def test_pthread_cleanup(self):
23422342
self.set_setting('PTHREAD_POOL_SIZE', 4)
23432343
self.do_run_in_out_file_test('pthread/test_pthread_cleanup.cpp')
23442344

2345+
@node_pthreads
2346+
def test_pthread_setspecific_mainthread(self):
2347+
self.set_setting('EXIT_RUNTIME')
2348+
self.do_run_in_out_file_test('pthread/test_pthread_setspecific_mainthread.c')
2349+
23452350
def test_tcgetattr(self):
23462351
self.do_runf(test_file('termios/test_tcgetattr.c'), 'success')
23472352

0 commit comments

Comments
 (0)