Skip to content

Fix flakiness in test_pthread_c11_threads #15178

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 2 commits into from
Sep 29, 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 @@ -1259,14 +1259,17 @@ var LibraryPThread = {
#endif
},

// This function is call by a pthread to signal that exit() was called and
// that the entire process should exit.
// This function is always called from a pthread, but is executed on the
// main thread due the __proxy attribute.
$exitOnMainThread__deps: ['exit',
#if !MINIMAL_RUNTIME
'$handleException',
#endif
],
$exitOnMainThread__proxy: 'async',
$exitOnMainThread: function(returnCode) {
// A pthread has requested to exit the whole application process (runtime).
#if PTHREADS_DEBUG
err('exitOnMainThread');
#endif
Expand Down
22 changes: 16 additions & 6 deletions tests/pthread/test_pthread_c11_threads.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,13 @@ void do_once(void) {
}

int thread_main(void* arg) {
printf("in thread_main %p\n", thrd_current());
tss_set(key, thrd_current());
printf("in thread_main %p\n", (void*)thrd_current());
tss_set(key, (void*)thrd_current());
call_once(&flag, do_once);
mtx_lock(&mutex);
thread_counter--;
cnd_signal(&cond);
mtx_unlock(&mutex);
printf("done thread_main\n");
return 42;
}

Expand All @@ -35,14 +34,14 @@ int run_with_exit(void* arg) {
}

void destructor(void* val) {
printf("destructor: %p\n", thrd_current());
assert(val == thrd_current());
printf("destructor: %p\n", (void*)thrd_current());
assert(val == (void*)thrd_current());
destructor_counter++;
}

int main(int argc, char* argv[]) {
int result = 0;
printf("thrd_current: %p\n", thrd_current());
printf("thrd_current: %p\n", (void*)thrd_current());

assert(tss_create(&key, destructor) == thrd_success);

Expand Down Expand Up @@ -89,8 +88,19 @@ int main(int argc, char* argv[]) {
// Test thrd_detach
thrd_t t6;
assert(thrd_create(&t6, thread_main, NULL) == thrd_success);
thread_counter++;
assert(thrd_detach(t6) == thrd_success);

// Wait for the thread to at least be done printing before exiting
// the process.
// We shouldn't need to do this but there is a bug in emscripten
// where a deadlock can occur between main thread calling fflush()
// during exitRuntime and the detached thread calling print (and
// therefore holding the stdout lock).
// See https://github.com/emscripten-core/emscripten/issues/14579.
while (thread_counter)
assert(cnd_wait(&cond, &mutex) == thrd_success);

printf("done!\n");
return 0;
}