blob: 00b99ad553f679388c695d7fb9e382a19362c503 [file] [log] [blame]
lh9ed821d2023-04-07 01:36:19 -07001#include <pthreadP.h>
2#include <signal.h>
3#include <stdio.h>
4#include <stdlib.h>
5
6
7static pthread_barrier_t b;
8static pthread_t th2;
9
10
11static void *
12tf2 (void *arg)
13{
14 sigset_t mask;
15 if (pthread_sigmask (SIG_SETMASK, NULL, &mask) != 0)
16 {
17 puts ("pthread_sigmask failed");
18 exit (1);
19 }
20 if (sigismember (&mask, SIGCANCEL))
21 {
22 puts ("SIGCANCEL blocked in new thread");
23 exit (1);
24 }
25
26 /* Sync with the main thread so that we do not test anything else. */
27 int e = pthread_barrier_wait (&b);
28 if (e != 0 && e != PTHREAD_BARRIER_SERIAL_THREAD)
29 {
30 puts ("barrier_wait failed");
31 exit (1);
32 }
33
34 while (1)
35 {
36 /* Just a cancelable call. */
37 struct timespec ts = { 10000, 0 };
38 nanosleep (&ts, 0);
39 }
40
41 return NULL;
42}
43
44
45static void
46unwhand (void *arg)
47{
48 if (pthread_create (&th2, NULL, tf2, NULL) != 0)
49 {
50 puts ("unwhand: create failed");
51 exit (1);
52 }
53}
54
55
56static void *
57tf (void *arg)
58{
59 pthread_cleanup_push (unwhand, NULL);
60
61 /* Sync with the main thread so that we do not test anything else. */
62 int e = pthread_barrier_wait (&b);
63 if (e != 0 && e != PTHREAD_BARRIER_SERIAL_THREAD)
64 {
65 puts ("barrier_wait failed");
66 exit (1);
67 }
68
69 while (1)
70 {
71 /* Just a cancelable call. */
72 struct timespec ts = { 10000, 0 };
73 nanosleep (&ts, 0);
74 }
75
76 pthread_cleanup_pop (0);
77
78 return NULL;
79}
80
81
82static int
83do_test (void)
84{
85 if (pthread_barrier_init (&b, NULL, 2) != 0)
86 {
87 puts ("barrier_init failed");
88 return 1;
89 }
90
91 pthread_t th1;
92 if (pthread_create (&th1, NULL, tf, NULL) != 0)
93 {
94 puts ("create failed");
95 return 1;
96 }
97
98 int e = pthread_barrier_wait (&b);
99 if (e != 0 && e != PTHREAD_BARRIER_SERIAL_THREAD)
100 {
101 puts ("barrier_wait failed");
102 return 1;
103 }
104
105 /* Make sure tf1 enters nanosleep. */
106 struct timespec ts = { 0, 500000000 };
107 while (nanosleep (&ts, &ts) != 0)
108 ;
109
110 if (pthread_cancel (th1) != 0)
111 {
112 puts ("1st cancel failed");
113 return 1;
114 }
115
116 void *res;
117 if (pthread_join (th1, &res) != 0)
118 {
119 puts ("1st join failed");
120 return 1;
121 }
122 if (res != PTHREAD_CANCELED)
123 {
124 puts ("1st thread not canceled");
125 return 1;
126 }
127
128 e = pthread_barrier_wait (&b);
129 if (e != 0 && e != PTHREAD_BARRIER_SERIAL_THREAD)
130 {
131 puts ("barrier_wait failed");
132 return 1;
133 }
134
135 /* Make sure tf2 enters nanosleep. */
136 ts.tv_sec = 0;
137 ts.tv_nsec = 500000000;
138 while (nanosleep (&ts, &ts) != 0)
139 ;
140
141 puts ("calling pthread_cancel the second time");
142 if (pthread_cancel (th2) != 0)
143 {
144 puts ("2nd cancel failed");
145 return 1;
146 }
147
148 puts ("calling pthread_join the second time");
149 if (pthread_join (th2, &res) != 0)
150 {
151 puts ("2nd join failed");
152 return 1;
153 }
154 if (res != PTHREAD_CANCELED)
155 {
156 puts ("2nd thread not canceled");
157 return 1;
158 }
159
160 if (pthread_barrier_destroy (&b) != 0)
161 {
162 puts ("barrier_destroy failed");
163 return 0;
164 }
165
166 return 0;
167}
168
169#define TEST_FUNCTION do_test ()
170#define TIMEOUT 4
171#include "../test-skeleton.c"