5
5
6
6
#include <pthread.h>
7
7
#include <sys/types.h>
8
+ #include <stdbool.h>
8
9
#include <stdio.h>
9
10
#include <stdlib.h>
10
11
#include <assert.h>
11
12
#include <unistd.h>
12
13
#include <errno.h>
13
- #include <emscripten.h>
14
+ #include <emscripten/console .h>
14
15
15
16
pthread_barrier_t barrier ;
16
17
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER ;
17
18
pthread_cond_t condvar = PTHREAD_COND_INITIALIZER ;
18
19
19
- int th_cancelled = 0 ;
20
-
21
- volatile int res = 43 ;
20
+ _Atomic bool th_cancelled = false;
21
+ _Atomic int result = 0 ;
22
22
23
23
static void cleanup_handler (void * arg ) {
24
- emscripten_log ( EM_LOG_CONSOLE , "Called clean-up handler with arg %p" , arg );
25
- int a = (intptr_t )(arg );
26
- res -= a ;
24
+ emscripten_outf ( "Called clean-up handler with arg %p" , arg );
25
+ result = (intptr_t )(arg );
26
+ assert ( result == 42 ) ;
27
27
28
28
pthread_mutex_unlock (& mutex );
29
29
pthread_barrier_wait (& barrier );
30
30
}
31
31
32
32
static void * thread_start (void * arg ) {
33
33
pthread_cleanup_push (cleanup_handler , (void * )42 );
34
- emscripten_log ( EM_LOG_CONSOLE , "Thread started!" );
34
+ emscripten_outf ( "Thread started!" );
35
35
pthread_mutex_lock (& mutex );
36
36
pthread_barrier_wait (& barrier );
37
37
38
38
int ret = 0 ;
39
39
do {
40
- emscripten_log ( EM_LOG_CONSOLE , "Waiting on conditional variable" );
40
+ emscripten_outf ( "Waiting on conditional variable" );
41
41
ret = pthread_cond_wait (& condvar , & mutex );
42
- } while (ret == 0 && th_cancelled == 0 );
42
+ } while (ret == 0 && ! th_cancelled );
43
43
44
44
if (ret != 0 ) {
45
- emscripten_log ( EM_LOG_CONSOLE , "Cond wait failed ret: %d" , ret );
45
+ emscripten_outf ( "Cond wait failed ret: %d" , ret );
46
46
}
47
47
48
- res = 1000 ; // Shouldn't ever reach here.
48
+ assert (false) ; // Shouldn't ever reach here.
49
49
pthread_cleanup_pop (0 );
50
50
51
51
pthread_mutex_unlock (& mutex );
@@ -59,23 +59,24 @@ int main() {
59
59
pthread_t thr ;
60
60
int s = pthread_create (& thr , NULL , thread_start , (void * )0 );
61
61
assert (s == 0 );
62
- emscripten_log ( EM_LOG_CONSOLE , "Thread created" );
62
+ emscripten_outf ( "Thread created" );
63
63
64
64
pthread_barrier_wait (& barrier );
65
65
66
66
// Lock mutex to ensure that thread is waiting
67
67
pthread_mutex_lock (& mutex );
68
68
69
- emscripten_log ( EM_LOG_CONSOLE , "Canceling thread.." );
69
+ emscripten_outf ( "Canceling thread.." );
70
70
s = pthread_cancel (thr );
71
71
assert (s == 0 );
72
- th_cancelled = 1 ;
72
+ th_cancelled = true ;
73
73
pthread_mutex_unlock (& mutex );
74
74
75
- emscripten_log ( EM_LOG_CONSOLE , "Main thread waitnig for side-thread" );
75
+ emscripten_outf ( "Main thread waitnig for side-thread" );
76
76
pthread_barrier_wait (& barrier );
77
77
pthread_barrier_destroy (& barrier );
78
78
79
- emscripten_log (EM_LOG_CONSOLE , "Test finished result: %d" , res );
80
- return res ;
79
+ emscripten_outf ("Test finished result: %d" , result );
80
+ assert (result == 42 );
81
+ return 0 ;
81
82
}
0 commit comments