Skip to content
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
7 changes: 2 additions & 5 deletions src/postamble.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ function callMain(args) {

#if ABORT_ON_WASM_EXCEPTIONS
// See abortWrapperDepth in preamble.js!
abortWrapperDepth += 2;
abortWrapperDepth += 2;
#endif

#if STANDALONE_WASM
Expand Down Expand Up @@ -202,7 +202,7 @@ function callMain(args) {

#if ABORT_ON_WASM_EXCEPTIONS
// See abortWrapperDepth in preamble.js!
abortWrapperDepth -= 2;
abortWrapperDepth -= 2;
#endif
}
}
Expand Down Expand Up @@ -440,9 +440,6 @@ function exit(status, implicit) {
}
#endif // ASSERTIONS
} else {
#if USE_PTHREADS
PThread.terminateAllThreads();
#endif
exitRuntime();
}

Expand Down
3 changes: 3 additions & 0 deletions src/postamble_minimal.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ function run() {
callRuntimeCallbacks(__ATEXIT__);
<<< ATEXITS >>>
#endif
#if USE_PTHREADS
PThread.terminateAllThreads();
#endif

#if IN_TEST_HARNESS
// fflush() filesystem stdio for test harness, since there are existing
Expand Down
3 changes: 3 additions & 0 deletions src/preamble.js
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,9 @@ function exitRuntime() {
#if EXIT_RUNTIME
callRuntimeCallbacks(__ATEXIT__);
<<< ATEXITS >>>
#endif
#if USE_PTHREADS
PThread.terminateAllThreads();
#endif
runtimeExited = true;
}
Expand Down
43 changes: 43 additions & 0 deletions tests/pthread/test_pthread_atexit.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#include <assert.h>
#include <pthread.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>

pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
bool should_exit;

pthread_t thread;

void *workerThread(void* arg) {
pthread_mutex_lock(&mutex);
while (!should_exit)
pthread_cond_wait(&cond, &mutex);
pthread_mutex_unlock(&mutex);

return NULL;
}

void terminateThread() {
pthread_mutex_lock(&mutex);
should_exit = true;
pthread_cond_signal(&cond);
pthread_mutex_unlock(&mutex);

int res = 0;
int rc = pthread_join(thread, (void**)&res);
assert(rc == 0);
assert(res == 0);

printf("done waiting - thread successfully terminated\n");
}

int main(int argc, char* argv[]) {
int rc = atexit(terminateThread);
assert(rc == 0);

rc = pthread_create(&thread, NULL, workerThread, NULL);
assert(rc == 0);
return 0;
}
1 change: 1 addition & 0 deletions tests/pthread/test_pthread_atexit.out
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
done waiting - thread successfully terminated
7 changes: 7 additions & 0 deletions tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -2323,6 +2323,13 @@ def test_pthread_equal(self):
def test_pthread_dispatch_after_exit(self):
self.do_run_in_out_file_test('pthread/test_pthread_dispatch_after_exit.c', interleaved_output=False)

@node_pthreads
def test_pthread_atexit(self):

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Can we add a comment either here or on the C source file describing exactly what we are testing?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Good idea! Done with commit 786473b.

# Test to ensure threads are still running when atexit-registered functions are called
self.set_setting('EXIT_RUNTIME')
self.set_setting('PTHREAD_POOL_SIZE', 1)
self.do_run_in_out_file_test('pthread/test_pthread_atexit.c')

@node_pthreads
def test_pthread_nested_work_queue(self):
self.set_setting('EXIT_RUNTIME')
Expand Down