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/>. */
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
;
39 if (pthread_mutex_lock (&mut
) != 0)
41 puts ("once_handler1: mutex_lock failed");
45 int r
= pthread_barrier_wait (&bar
);
46 if (r
!= 0 && r
!= PTHREAD_BARRIER_SERIAL_THREAD
)
48 puts ("once_handler1: barrier_wait failed");
52 pthread_cond_wait (&cond
, &mut
);
54 /* We should never get here. */
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");
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");
99 pthread_cleanup_pop (0);
101 pthread_once (&once
, once_handler2
);
112 if (pthread_barrier_init (&bar
, NULL
, 2) != 0)
114 puts ("barrier_init failed");
118 if (pthread_create (&th
[0], NULL
, tf1
, NULL
) != 0)
120 puts ("first create failed");
124 int r
= pthread_barrier_wait (&bar
);
125 if (r
!= 0 && r
!= PTHREAD_BARRIER_SERIAL_THREAD
)
127 puts ("first barrier_wait failed");
131 if (pthread_mutex_lock (&mut
) != 0)
133 puts ("mutex_lock failed");
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");
144 if (pthread_create (&th
[1], NULL
, tf2
, NULL
) != 0)
146 puts ("second create failed");
150 r
= pthread_barrier_wait (&bar
);
151 if (r
!= 0 && r
!= PTHREAD_BARRIER_SERIAL_THREAD
)
153 puts ("second barrier_wait failed");
157 /* Give the second thread a chance to reach the pthread_once call. */
160 /* Cancel the thread. */
161 if (pthread_cancel (th
[0]) != 0)
163 puts ("cancel failed");
168 pthread_join (th
[0], &result
);
169 if (result
!= PTHREAD_CANCELED
)
171 puts ("first join didn't return PTHREAD_CANCELED");
175 puts ("joined first thread");
177 pthread_join (th
[1], &result
);
180 puts ("second join didn't return PTHREAD_CANCELED");
186 puts ("global still 0");
192 printf ("cl_called = %d\n", cl_called
);
199 #define TEST_FUNCTION do_test ()
201 #include "../test-skeleton.c"