blob: bd978e50caa4ba76315626be1c489fab87e81ce7 [file] [log] [blame]
lh9ed821d2023-04-07 01:36:19 -07001#include <pthread.h>
2#include <stdio.h>
3#include <stdlib.h>
4
5
6static pthread_barrier_t b;
7static pthread_cond_t c = PTHREAD_COND_INITIALIZER;
8static pthread_mutex_t m = PTHREAD_MUTEX_INITIALIZER;
9
10
11static void
12cl (void *arg)
13{
14 pthread_mutex_unlock (&m);
15}
16
17
18static void *
19tf (void *arg)
20{
21 if (pthread_mutex_lock (&m) != 0)
22 {
23 printf ("%s: mutex_lock failed\n", __func__);
24 exit (1);
25 }
26 int e = pthread_barrier_wait (&b);
27 if (e != 0 && e != PTHREAD_BARRIER_SERIAL_THREAD)
28 {
29 printf ("%s: barrier_wait failed\n", __func__);
30 exit (1);
31 }
32 pthread_cleanup_push (cl, NULL);
33 /* We have to loop here because the cancellation might come after
34 the cond_wait call left the cancelable area and is then waiting
35 on the mutex. In this case the beginning of the second cond_wait
36 call will cause the cancellation to happen. */
37 do
38 if (pthread_cond_wait (&c, &m) != 0)
39 {
40 printf ("%s: cond_wait failed\n", __func__);
41 exit (1);
42 }
43 while (arg == NULL);
44 pthread_cleanup_pop (0);
45 if (pthread_mutex_unlock (&m) != 0)
46 {
47 printf ("%s: mutex_unlock failed\n", __func__);
48 exit (1);
49 }
50 return NULL;
51}
52
53
54static int
55do_test (void)
56{
57 int status = 0;
58
59 if (pthread_barrier_init (&b, NULL, 2) != 0)
60 {
61 puts ("barrier_init failed");
62 return 1;
63 }
64
65 pthread_t th;
66 if (pthread_create (&th, NULL, tf, NULL) != 0)
67 {
68 puts ("1st create failed");
69 return 1;
70 }
71 int e = pthread_barrier_wait (&b);
72 if (e != 0 && e != PTHREAD_BARRIER_SERIAL_THREAD)
73 {
74 puts ("1st barrier_wait failed");
75 return 1;
76 }
77 if (pthread_mutex_lock (&m) != 0)
78 {
79 puts ("1st mutex_lock failed");
80 return 1;
81 }
82 if (pthread_cond_signal (&c) != 0)
83 {
84 puts ("1st cond_signal failed");
85 return 1;
86 }
87 if (pthread_cancel (th) != 0)
88 {
89 puts ("cancel failed");
90 return 1;
91 }
92 if (pthread_mutex_unlock (&m) != 0)
93 {
94 puts ("1st mutex_unlock failed");
95 return 1;
96 }
97 void *res;
98 if (pthread_join (th, &res) != 0)
99 {
100 puts ("1st join failed");
101 return 1;
102 }
103 if (res != PTHREAD_CANCELED)
104 {
105 puts ("first thread not canceled");
106 status = 1;
107 }
108
109 printf ("cond = { %d, %x, %lld, %lld, %lld, %p, %u, %u }\n",
110 c.__data.__lock, c.__data.__futex, c.__data.__total_seq,
111 c.__data.__wakeup_seq, c.__data.__woken_seq, c.__data.__mutex,
112 c.__data.__nwaiters, c.__data.__broadcast_seq);
113
114 if (pthread_create (&th, NULL, tf, (void *) 1l) != 0)
115 {
116 puts ("2nd create failed");
117 return 1;
118 }
119 e = pthread_barrier_wait (&b);
120 if (e != 0 && e != PTHREAD_BARRIER_SERIAL_THREAD)
121 {
122 puts ("2nd barrier_wait failed");
123 return 1;
124 }
125 if (pthread_mutex_lock (&m) != 0)
126 {
127 puts ("2nd mutex_lock failed");
128 return 1;
129 }
130 if (pthread_cond_signal (&c) != 0)
131 {
132 puts ("2nd cond_signal failed");
133 return 1;
134 }
135 if (pthread_mutex_unlock (&m) != 0)
136 {
137 puts ("2nd mutex_unlock failed");
138 return 1;
139 }
140 if (pthread_join (th, &res) != 0)
141 {
142 puts ("2nd join failed");
143 return 1;
144 }
145 if (res != NULL)
146 {
147 puts ("2nd thread canceled");
148 status = 1;
149 }
150
151 printf ("cond = { %d, %x, %lld, %lld, %lld, %p, %u, %u }\n",
152 c.__data.__lock, c.__data.__futex, c.__data.__total_seq,
153 c.__data.__wakeup_seq, c.__data.__woken_seq, c.__data.__mutex,
154 c.__data.__nwaiters, c.__data.__broadcast_seq);
155
156 return status;
157}
158
159#define TEST_FUNCTION do_test ()
160#include "../test-skeleton.c"