forked from emscripten-core/emscripten
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Ensure Node worker threads are exited gracefully
Resolves (partially): emscripten-core#9763.
- Loading branch information
Showing
5 changed files
with
65 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
#include <assert.h> | ||
#include <pthread.h> | ||
#include <stdlib.h> | ||
#include <stdio.h> | ||
|
||
/* This test aims to check the following assertion: | ||
* | ||
* pthread_exit() terminates the current thread and | ||
* allows a call to pthread_join() on this thread retrieve | ||
* the argument (if the thread is not detached). | ||
* | ||
* We do this twice to ensure that the thread has exited | ||
* gracefully and that we are able to recreate the thread. | ||
*/ | ||
|
||
pthread_t t; | ||
static int thread_state; | ||
|
||
#define NUM_THREADS 2 | ||
|
||
#define NOT_CREATED_THREAD 1 | ||
#define EXITING_THREAD 2 | ||
|
||
void* thread_exit(void* arg) { | ||
printf("calling pthread_exit\n"); | ||
thread_state = EXITING_THREAD; | ||
pthread_exit(arg); | ||
printf("pthread_exit() did not terminate the thread\n"); | ||
return NULL; | ||
} | ||
|
||
int main() { | ||
printf("main\n"); | ||
for (int i = 0; i < NUM_THREADS; ++i) { | ||
thread_state = NOT_CREATED_THREAD; | ||
int rc = pthread_create(&t, NULL, thread_exit, (void*)42); | ||
assert(rc == 0); | ||
void* thread_rtn = 0; | ||
rc = pthread_join(t, &thread_rtn); | ||
assert(rc == 0); | ||
assert(thread_state == EXITING_THREAD); | ||
printf("done join -- thread exited with %ld\n", (intptr_t)thread_rtn); | ||
} | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
main | ||
calling pthread_exit | ||
done join -- thread exited with 42 | ||
calling pthread_exit | ||
done join -- thread exited with 42 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters