blob: 592c0864f16ac67d6c34b14f4a36e172b3d7dc77 [file] [log] [blame]
lh9ed821d2023-04-07 01:36:19 -07001/* 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.
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
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
18 02111-1307 USA. */
19
20#include <errno.h>
21#include <pthread.h>
22#include <semaphore.h>
23#include <stdio.h>
24#include <stdlib.h>
25#include <string.h>
26#include <unistd.h>
27#include <sys/time.h>
28
29
30static pthread_barrier_t bar;
31static sem_t sem;
32
33
34static void
35cleanup (void *arg)
36{
37 static int ncall;
38
39 if (++ncall != 1)
40 {
41 puts ("second call to cleanup");
42 exit (1);
43 }
44
45 printf ("cleanup call #%d\n", ncall);
46}
47
48
49static void *
50tf (void *arg)
51{
52 pthread_cleanup_push (cleanup, NULL);
53
54 int e = pthread_barrier_wait (&bar);
55 if (e != 0 && e != PTHREAD_BARRIER_SERIAL_THREAD)
56 {
57 puts ("tf: 1st barrier_wait failed");
58 exit (1);
59 }
60
61 struct timeval tv;
62 (void) gettimeofday (&tv, NULL);
63
64 struct timespec ts;
65 TIMEVAL_TO_TIMESPEC (&tv, &ts);
66
67 /* Timeout in 5 seconds. */
68 ts.tv_sec += 5;
69
70 /* This call should block and be cancelable. */
71 sem_timedwait (&sem, &ts);
72
73 pthread_cleanup_pop (0);
74
75 puts ("sem_timedwait returned");
76
77 return NULL;
78}
79
80
81static int
82do_test (void)
83{
84 pthread_t th;
85
86 if (pthread_barrier_init (&bar, NULL, 2) != 0)
87 {
88 puts ("barrier_init failed");
89 exit (1);
90 }
91
92 if (sem_init (&sem, 0, 1) != 0)
93 {
94 puts ("sem_init failed");
95 exit (1);
96 }
97
98 if (pthread_create (&th, NULL, tf, NULL) != 0)
99 {
100 puts ("create failed");
101 exit (1);
102 }
103
104 /* Check whether cancellation is honored even before sem_timedwait does
105 anything. */
106 if (pthread_cancel (th) != 0)
107 {
108 puts ("1st cancel failed");
109 exit (1);
110 }
111
112 int e = pthread_barrier_wait (&bar);
113 if (e != 0 && e != PTHREAD_BARRIER_SERIAL_THREAD)
114 {
115 puts ("1st barrier_wait failed");
116 exit (1);
117 }
118
119 void *r;
120 if (pthread_join (th, &r) != 0)
121 {
122 puts ("join failed");
123 exit (1);
124 }
125
126 if (r != PTHREAD_CANCELED)
127 {
128 puts ("thread not canceled");
129 exit (1);
130 }
131
132 return 0;
133}
134
135
136#define TEST_FUNCTION do_test ()
137#include "../test-skeleton.c"