* sysdeps/unix/sysv/linux/m68k/semtimedop.S: New file.
[glibc/pb-stable.git] / nptl / tst-once4.c
blobbe83b54692354d4935cab28bd6ef302139cf34d9
1 /* Copyright (C) 2003 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, 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>
24 #include <unistd.h>
27 #define N 100
29 static pthread_once_t once = PTHREAD_ONCE_INIT;
31 static pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
32 static pthread_mutex_t mut = PTHREAD_MUTEX_INITIALIZER;
34 static pthread_barrier_t bar;
36 static int global;
38 static void
39 once_handler1 (void)
41 if (pthread_mutex_lock (&mut) != 0)
43 puts ("once_handler1: mutex_lock failed");
44 exit (1);
47 int r = pthread_barrier_wait (&bar);
48 if (r != 0 && r!= PTHREAD_BARRIER_SERIAL_THREAD)
50 puts ("once_handler1: barrier_wait failed");
51 exit (1);
54 pthread_cond_wait (&cond, &mut);
56 /* We should never get here. */
60 static void
61 once_handler2 (void)
63 global = 1;
67 static void *
68 tf1 (void *arg)
70 pthread_once (&once, once_handler1);
72 /* We should never get here. */
73 puts ("pthread_once in tf returned");
74 exit (1);
78 static void *
79 tf2 (void *arg)
81 int r = pthread_barrier_wait (&bar);
82 if (r != 0 && r!= PTHREAD_BARRIER_SERIAL_THREAD)
84 puts ("once_handler2: barrier_wait failed");
85 exit (1);
88 pthread_once (&once, once_handler2);
90 return NULL;
94 static int
95 do_test (void)
97 pthread_t th[2];
99 if (pthread_barrier_init (&bar, NULL, 2) != 0)
101 puts ("barrier_init failed");
102 exit (1);
105 if (pthread_create (&th[0], NULL, tf1, NULL) != 0)
107 puts ("first create failed");
108 exit (1);
111 int r = pthread_barrier_wait (&bar);
112 if (r != 0 && r!= PTHREAD_BARRIER_SERIAL_THREAD)
114 puts ("first barrier_wait failed");
115 exit (1);
118 if (pthread_mutex_lock (&mut) != 0)
120 puts ("mutex_lock failed");
121 exit (1);
123 /* We unlock the mutex so that we catch the case where the pthread_cond_wait
124 call incorrectly resumes and tries to get the mutex. */
125 if (pthread_mutex_unlock (&mut) != 0)
127 puts ("mutex_unlock failed");
128 exit (1);
131 if (pthread_create (&th[1], NULL, tf2, NULL) != 0)
133 puts ("second create failed");
134 exit (1);
137 r = pthread_barrier_wait (&bar);
138 if (r != 0 && r!= PTHREAD_BARRIER_SERIAL_THREAD)
140 puts ("second barrier_wait failed");
141 exit (1);
144 /* Give the second thread a chance to reach the pthread_once call. */
145 sleep (2);
147 /* Cancel the thread. */
148 if (pthread_cancel (th[0]) != 0)
150 puts ("cancel failed");
151 exit (1);
154 void *result;
155 pthread_join (th[0], &result);
156 if (result != PTHREAD_CANCELED)
158 puts ("first join didn't return PTHREAD_CANCELED");
159 exit (1);
162 puts ("joined first thread");
164 pthread_join (th[1], &result);
165 if (result != NULL)
167 puts ("second join didn't return PTHREAD_CANCELED");
168 exit (1);
171 if (global != 1)
173 puts ("global still 0");
174 exit (1);
177 return 0;
180 #define TEST_FUNCTION do_test ()
181 #define TIMEOUT 4
182 #include "../test-skeleton.c"