55
66#include <pthread.h>
77#include <sys/types.h>
8+ #include <stdbool.h>
89#include <stdio.h>
910#include <stdlib.h>
1011#include <assert.h>
1112#include <unistd.h>
1213#include <errno.h>
13- #include <emscripten.h>
14+ #include <emscripten/console .h>
1415
1516pthread_barrier_t barrier ;
1617pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER ;
1718pthread_cond_t condvar = PTHREAD_COND_INITIALIZER ;
1819
19- int th_cancelled = 0 ;
20-
21- volatile int res = 43 ;
20+ _Atomic bool th_cancelled = false;
21+ _Atomic int result = 0 ;
2222
2323static 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 ) ;
2727
2828 pthread_mutex_unlock (& mutex );
2929 pthread_barrier_wait (& barrier );
3030}
3131
3232static void * thread_start (void * arg ) {
3333 pthread_cleanup_push (cleanup_handler , (void * )42 );
34- emscripten_log ( EM_LOG_CONSOLE , "Thread started!" );
34+ emscripten_outf ( "Thread started!" );
3535 pthread_mutex_lock (& mutex );
3636 pthread_barrier_wait (& barrier );
3737
3838 int ret = 0 ;
3939 do {
40- emscripten_log ( EM_LOG_CONSOLE , "Waiting on conditional variable" );
40+ emscripten_outf ( "Waiting on conditional variable" );
4141 ret = pthread_cond_wait (& condvar , & mutex );
42- } while (ret == 0 && th_cancelled == 0 );
42+ } while (ret == 0 && ! th_cancelled );
4343
4444 if (ret != 0 ) {
45- emscripten_log ( EM_LOG_CONSOLE , "Cond wait failed ret: %d" , ret );
45+ emscripten_outf ( "Cond wait failed ret: %d" , ret );
4646 }
4747
48- res = 1000 ; // Shouldn't ever reach here.
48+ assert (false) ; // Shouldn't ever reach here.
4949 pthread_cleanup_pop (0 );
5050
5151 pthread_mutex_unlock (& mutex );
@@ -59,23 +59,24 @@ int main() {
5959 pthread_t thr ;
6060 int s = pthread_create (& thr , NULL , thread_start , (void * )0 );
6161 assert (s == 0 );
62- emscripten_log ( EM_LOG_CONSOLE , "Thread created" );
62+ emscripten_outf ( "Thread created" );
6363
6464 pthread_barrier_wait (& barrier );
6565
6666 // Lock mutex to ensure that thread is waiting
6767 pthread_mutex_lock (& mutex );
6868
69- emscripten_log ( EM_LOG_CONSOLE , "Canceling thread.." );
69+ emscripten_outf ( "Canceling thread.." );
7070 s = pthread_cancel (thr );
7171 assert (s == 0 );
72- th_cancelled = 1 ;
72+ th_cancelled = true ;
7373 pthread_mutex_unlock (& mutex );
7474
75- emscripten_log ( EM_LOG_CONSOLE , "Main thread waitnig for side-thread" );
75+ emscripten_outf ( "Main thread waitnig for side-thread" );
7676 pthread_barrier_wait (& barrier );
7777 pthread_barrier_destroy (& barrier );
7878
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 ;
8182}
0 commit comments