(glibc.spec): Drop Makefile dep.
[glibc.git] / nptl / tst-once3.c
blob43b354a391e9eb210d945d7b194a59ca4b08fa56
1 /* Copyright (C) 2002, 2003 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Ulrich Drepper <drepper@redhat.com>, 2002.
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, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18 02111-1307 USA. */
20 #include <pthread.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <time.h>
26 #define N 100
28 static pthread_once_t once = PTHREAD_ONCE_INIT;
30 static pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
31 static pthread_mutex_t mut = PTHREAD_MUTEX_INITIALIZER;
33 static pthread_barrier_t bar;
35 static int global;
36 static int cl_called;
38 static void
39 once_handler1 (void)
41 if (pthread_mutex_lock (&mut) != 0)
43 puts ("once_handler1: mutex_lock failed");
44 exit (1);
46 puts ("once_handler1: locked");
48 int r = pthread_barrier_wait (&bar);
49 if (r != 0 && r!= PTHREAD_BARRIER_SERIAL_THREAD)
51 puts ("once_handler1: barrier_wait failed");
52 exit (1);
55 pthread_cond_wait (&cond, &mut);
57 /* We should never get here. */
58 exit (42);
61 static void
62 once_handler2 (void)
64 global = 1;
68 static void
69 cl (void *arg)
71 cl_called = 1;
75 static void *
76 tf (void *arg)
78 pthread_cleanup_push (cl, NULL)
80 pthread_once (&once, once_handler1);
82 pthread_cleanup_pop (0);
84 /* We should never get here. */
85 puts ("pthread_once in tf returned");
86 exit (1);
90 static int
91 do_test (void)
93 pthread_t th;
95 if (pthread_barrier_init (&bar, NULL, 2) != 0)
97 puts ("barrier_init failed");
98 return 1;
101 if (pthread_create (&th, NULL, tf, NULL) != 0)
103 puts ("first create failed");
104 return 1;
107 int r = pthread_barrier_wait (&bar);
108 if (r != 0 && r!= PTHREAD_BARRIER_SERIAL_THREAD)
110 puts ("barrier_wait failed");
111 return 1;
114 if (pthread_mutex_lock (&mut) != 0)
116 puts ("mutex_lock failed");
117 return 1;
119 /* We unlock the mutex so that we catch the case where the pthread_cond_wait
120 call incorrectly resumes and tries to get the mutex. */
121 if (pthread_mutex_unlock (&mut) != 0)
123 puts ("mutex_unlock failed");
124 return 1;
127 /* Cancel the thread. */
128 puts ("going to cancel");
129 if (pthread_cancel (th) != 0)
131 puts ("cancel failed");
132 return 1;
135 void *result;
136 pthread_join (th, &result);
137 if (result != PTHREAD_CANCELED)
139 puts ("join didn't return PTHREAD_CANCELED");
140 return 1;
143 if (cl_called != 1)
145 puts ("cleanup handler not called");
146 return 1;
149 pthread_once (&once, once_handler2);
151 if (global != 1)
153 puts ("global still 0");
154 return 1;
157 return 0;
160 #define TEST_FUNCTION do_test ()
161 #define TIMEOUT 4
162 #include "../test-skeleton.c"