iconv: Suppress array out of bounds warning.
[platform/upstream/glibc.git] / nptl / tst-join5.c
1 /* Copyright (C) 2003-2015 Free Software Foundation, Inc.
2    This file is part of the GNU C Library.
3    Contributed by Ulrich Drepper <drepper@redhat.com>, 2003.
4
5    The GNU C Library is free software; you can redistribute it and/or
6    modify it under the terms of the GNU Lesser General Public
7    License as published by the Free Software Foundation; either
8    version 2.1 of the License, or (at your option) any later version.
9
10    The GNU C Library is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13    Lesser General Public License for more details.
14
15    You should have received a copy of the GNU Lesser General Public
16    License along with the GNU C Library; if not, see
17    <http://www.gnu.org/licenses/>.  */
18
19 #include <errno.h>
20 #include <pthread.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <time.h>
24 #include <unistd.h>
25 #include <sys/syscall.h>
26
27
28 #define wait_code()                                                           \
29   do {                                                                        \
30     struct timespec ts = { .tv_sec = 0, .tv_nsec = 200000000 };               \
31     while (syscall (__NR_nanosleep, &ts, &ts) < 0)                            \
32       /* nothing */;                                                          \
33   } while (0)
34
35
36 #ifdef WAIT_IN_CHILD
37 static pthread_barrier_t b;
38 #endif
39
40
41 static void *
42 tf1 (void *arg)
43 {
44 #ifdef WAIT_IN_CHILD
45   int e = pthread_barrier_wait (&b);
46   if (e != 0 && e != PTHREAD_BARRIER_SERIAL_THREAD)
47     {
48       printf ("%s: barrier_wait failed\n", __func__);
49       exit (1);
50     }
51
52   wait_code ();
53 #endif
54
55   pthread_join ((pthread_t) arg, NULL);
56
57   exit (42);
58 }
59
60
61 static void *
62 tf2 (void *arg)
63 {
64 #ifdef WAIT_IN_CHILD
65   int e = pthread_barrier_wait (&b);
66   if (e != 0 && e != PTHREAD_BARRIER_SERIAL_THREAD)
67     {
68       printf ("%s: barrier_wait failed\n", __func__);
69       exit (1);
70     }
71
72   wait_code ();
73 #endif
74   pthread_join ((pthread_t) arg, NULL);
75
76   exit (43);
77 }
78
79
80 static int
81 do_test (void)
82 {
83 #ifdef WAIT_IN_CHILD
84   if (pthread_barrier_init (&b, NULL, 2) != 0)
85     {
86       puts ("barrier_init failed");
87       return 1;
88     }
89 #endif
90
91   pthread_t th;
92
93   int err = pthread_join (pthread_self (), NULL);
94   if (err == 0)
95     {
96       puts ("1st circular join succeeded");
97       return 1;
98     }
99   if (err != EDEADLK)
100     {
101       printf ("1st circular join %d, not EDEADLK\n", err);
102       return 1;
103     }
104
105   if (pthread_create (&th, NULL, tf1, (void *) pthread_self ()) != 0)
106     {
107       puts ("1st create failed");
108       return 1;
109     }
110
111 #ifndef WAIT_IN_CHILD
112   wait_code ();
113 #endif
114
115   if (pthread_cancel (th) != 0)
116     {
117       puts ("cannot cancel 1st thread");
118       return 1;
119     }
120
121 #ifdef WAIT_IN_CHILD
122   int e = pthread_barrier_wait (&b);
123   if (e != 0 && e != PTHREAD_BARRIER_SERIAL_THREAD)
124     {
125       printf ("%s: barrier_wait failed\n", __func__);
126       return 1;
127     }
128 #endif
129
130   void *r;
131   err = pthread_join (th, &r);
132   if (err != 0)
133     {
134       printf ("cannot join 1st thread: %d\n", err);
135       return 1;
136     }
137   if (r != PTHREAD_CANCELED)
138     {
139       puts ("1st thread not canceled");
140       return 1;
141     }
142
143   err = pthread_join (pthread_self (), NULL);
144   if (err == 0)
145     {
146       puts ("2nd circular join succeeded");
147       return 1;
148     }
149   if (err != EDEADLK)
150     {
151       printf ("2nd circular join %d, not EDEADLK\n", err);
152       return 1;
153     }
154
155   if (pthread_create (&th, NULL, tf2, (void *) pthread_self ()) != 0)
156     {
157       puts ("2nd create failed");
158       return 1;
159     }
160
161 #ifndef WAIT_IN_CHILD
162   wait_code ();
163 #endif
164
165   if (pthread_cancel (th) != 0)
166     {
167       puts ("cannot cancel 2nd thread");
168       return 1;
169     }
170
171 #ifdef WAIT_IN_CHILD
172   e = pthread_barrier_wait (&b);
173   if (e != 0 && e != PTHREAD_BARRIER_SERIAL_THREAD)
174     {
175       printf ("%s: barrier_wait failed\n", __func__);
176       return 1;
177     }
178 #endif
179
180   if (pthread_join (th, &r) != 0)
181     {
182       puts ("cannot join 2nd thread");
183       return 1;
184     }
185   if (r != PTHREAD_CANCELED)
186     {
187       puts ("2nd thread not canceled");
188       return 1;
189     }
190
191   err = pthread_join (pthread_self (), NULL);
192   if (err == 0)
193     {
194       puts ("3rd circular join succeeded");
195       return 1;
196     }
197   if (err != EDEADLK)
198     {
199       printf ("3rd circular join %d, not EDEADLK\n", err);
200       return 1;
201     }
202
203   return 0;
204 }
205
206 #define TEST_FUNCTION do_test ()
207 #include "../test-skeleton.c"