Fix features.h for -Wundef (bug 19212).
[glibc.git] / nptl / tst-once4.c
blob54bc0f121439159ae296dc3d825220b2fd56c603
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.
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.
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.
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/>. */
19 #include <pthread.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <time.h>
23 #include <unistd.h>
26 static pthread_once_t once = PTHREAD_ONCE_INIT;
28 static pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
29 static pthread_mutex_t mut = PTHREAD_MUTEX_INITIALIZER;
31 static pthread_barrier_t bar;
33 static int global;
34 static int cl_called;
36 static void
37 once_handler1 (void)
39 if (pthread_mutex_lock (&mut) != 0)
41 puts ("once_handler1: mutex_lock failed");
42 exit (1);
45 int r = pthread_barrier_wait (&bar);
46 if (r != 0 && r!= PTHREAD_BARRIER_SERIAL_THREAD)
48 puts ("once_handler1: barrier_wait failed");
49 exit (1);
52 pthread_cond_wait (&cond, &mut);
54 /* We should never get here. */
58 static void
59 once_handler2 (void)
61 global = 1;
65 static void
66 cl (void *arg)
68 ++cl_called;
72 static void *
73 tf1 (void *arg)
75 pthread_cleanup_push (cl, NULL);
77 pthread_once (&once, once_handler1);
79 pthread_cleanup_pop (0);
81 /* We should never get here. */
82 puts ("pthread_once in tf returned");
83 exit (1);
87 static void *
88 tf2 (void *arg)
90 pthread_cleanup_push (cl, NULL);
92 int r = pthread_barrier_wait (&bar);
93 if (r != 0 && r!= PTHREAD_BARRIER_SERIAL_THREAD)
95 puts ("once_handler2: barrier_wait failed");
96 exit (1);
99 pthread_cleanup_pop (0);
101 pthread_once (&once, once_handler2);
103 return NULL;
107 static int
108 do_test (void)
110 pthread_t th[2];
112 if (pthread_barrier_init (&bar, NULL, 2) != 0)
114 puts ("barrier_init failed");
115 return 1;
118 if (pthread_create (&th[0], NULL, tf1, NULL) != 0)
120 puts ("first create failed");
121 return 1;
124 int r = pthread_barrier_wait (&bar);
125 if (r != 0 && r!= PTHREAD_BARRIER_SERIAL_THREAD)
127 puts ("first barrier_wait failed");
128 return 1;
131 if (pthread_mutex_lock (&mut) != 0)
133 puts ("mutex_lock failed");
134 return 1;
136 /* We unlock the mutex so that we catch the case where the pthread_cond_wait
137 call incorrectly resumes and tries to get the mutex. */
138 if (pthread_mutex_unlock (&mut) != 0)
140 puts ("mutex_unlock failed");
141 return 1;
144 if (pthread_create (&th[1], NULL, tf2, NULL) != 0)
146 puts ("second create failed");
147 return 1;
150 r = pthread_barrier_wait (&bar);
151 if (r != 0 && r!= PTHREAD_BARRIER_SERIAL_THREAD)
153 puts ("second barrier_wait failed");
154 return 1;
157 /* Give the second thread a chance to reach the pthread_once call. */
158 sleep (2);
160 /* Cancel the thread. */
161 if (pthread_cancel (th[0]) != 0)
163 puts ("cancel failed");
164 return 1;
167 void *result;
168 pthread_join (th[0], &result);
169 if (result != PTHREAD_CANCELED)
171 puts ("first join didn't return PTHREAD_CANCELED");
172 return 1;
175 puts ("joined first thread");
177 pthread_join (th[1], &result);
178 if (result != NULL)
180 puts ("second join didn't return PTHREAD_CANCELED");
181 return 1;
184 if (global != 1)
186 puts ("global still 0");
187 return 1;
190 if (cl_called != 1)
192 printf ("cl_called = %d\n", cl_called);
193 return 1;
196 return 0;
199 #define TEST_FUNCTION do_test ()
200 #define TIMEOUT 4
201 #include "../test-skeleton.c"