ldbl-128ibm-compat: Provide nexttoward functions
[glibc.git] / nptl / tst-cancel25.c
blobc6e1c23c02bed64de8418e22357498fc024b8fe0
1 #include <pthread.h>
2 #include <signal.h>
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <internal-signals.h>
8 static pthread_barrier_t b;
9 static pthread_t th2;
12 static void *
13 tf2 (void *arg)
15 sigset_t mask;
16 if (pthread_sigmask (SIG_SETMASK, NULL, &mask) != 0)
18 puts ("pthread_sigmask failed");
19 exit (1);
21 if (sigismember (&mask, SIGCANCEL))
23 puts ("SIGCANCEL blocked in new thread");
24 exit (1);
27 /* Sync with the main thread so that we do not test anything else. */
28 int e = pthread_barrier_wait (&b);
29 if (e != 0 && e != PTHREAD_BARRIER_SERIAL_THREAD)
31 puts ("barrier_wait failed");
32 exit (1);
35 while (1)
37 /* Just a cancelable call. */
38 struct timespec ts = { 10000, 0 };
39 nanosleep (&ts, 0);
42 return NULL;
46 static void
47 unwhand (void *arg)
49 if (pthread_create (&th2, NULL, tf2, NULL) != 0)
51 puts ("unwhand: create failed");
52 exit (1);
57 static void *
58 tf (void *arg)
60 pthread_cleanup_push (unwhand, NULL);
62 /* Sync with the main thread so that we do not test anything else. */
63 int e = pthread_barrier_wait (&b);
64 if (e != 0 && e != PTHREAD_BARRIER_SERIAL_THREAD)
66 puts ("barrier_wait failed");
67 exit (1);
70 while (1)
72 /* Just a cancelable call. */
73 struct timespec ts = { 10000, 0 };
74 nanosleep (&ts, 0);
77 pthread_cleanup_pop (0);
79 return NULL;
83 static int
84 do_test (void)
86 if (pthread_barrier_init (&b, NULL, 2) != 0)
88 puts ("barrier_init failed");
89 return 1;
92 pthread_t th1;
93 if (pthread_create (&th1, NULL, tf, NULL) != 0)
95 puts ("create failed");
96 return 1;
99 int e = pthread_barrier_wait (&b);
100 if (e != 0 && e != PTHREAD_BARRIER_SERIAL_THREAD)
102 puts ("barrier_wait failed");
103 return 1;
106 /* Make sure tf1 enters nanosleep. */
107 struct timespec ts = { 0, 500000000 };
108 while (nanosleep (&ts, &ts) != 0)
111 if (pthread_cancel (th1) != 0)
113 puts ("1st cancel failed");
114 return 1;
117 void *res;
118 if (pthread_join (th1, &res) != 0)
120 puts ("1st join failed");
121 return 1;
123 if (res != PTHREAD_CANCELED)
125 puts ("1st thread not canceled");
126 return 1;
129 e = pthread_barrier_wait (&b);
130 if (e != 0 && e != PTHREAD_BARRIER_SERIAL_THREAD)
132 puts ("barrier_wait failed");
133 return 1;
136 /* Make sure tf2 enters nanosleep. */
137 ts.tv_sec = 0;
138 ts.tv_nsec = 500000000;
139 while (nanosleep (&ts, &ts) != 0)
142 puts ("calling pthread_cancel the second time");
143 if (pthread_cancel (th2) != 0)
145 puts ("2nd cancel failed");
146 return 1;
149 puts ("calling pthread_join the second time");
150 if (pthread_join (th2, &res) != 0)
152 puts ("2nd join failed");
153 return 1;
155 if (res != PTHREAD_CANCELED)
157 puts ("2nd thread not canceled");
158 return 1;
161 if (pthread_barrier_destroy (&b) != 0)
163 puts ("barrier_destroy failed");
164 return 0;
167 return 0;
170 #define TEST_FUNCTION do_test ()
171 #include "../test-skeleton.c"