1 /* Copyright (C) 2003-2021 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, see
16 <https://www.gnu.org/licenses/>. */
25 static pthread_once_t once
= PTHREAD_ONCE_INIT
;
27 static pthread_cond_t cond
= PTHREAD_COND_INITIALIZER
;
28 static pthread_mutex_t mut
= PTHREAD_MUTEX_INITIALIZER
;
30 static pthread_barrier_t bar
;
38 if (pthread_mutex_lock (&mut
) != 0)
40 puts ("once_handler1: mutex_lock failed");
44 int r
= pthread_barrier_wait (&bar
);
45 if (r
!= 0 && r
!= PTHREAD_BARRIER_SERIAL_THREAD
)
47 puts ("once_handler1: barrier_wait failed");
51 pthread_cond_wait (&cond
, &mut
);
53 /* We should never get here. */
74 pthread_cleanup_push (cl
, NULL
);
76 pthread_once (&once
, once_handler1
);
78 pthread_cleanup_pop (0);
80 /* We should never get here. */
81 puts ("pthread_once in tf returned");
89 pthread_cleanup_push (cl
, NULL
);
91 int r
= pthread_barrier_wait (&bar
);
92 if (r
!= 0 && r
!= PTHREAD_BARRIER_SERIAL_THREAD
)
94 puts ("once_handler2: barrier_wait failed");
98 pthread_cleanup_pop (0);
100 pthread_once (&once
, once_handler2
);
111 if (pthread_barrier_init (&bar
, NULL
, 2) != 0)
113 puts ("barrier_init failed");
117 if (pthread_create (&th
[0], NULL
, tf1
, NULL
) != 0)
119 puts ("first create failed");
123 int r
= pthread_barrier_wait (&bar
);
124 if (r
!= 0 && r
!= PTHREAD_BARRIER_SERIAL_THREAD
)
126 puts ("first barrier_wait failed");
130 if (pthread_mutex_lock (&mut
) != 0)
132 puts ("mutex_lock failed");
135 /* We unlock the mutex so that we catch the case where the pthread_cond_wait
136 call incorrectly resumes and tries to get the mutex. */
137 if (pthread_mutex_unlock (&mut
) != 0)
139 puts ("mutex_unlock failed");
143 if (pthread_create (&th
[1], NULL
, tf2
, NULL
) != 0)
145 puts ("second create failed");
149 r
= pthread_barrier_wait (&bar
);
150 if (r
!= 0 && r
!= PTHREAD_BARRIER_SERIAL_THREAD
)
152 puts ("second barrier_wait failed");
156 /* Give the second thread a chance to reach the pthread_once call. */
159 /* Cancel the thread. */
160 if (pthread_cancel (th
[0]) != 0)
162 puts ("cancel failed");
167 pthread_join (th
[0], &result
);
168 if (result
!= PTHREAD_CANCELED
)
170 puts ("first join didn't return PTHREAD_CANCELED");
174 puts ("joined first thread");
176 pthread_join (th
[1], &result
);
179 puts ("second join didn't return PTHREAD_CANCELED");
185 puts ("global still 0");
191 printf ("cl_called = %d\n", cl_called
);
198 #define TEST_FUNCTION do_test ()
199 #include "../test-skeleton.c"