Always do locking when iterating over list of streams (bug 15142)
[glibc.git] / nptl / tst-cond22.c
blob64f19ea0a55af05788aba392ca14f44d6883c67a
1 #include <pthread.h>
2 #include <stdio.h>
3 #include <stdlib.h>
6 static pthread_barrier_t b;
7 static pthread_cond_t c = PTHREAD_COND_INITIALIZER;
8 static pthread_mutex_t m = PTHREAD_MUTEX_INITIALIZER;
11 static void
12 cl (void *arg)
14 pthread_mutex_unlock (&m);
18 static void *
19 tf (void *arg)
21 if (pthread_mutex_lock (&m) != 0)
23 printf ("%s: mutex_lock failed\n", __func__);
24 exit (1);
26 int e = pthread_barrier_wait (&b);
27 if (e != 0 && e != PTHREAD_BARRIER_SERIAL_THREAD)
29 printf ("%s: barrier_wait failed\n", __func__);
30 exit (1);
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. */
38 if (pthread_cond_wait (&c, &m) != 0)
40 printf ("%s: cond_wait failed\n", __func__);
41 exit (1);
43 while (arg == NULL);
44 pthread_cleanup_pop (0);
45 if (pthread_mutex_unlock (&m) != 0)
47 printf ("%s: mutex_unlock failed\n", __func__);
48 exit (1);
50 return NULL;
54 static int
55 do_test (void)
57 int status = 0;
59 if (pthread_barrier_init (&b, NULL, 2) != 0)
61 puts ("barrier_init failed");
62 return 1;
65 pthread_t th;
66 if (pthread_create (&th, NULL, tf, NULL) != 0)
68 puts ("1st create failed");
69 return 1;
71 int e = pthread_barrier_wait (&b);
72 if (e != 0 && e != PTHREAD_BARRIER_SERIAL_THREAD)
74 puts ("1st barrier_wait failed");
75 return 1;
77 if (pthread_mutex_lock (&m) != 0)
79 puts ("1st mutex_lock failed");
80 return 1;
82 if (pthread_cond_signal (&c) != 0)
84 puts ("1st cond_signal failed");
85 return 1;
87 if (pthread_cancel (th) != 0)
89 puts ("cancel failed");
90 return 1;
92 if (pthread_mutex_unlock (&m) != 0)
94 puts ("1st mutex_unlock failed");
95 return 1;
97 void *res;
98 if (pthread_join (th, &res) != 0)
100 puts ("1st join failed");
101 return 1;
103 if (res != PTHREAD_CANCELED)
105 puts ("first thread not canceled");
106 status = 1;
109 printf ("cond = { %llu, %llu, %u/%u/%u, %u/%u/%u, %u, %u }\n",
110 c.__data.__wseq, c.__data.__g1_start,
111 c.__data.__g_signals[0], c.__data.__g_refs[0], c.__data.__g_size[0],
112 c.__data.__g_signals[1], c.__data.__g_refs[1], c.__data.__g_size[1],
113 c.__data.__g1_orig_size, c.__data.__wrefs);
115 if (pthread_create (&th, NULL, tf, (void *) 1l) != 0)
117 puts ("2nd create failed");
118 return 1;
120 e = pthread_barrier_wait (&b);
121 if (e != 0 && e != PTHREAD_BARRIER_SERIAL_THREAD)
123 puts ("2nd barrier_wait failed");
124 return 1;
126 if (pthread_mutex_lock (&m) != 0)
128 puts ("2nd mutex_lock failed");
129 return 1;
131 if (pthread_cond_signal (&c) != 0)
133 puts ("2nd cond_signal failed");
134 return 1;
136 if (pthread_mutex_unlock (&m) != 0)
138 puts ("2nd mutex_unlock failed");
139 return 1;
141 if (pthread_join (th, &res) != 0)
143 puts ("2nd join failed");
144 return 1;
146 if (res != NULL)
148 puts ("2nd thread canceled");
149 status = 1;
152 printf ("cond = { %llu, %llu, %u/%u/%u, %u/%u/%u, %u, %u }\n",
153 c.__data.__wseq, c.__data.__g1_start,
154 c.__data.__g_signals[0], c.__data.__g_refs[0], c.__data.__g_size[0],
155 c.__data.__g_signals[1], c.__data.__g_refs[1], c.__data.__g_size[1],
156 c.__data.__g1_orig_size, c.__data.__wrefs);
158 return status;
161 #define TEST_FUNCTION do_test ()
162 #include "../test-skeleton.c"