2003-05-16 Roland McGrath <roland@redhat.com>
[glibc.git] / nptl / tst-once3.c
blob802864c09a188f826a158fc1b5326080a383756e
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;
37 static void
38 once_handler1 (void)
40 if (pthread_mutex_lock (&mut) != 0)
42 puts ("once_handler1: mutex_lock failed");
43 exit (1);
46 int r = pthread_barrier_wait (&bar);
47 if (r != 0 && r!= PTHREAD_BARRIER_SERIAL_THREAD)
49 puts ("once_handler1: barrier_wait failed");
50 exit (1);
53 pthread_cond_wait (&cond, &mut);
55 /* We should never get here. */
58 static void
59 once_handler2 (void)
61 global = 1;
65 static void *
66 tf (void *arg)
68 pthread_once (&once, once_handler1);
70 /* We should never get here. */
71 puts ("pthread_once in tf returned");
72 exit (1);
76 static int
77 do_test (void)
79 pthread_t th;
81 if (pthread_barrier_init (&bar, NULL, 2) != 0)
83 puts ("barrier_init failed");
84 exit (1);
87 if (pthread_create (&th, NULL, tf, NULL) != 0)
89 puts ("first create failed");
90 exit (1);
93 int r = pthread_barrier_wait (&bar);
94 if (r != 0 && r!= PTHREAD_BARRIER_SERIAL_THREAD)
96 puts ("barrier_wait failed");
97 exit (1);
100 if (pthread_mutex_lock (&mut) != 0)
102 puts ("mutex_lock failed");
103 exit (1);
105 /* We unlock the mutex so that we catch the case where the pthread_cond_wait
106 call incorrectly resumes and tries to get the mutex. */
107 if (pthread_mutex_unlock (&mut) != 0)
109 puts ("mutex_unlock failed");
110 exit (1);
113 /* Cancel the thread. */
114 if (pthread_cancel (th) != 0)
116 puts ("cancel failed");
117 exit (1);
120 void *result;
121 pthread_join (th, &result);
122 if (result != PTHREAD_CANCELED)
124 puts ("join didn't return PTHREAD_CANCELED");
125 exit (1);
128 pthread_once (&once, once_handler2);
130 if (global != 1)
132 puts ("global still 0");
133 exit (1);
136 return 0;
139 #define TEST_FUNCTION do_test ()
140 #define TIMEOUT 4
141 #include "../test-skeleton.c"