1 /* Copyright (C) 2002-2016 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, see
17 <http://www.gnu.org/licenses/>. */
27 static pthread_once_t once
= PTHREAD_ONCE_INIT
;
29 static pthread_cond_t cond
= PTHREAD_COND_INITIALIZER
;
30 static pthread_mutex_t mut
= PTHREAD_MUTEX_INITIALIZER
;
32 static pthread_barrier_t bar
;
40 if (pthread_mutex_lock (&mut
) != 0)
42 puts ("once_handler1: mutex_lock failed");
45 puts ("once_handler1: locked");
47 int r
= pthread_barrier_wait (&bar
);
48 if (r
!= 0 && r
!= PTHREAD_BARRIER_SERIAL_THREAD
)
50 puts ("once_handler1: barrier_wait failed");
54 puts ("once_handler1: going to wait on cond");
56 pthread_cond_wait (&cond
, &mut
);
58 /* We should never get here. */
79 pthread_cleanup_push (cl
, NULL
)
81 pthread_once (&once
, once_handler1
);
83 pthread_cleanup_pop (0);
85 /* We should never get here. */
86 puts ("pthread_once in tf returned");
96 if (pthread_barrier_init (&bar
, NULL
, 2) != 0)
98 puts ("barrier_init failed");
102 if (pthread_create (&th
, NULL
, tf
, NULL
) != 0)
104 puts ("first create failed");
108 int r
= pthread_barrier_wait (&bar
);
109 if (r
!= 0 && r
!= PTHREAD_BARRIER_SERIAL_THREAD
)
111 puts ("barrier_wait failed");
115 if (pthread_mutex_lock (&mut
) != 0)
117 puts ("mutex_lock failed");
120 /* We unlock the mutex so that we catch the case where the pthread_cond_wait
121 call incorrectly resumes and tries to get the mutex. */
122 if (pthread_mutex_unlock (&mut
) != 0)
124 puts ("mutex_unlock failed");
128 /* Cancel the thread. */
129 puts ("going to cancel");
130 if (pthread_cancel (th
) != 0)
132 puts ("cancel failed");
137 pthread_join (th
, &result
);
138 if (result
!= PTHREAD_CANCELED
)
140 puts ("join didn't return PTHREAD_CANCELED");
143 puts ("joined successfully");
145 printf ("once = %d\n", *(int *) &once
);
149 puts ("cleanup handler not called");
153 pthread_once (&once
, once_handler2
);
157 puts ("global still 0");
164 #define TEST_FUNCTION do_test ()
166 #include "../test-skeleton.c"