1 /* Test for bogus per-thread deletion of timers.  */
2 
3 #include <stdio.h>
4 #include <error.h>
5 #include <time.h>
6 #include <signal.h>
7 #include <stdint.h>
8 #include <string.h>
9 #include <sys/time.h>
10 #include <sys/resource.h>
11 #include <unistd.h>
12 #if _POSIX_THREADS
13 # include <pthread.h>
14 
15 
16 /* Creating timers in another thread should work too.  */
17 static void *
do_timer_create(void * arg)18 do_timer_create (void *arg)
19 {
20   struct sigevent *const sigev = arg;
21   timer_t *const timerId = sigev->sigev_value.sival_ptr;
22   if (timer_create (CLOCK_REALTIME, sigev, timerId) < 0)
23     {
24       printf ("timer_create: %m\n");
25       return NULL;
26     }
27   return timerId;
28 }
29 
30 
31 static int
do_test(void)32 do_test (void)
33 {
34   int i, res;
35   timer_t timerId;
36   struct itimerspec itval;
37   struct sigevent sigev;
38 
39   itval.it_interval.tv_sec = 2;
40   itval.it_interval.tv_nsec = 0;
41   itval.it_value.tv_sec = 2;
42   itval.it_value.tv_nsec = 0;
43 
44   sigev.sigev_notify = SIGEV_SIGNAL;
45   sigev.sigev_signo = SIGALRM;
46   sigev.sigev_value.sival_ptr = (void *) &timerId;
47 
48   for (i = 0; i < 100; i++)
49     {
50       printf ("cnt = %d\n", i);
51 
52       pthread_t thr;
53       res = pthread_create (&thr, NULL, &do_timer_create, &sigev);
54       if (res)
55 	{
56 	  printf ("pthread_create: %s\n", strerror (res));
57 	  continue;
58 	}
59       void *val;
60       res = pthread_join (thr, &val);
61       if (res)
62 	{
63 	  printf ("pthread_join: %s\n", strerror (res));
64 	  continue;
65 	}
66       if (val == NULL)
67 	continue;
68 
69       res = timer_settime (timerId, 0, &itval, NULL);
70       if (res < 0)
71 	printf ("timer_settime: %m\n");
72 
73       res = timer_delete (timerId);
74       if (res < 0)
75 	printf ("timer_delete: %m\n");
76     }
77 
78   return 0;
79 }
80 
81 # define TEST_FUNCTION do_test ()
82 #else
83 # define TEST_FUNCTION 0
84 #endif
85 
86 #include "../test-skeleton.c"
87