1 /* Check if the thread created by POSIX timer using SIGEV_THREAD is
2    cancellable.
3    Copyright (C) 2020-2022 Free Software Foundation, Inc.
4    This file is part of the GNU C Library.
5 
6    The GNU C Library is free software; you can redistribute it and/or
7    modify it under the terms of the GNU Lesser General Public
8    License as published by the Free Software Foundation; either
9    version 2.1 of the License, or (at your option) any later version.
10 
11    The GNU C Library is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14    Lesser General Public License for more details.
15 
16    You should have received a copy of the GNU Lesser General Public
17    License along with the GNU C Library; if not, see
18    <https://www.gnu.org/licenses/>.  */
19 
20 #include <stdio.h>
21 #include <time.h>
22 #include <signal.h>
23 #include <unistd.h>
24 #include <stdbool.h>
25 
26 #include <support/check.h>
27 #include <support/test-driver.h>
28 #include <support/xthread.h>
29 
30 static pthread_barrier_t barrier;
31 static pthread_t timer_thread;
32 
33 static void
cl(void * arg)34 cl (void *arg)
35 {
36   xpthread_barrier_wait (&barrier);
37 }
38 
39 static void
thread_handler(union sigval sv)40 thread_handler (union sigval sv)
41 {
42   timer_thread = pthread_self ();
43 
44   xpthread_barrier_wait (&barrier);
45 
46   pthread_cleanup_push (cl, NULL);
47   while (1)
48     clock_nanosleep (CLOCK_REALTIME, 0, &(struct timespec) { 1, 0 }, NULL);
49   pthread_cleanup_pop (0);
50 }
51 
52 static int
do_test(void)53 do_test (void)
54 {
55   struct sigevent sev = { 0 };
56   sev.sigev_notify = SIGEV_THREAD;
57   sev.sigev_notify_function = &thread_handler;
58 
59   timer_t timerid;
60   TEST_COMPARE (timer_create (CLOCK_REALTIME, &sev, &timerid), 0);
61 
62   xpthread_barrier_init (&barrier, NULL, 2);
63 
64   struct itimerspec trigger = { 0 };
65   trigger.it_value.tv_nsec = 1000000;
66   TEST_COMPARE (timer_settime (timerid, 0, &trigger, NULL), 0);
67 
68   xpthread_barrier_wait (&barrier);
69 
70   xpthread_cancel (timer_thread);
71 
72   xpthread_barrier_wait (&barrier);
73 
74   return 0;
75 }
76 
77 /* A stall in cancellation is a regression.  */
78 #include <support/test-driver.c>
79