1 /* Copyright (C) 2003-2024 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/>. */
28 static pthread_barrier_t b2
;
43 int r
= pthread_barrier_wait (&b2
);
44 if (r
!= 0 && r
!= PTHREAD_BARRIER_SERIAL_THREAD
)
46 puts ("child thread: barrier_wait failed");
50 pthread_cleanup_push (cl
, NULL
);
52 /* This call should never return. */
53 if (lockf (fd
, F_LOCK
, 0))
55 puts ("child thread: lockf failed");
59 pthread_cleanup_pop (0);
68 char fname
[] = "/tmp/cancel16XXXXXX";
72 puts ("mkstemp failed");
77 char mem
[sizeof (pthread_barrier_t
)];
78 memset (mem
, '\0', sizeof (mem
));
79 if (TEMP_FAILURE_RETRY (pwrite (fd
, mem
, sizeof (mem
), 0)) != sizeof (mem
))
81 puts ("pwrite failed");
85 void *p
= mmap (NULL
, sizeof (mem
), PROT_READ
|PROT_WRITE
, MAP_SHARED
, fd
, 0);
91 pthread_barrier_t
*b
= (pthread_barrier_t
*) p
;
93 pthread_barrierattr_t ba
;
94 if (pthread_barrierattr_init (&ba
) != 0)
96 puts ("barrierattr_init failed");
99 if (pthread_barrierattr_setpshared (&ba
, 1) != 0)
101 puts ("barrierattr_setshared failed");
105 if (pthread_barrier_init (b
, &ba
, 2) != 0)
107 puts ("1st barrier_init failed");
110 if (pthread_barrierattr_destroy (&ba
) != 0)
112 puts ("barrier_destroy failed");
119 /* Child. Lock the file and wait. */
120 if (lockf (fd
, F_LOCK
, 0) != 0)
122 puts ("child process: lockf failed");
126 int r
= pthread_barrier_wait (b
);
127 if (r
!= 0 && r
!= PTHREAD_BARRIER_SERIAL_THREAD
)
129 puts ("child process: 1st barrier_wait failed");
133 /* Make sure the process dies. */
136 r
= pthread_barrier_wait (b
);
137 if (r
!= 0 && r
!= PTHREAD_BARRIER_SERIAL_THREAD
)
139 puts ("child process: 2nd barrier_wait failed");
147 puts ("fork failed");
151 int r
= pthread_barrier_wait (b
);
152 if (r
!= 0 && r
!= PTHREAD_BARRIER_SERIAL_THREAD
)
154 puts ("main: 1st barrier_wait failed");
158 if (pthread_barrier_init (&b2
, NULL
, 2) != 0)
160 puts ("2nd barrier_init failed");
165 if (pthread_create (&th
, NULL
, tf
, NULL
) != 0)
167 puts ("create failed");
171 r
= pthread_barrier_wait (&b2
);
172 if (r
!= 0 && r
!= PTHREAD_BARRIER_SERIAL_THREAD
)
174 puts ("main: 2nd barrier_wait failed");
181 if (pthread_cancel (th
) != 0)
183 puts ("cancel failed");
188 if (pthread_join (th
, &result
) != 0)
190 puts ("join failed");
193 if (result
!= PTHREAD_CANCELED
)
195 puts ("thread not canceled");
200 puts ("cleanup handler not called");
204 r
= pthread_barrier_wait (b
);
205 if (r
!= 0 && r
!= PTHREAD_BARRIER_SERIAL_THREAD
)
207 puts ("main: 3rd barrier_wait failed");
212 if (TEMP_FAILURE_RETRY (waitpid (pid
, &status
, 0)) != pid
)
214 puts ("waitpid failed");
217 if (WEXITSTATUS (status
) != 0)
219 printf ("child process exits with %d\n", WEXITSTATUS (status
));
223 if (lockf (fd
, F_LOCK
, 0) != 0)
225 puts ("main: lockf failed");
232 #define TEST_FUNCTION do_test ()
233 #include "../test-skeleton.c"