]> git.ipfire.org Git - thirdparty/glibc.git/blame - nptl/tst-once4.c
Update copyright notices with scripts/update-copyrights
[thirdparty/glibc.git] / nptl / tst-once4.c
CommitLineData
d4697bc9 1/* Copyright (C) 2003-2014 Free Software Foundation, Inc.
defd1870
UD
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
59ba27a6
PE
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */
defd1870
UD
18
19#include <pthread.h>
20#include <stdio.h>
21#include <stdlib.h>
22#include <time.h>
23#include <unistd.h>
24
25
defd1870
UD
26static pthread_once_t once = PTHREAD_ONCE_INIT;
27
28static pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
29static pthread_mutex_t mut = PTHREAD_MUTEX_INITIALIZER;
30
31static pthread_barrier_t bar;
32
33static int global;
3a4d1e1e 34static int cl_called;
defd1870
UD
35
36static void
37once_handler1 (void)
38{
39 if (pthread_mutex_lock (&mut) != 0)
40 {
41 puts ("once_handler1: mutex_lock failed");
42 exit (1);
43 }
44
45 int r = pthread_barrier_wait (&bar);
46 if (r != 0 && r!= PTHREAD_BARRIER_SERIAL_THREAD)
47 {
48 puts ("once_handler1: barrier_wait failed");
49 exit (1);
50 }
51
52 pthread_cond_wait (&cond, &mut);
53
54 /* We should never get here. */
55}
56
57
58static void
59once_handler2 (void)
60{
61 global = 1;
62}
63
64
3a4d1e1e
UD
65static void
66cl (void *arg)
67{
68 ++cl_called;
69}
70
71
defd1870
UD
72static void *
73tf1 (void *arg)
74{
3a4d1e1e
UD
75 pthread_cleanup_push (cl, NULL);
76
defd1870
UD
77 pthread_once (&once, once_handler1);
78
3a4d1e1e
UD
79 pthread_cleanup_pop (0);
80
defd1870
UD
81 /* We should never get here. */
82 puts ("pthread_once in tf returned");
83 exit (1);
84}
85
86
87static void *
88tf2 (void *arg)
89{
3a4d1e1e
UD
90 pthread_cleanup_push (cl, NULL);
91
defd1870
UD
92 int r = pthread_barrier_wait (&bar);
93 if (r != 0 && r!= PTHREAD_BARRIER_SERIAL_THREAD)
94 {
95 puts ("once_handler2: barrier_wait failed");
96 exit (1);
97 }
98
3a4d1e1e
UD
99 pthread_cleanup_pop (0);
100
defd1870
UD
101 pthread_once (&once, once_handler2);
102
103 return NULL;
104}
105
106
107static int
108do_test (void)
109{
110 pthread_t th[2];
111
112 if (pthread_barrier_init (&bar, NULL, 2) != 0)
113 {
114 puts ("barrier_init failed");
3a4d1e1e 115 return 1;
defd1870
UD
116 }
117
118 if (pthread_create (&th[0], NULL, tf1, NULL) != 0)
119 {
120 puts ("first create failed");
3a4d1e1e 121 return 1;
defd1870
UD
122 }
123
124 int r = pthread_barrier_wait (&bar);
125 if (r != 0 && r!= PTHREAD_BARRIER_SERIAL_THREAD)
126 {
127 puts ("first barrier_wait failed");
3a4d1e1e 128 return 1;
defd1870
UD
129 }
130
131 if (pthread_mutex_lock (&mut) != 0)
132 {
133 puts ("mutex_lock failed");
3a4d1e1e 134 return 1;
defd1870
UD
135 }
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)
139 {
140 puts ("mutex_unlock failed");
3a4d1e1e 141 return 1;
defd1870
UD
142 }
143
144 if (pthread_create (&th[1], NULL, tf2, NULL) != 0)
145 {
146 puts ("second create failed");
3a4d1e1e 147 return 1;
defd1870
UD
148 }
149
150 r = pthread_barrier_wait (&bar);
151 if (r != 0 && r!= PTHREAD_BARRIER_SERIAL_THREAD)
152 {
153 puts ("second barrier_wait failed");
3a4d1e1e 154 return 1;
defd1870
UD
155 }
156
157 /* Give the second thread a chance to reach the pthread_once call. */
158 sleep (2);
159
160 /* Cancel the thread. */
161 if (pthread_cancel (th[0]) != 0)
162 {
163 puts ("cancel failed");
3a4d1e1e 164 return 1;
defd1870
UD
165 }
166
167 void *result;
168 pthread_join (th[0], &result);
169 if (result != PTHREAD_CANCELED)
170 {
171 puts ("first join didn't return PTHREAD_CANCELED");
3a4d1e1e 172 return 1;
defd1870
UD
173 }
174
175 puts ("joined first thread");
176
177 pthread_join (th[1], &result);
178 if (result != NULL)
179 {
180 puts ("second join didn't return PTHREAD_CANCELED");
3a4d1e1e 181 return 1;
defd1870
UD
182 }
183
184 if (global != 1)
185 {
186 puts ("global still 0");
3a4d1e1e
UD
187 return 1;
188 }
189
190 if (cl_called != 1)
191 {
192 printf ("cl_called = %d\n", cl_called);
193 return 1;
defd1870
UD
194 }
195
196 return 0;
197}
198
199#define TEST_FUNCTION do_test ()
200#define TIMEOUT 4
201#include "../test-skeleton.c"