.
[glibc/history.git] / nptl / tst-cancel24.cc
blob1af709a8cab1d422ef4401e2b2d178df86f863c5
1 #include <pthread.h>
2 #include <semaphore.h>
3 #include <stdlib.h>
4 #include <stdio.h>
5 #include <unistd.h>
8 static volatile bool destr_called;
9 static volatile bool except_caught;
11 static pthread_barrier_t b;
14 struct monitor
16 // gcc is broken and would generate a warning without this dummy
17 // constructor.
18 monitor () { }
19 ~monitor() { destr_called = true; }
23 static void *
24 tf (void *arg)
26 sem_t *s = static_cast<sem_t *> (arg);
28 try
30 monitor m;
32 pthread_barrier_wait (&b);
34 while (1)
35 sem_wait (s);
37 catch (...)
39 except_caught = true;
40 throw;
43 return NULL;
47 static int
48 do_test ()
50 if (pthread_barrier_init (&b, NULL, 2) != 0)
52 puts ("barrier_init failed");
53 return 1;
56 sem_t s;
57 if (sem_init (&s, 0, 0) != 0)
59 puts ("sem_init failed");
60 return 1;
63 pthread_t th;
64 if (pthread_create (&th, NULL, tf, &s) != 0)
66 puts ("pthread_create failed");
67 return 1;
70 pthread_barrier_wait (&b);
72 /* There is unfortunately no better method to try to assure the
73 child thread reached the sem_wait call and is actually waiting
74 than to sleep here. */
75 sleep (1);
77 if (pthread_cancel (th) != 0)
79 puts ("cancel failed");
80 return 1;
83 void *res;
84 if (pthread_join (th, &res) != 0)
86 puts ("join failed");
87 return 1;
90 if (res != PTHREAD_CANCELED)
92 puts ("thread was not canceled");
93 return 1;
96 if (! except_caught)
98 puts ("exception not caught");
99 return 1;
102 if (! destr_called)
104 puts ("destructor not called");
105 return 1;
108 return 0;
111 #define TEST_FUNCTION do_test ()
112 #define TIMEOUT 3
113 #include "../test-skeleton.c"