Skip to content

Commit 786473b

Browse files
committed
Address review comments
1 parent 8640757 commit 786473b

File tree

2 files changed

+16
-13
lines changed

2 files changed

+16
-13
lines changed

tests/pthread/test_pthread_atexit.c

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,33 +3,35 @@
33
#include <stdio.h>
44
#include <stdlib.h>
55

6-
#include "emscripten.h"
7-
#include "emscripten/threading.h"
6+
pthread_mutex_t count_lock = PTHREAD_MUTEX_INITIALIZER;
7+
pthread_cond_t count_nonzero = PTHREAD_COND_INITIALIZER;
8+
unsigned count;
89

9-
static _Atomic int threadCounter = 0;
10-
static _Atomic int running = 1;
11-
static pthread_t thread;
10+
pthread_t thread;
1211

1312
void *workerThread(void* arg) {
14-
threadCounter++;
15-
16-
while (running)
17-
emscripten_thread_sleep(1000);
18-
19-
threadCounter--;
13+
pthread_mutex_lock(&count_lock);
14+
while (count == 0)
15+
pthread_cond_wait(&count_nonzero, &count_lock);
16+
count--;
17+
pthread_mutex_unlock(&count_lock);
2018

2119
return NULL;
2220
}
2321

2422
void terminateThread() {
25-
running = 0;
23+
pthread_mutex_lock(&count_lock);
24+
if (count == 0)
25+
pthread_cond_signal(&count_nonzero);
26+
count++;
27+
pthread_mutex_unlock(&count_lock);
2628

2729
int res = 0;
2830
int rc = pthread_join(thread, (void**)&res);
2931
assert(rc == 0);
3032
assert(res == 0);
3133

32-
printf("done waiting - counter is: %d\n", threadCounter);
34+
printf("done waiting - counter is: %d\n", count);
3335
}
3436

3537
int main(int argc, char* argv[]) {

tests/test_core.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2324,6 +2324,7 @@ def test_pthread_dispatch_after_exit(self):
23242324

23252325
@node_pthreads
23262326
def test_pthread_atexit(self):
2327+
# Test whether we can terminate a running thread during atexit.
23272328
self.set_setting('EXIT_RUNTIME')
23282329
self.set_setting('PTHREAD_POOL_SIZE', 1)
23292330
self.do_run_in_out_file_test('pthread/test_pthread_atexit.c')

0 commit comments

Comments
 (0)