Adjust name of ld.so in test-container.c.
[glibc.git] / sysdeps / pthread / tst-timer.c
blob7a943afb7cf254901d3de71999f269f405a4d89f
1 /* Tests for POSIX timer implementation.
2 Copyright (C) 2000-2018 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Kaz Kylheku <kaz@ashi.footprints.net>.
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 License as
8 published by the Free Software Foundation; either version 2.1 of the
9 License, or (at your option) any later version.
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.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; see the file COPYING.LIB. If
18 not, see <http://www.gnu.org/licenses/>. */
20 #include <errno.h>
21 #include <signal.h>
22 #include <stdio.h>
23 #include <time.h>
24 #include <unistd.h>
25 #include <stdlib.h>
26 #include <stdint.h>
29 static void
30 notify_func1 (union sigval sigval)
32 puts ("notify_func1");
36 static void
37 notify_func2 (union sigval sigval)
39 puts ("notify_func2");
43 static void
44 signal_func (int sig)
46 static const char text[] = "signal_func\n";
47 signal (sig, signal_func);
48 write (STDOUT_FILENO, text, sizeof text - 1);
51 static void
52 intr_sleep (int sec)
54 struct timespec ts;
56 ts.tv_sec = sec;
57 ts.tv_nsec = 0;
59 while (nanosleep (&ts, &ts) == -1 && errno == EINTR)
63 #define ZSIGALRM 14
66 int
67 main (void)
69 struct timespec ts;
70 timer_t timer_sig, timer_thr1, timer_thr2;
71 int retval;
72 struct sigevent sigev1 =
74 .sigev_notify = SIGEV_SIGNAL,
75 .sigev_signo = ZSIGALRM
77 struct sigevent sigev2;
78 struct itimerspec itimer1 = { { 0, 200000000 }, { 0, 200000000 } };
79 struct itimerspec itimer2 = { { 0, 100000000 }, { 0, 500000000 } };
80 struct itimerspec itimer3 = { { 0, 150000000 }, { 0, 300000000 } };
81 struct itimerspec old;
83 retval = clock_gettime (CLOCK_REALTIME, &ts);
85 sigev2.sigev_notify = SIGEV_THREAD;
86 sigev2.sigev_notify_function = notify_func1;
87 sigev2.sigev_notify_attributes = NULL;
88 /* It is unnecessary to do the following but to set a good example
89 we do it anyhow. */
90 sigev2.sigev_value.sival_ptr = NULL;
92 setvbuf (stdout, 0, _IOLBF, 0);
94 printf ("clock_gettime returned %d, timespec = { %jd, %jd }\n",
95 retval, (intmax_t) ts.tv_sec, (intmax_t) ts.tv_nsec);
97 retval = clock_getres (CLOCK_REALTIME, &ts);
99 printf ("clock_getres returned %d, timespec = { %jd, %jd }\n",
100 retval, (intmax_t) ts.tv_sec, (intmax_t) ts.tv_nsec);
102 if (timer_create (CLOCK_REALTIME, &sigev1, &timer_sig) != 0)
104 printf ("timer_create for timer_sig failed: %m\n");
105 exit (1);
107 if (timer_create (CLOCK_REALTIME, &sigev2, &timer_thr1) != 0)
109 printf ("timer_create for timer_thr1 failed: %m\n");
110 exit (1);
112 sigev2.sigev_notify_function = notify_func2;
113 if (timer_create (CLOCK_REALTIME, &sigev2, &timer_thr2) != 0)
115 printf ("timer_create for timer_thr2 failed: %m\n");
116 exit (1);
119 if (timer_settime (timer_thr1, 0, &itimer2, &old) != 0)
121 printf ("timer_settime for timer_thr1 failed: %m\n");
122 exit (1);
124 if (timer_settime (timer_thr2, 0, &itimer3, &old) != 0)
126 printf ("timer_settime for timer_thr2 failed: %m\n");
127 exit (1);
130 signal (ZSIGALRM, signal_func);
132 if (timer_settime (timer_sig, 0, &itimer1, &old) != 0)
134 printf ("timer_settime for timer_sig failed: %m\n");
135 exit (1);
138 intr_sleep (3);
140 if (timer_delete (timer_sig) != 0)
142 printf ("timer_delete for timer_sig failed: %m\n");
143 exit (1);
145 if (timer_delete (timer_thr1) != 0)
147 printf ("timer_delete for timer_thr1 failed: %m\n");
148 exit (1);
151 intr_sleep (3);
153 if (timer_delete (timer_thr2) != 0)
155 printf ("timer_delete for timer_thr2 failed: %m\n");
156 exit (1);
159 return 0;