1 /* Copyright (C) 2003 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, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
28 static pthread_barrier_t b
;
31 /* Cleanup handling test. */
44 int r
= pthread_barrier_wait (&b
);
45 if (r
!= 0 && r
!= PTHREAD_BARRIER_SERIAL_THREAD
)
47 puts ("barrier_wait failed");
51 pthread_cleanup_push (cl
, NULL
);
53 struct timespec ts
= { .tv_sec
= arg
== NULL
? 10000000 : 0, .tv_nsec
= 0 };
54 TEMP_FAILURE_RETRY (clock_nanosleep (CLOCK_REALTIME
, 0, &ts
, &ts
));
56 pthread_cleanup_pop (0);
58 puts ("clock_nanosleep returned");
67 if (pthread_barrier_init (&b
, NULL
, 2) != 0)
69 puts ("barrier_init failed");
74 if (pthread_create (&th
, NULL
, tf
, NULL
) != 0)
76 puts ("1st create failed");
80 int r
= pthread_barrier_wait (&b
);
81 if (r
!= 0 && r
!= PTHREAD_BARRIER_SERIAL_THREAD
)
83 puts ("barrier_wait failed");
87 struct timespec ts
= { .tv_sec
= 0, .tv_nsec
= 100000000 };
88 while (nanosleep (&ts
, &ts
) != 0)
91 puts ("going to cancel in-time");
92 if (pthread_cancel (th
) != 0)
94 puts ("1st cancel failed");
99 if (pthread_join (th
, &status
) != 0)
101 puts ("1st join failed");
104 if (status
!= PTHREAD_CANCELED
)
106 puts ("1st thread not canceled");
112 puts ("cleanup handler not called");
117 puts ("cleanup handler called more than once");
121 puts ("in-time cancellation succeeded");
126 if (pthread_create (&th
, NULL
, tf
, NULL
) != 0)
128 puts ("2nd create failed");
132 puts ("going to cancel early");
133 if (pthread_cancel (th
) != 0)
135 puts ("2nd cancel failed");
139 r
= pthread_barrier_wait (&b
);
140 if (r
!= 0 && r
!= PTHREAD_BARRIER_SERIAL_THREAD
)
142 puts ("barrier_wait failed");
146 if (pthread_join (th
, &status
) != 0)
148 puts ("2nd join failed");
151 if (status
!= PTHREAD_CANCELED
)
153 puts ("2nd thread not canceled");
159 printf ("cleanup handler not called\n");
164 printf ("cleanup handler called more than once\n");
168 puts ("early cancellation succeeded");
173 #define TEST_FUNCTION do_test ()
174 #include "../test-skeleton.c"