2.9
[glibc/nacl-glibc.git] / nptl / sysdeps / pthread / tst-timer.c
blobf19fe364bac7924754f6fbb49b8cfa7023b5ce57
1 /* Tests for POSIX timer implementation.
2 Copyright (C) 2000, 2002, 2003 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 not,
18 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
21 #include <errno.h>
22 #include <signal.h>
23 #include <stdio.h>
24 #include <time.h>
25 #include <unistd.h>
28 static void
29 notify_func1 (union sigval sigval)
31 puts ("notify_func1");
35 static void
36 notify_func2 (union sigval sigval)
38 puts ("notify_func2");
42 static void
43 signal_func (int sig)
45 static const char text[] = "signal_func\n";
46 signal (sig, signal_func);
47 write (STDOUT_FILENO, text, sizeof text - 1);
50 static void
51 intr_sleep (int sec)
53 struct timespec ts;
55 ts.tv_sec = sec;
56 ts.tv_nsec = 0;
58 while (nanosleep (&ts, &ts) == -1 && errno == EINTR)
62 #define ZSIGALRM 14
65 int
66 main (void)
68 struct timespec ts;
69 timer_t timer_sig, timer_thr1, timer_thr2;
70 int retval;
71 struct sigevent sigev1 =
73 .sigev_notify = SIGEV_SIGNAL,
74 .sigev_signo = ZSIGALRM
76 struct sigevent sigev2;
77 struct itimerspec itimer1 = { { 0, 200000000 }, { 0, 200000000 } };
78 struct itimerspec itimer2 = { { 0, 100000000 }, { 0, 500000000 } };
79 struct itimerspec itimer3 = { { 0, 150000000 }, { 0, 300000000 } };
80 struct itimerspec old;
82 retval = clock_gettime (CLOCK_REALTIME, &ts);
84 sigev2.sigev_notify = SIGEV_THREAD;
85 sigev2.sigev_notify_function = notify_func1;
86 sigev2.sigev_notify_attributes = NULL;
87 /* It is unnecessary to do the following but to set a good example
88 we do it anyhow. */
89 sigev2.sigev_value.sival_ptr = NULL;
91 setvbuf (stdout, 0, _IOLBF, 0);
93 printf ("clock_gettime returned %d, timespec = { %ld, %ld }\n",
94 retval, ts.tv_sec, ts.tv_nsec);
96 retval = clock_getres (CLOCK_REALTIME, &ts);
98 printf ("clock_getres returned %d, timespec = { %ld, %ld }\n",
99 retval, ts.tv_sec, ts.tv_nsec);
101 if (timer_create (CLOCK_REALTIME, &sigev1, &timer_sig) != 0)
103 printf ("timer_create for timer_sig failed: %m\n");
104 exit (1);
106 if (timer_create (CLOCK_REALTIME, &sigev2, &timer_thr1) != 0)
108 printf ("timer_create for timer_thr1 failed: %m\n");
109 exit (1);
111 sigev2.sigev_notify_function = notify_func2;
112 if (timer_create (CLOCK_REALTIME, &sigev2, &timer_thr2) != 0)
114 printf ("timer_create for timer_thr2 failed: %m\n");
115 exit (1);
118 if (timer_settime (timer_thr1, 0, &itimer2, &old) != 0)
120 printf ("timer_settime for timer_thr1 failed: %m\n");
121 exit (1);
123 if (timer_settime (timer_thr2, 0, &itimer3, &old) != 0)
125 printf ("timer_settime for timer_thr2 failed: %m\n");
126 exit (1);
129 signal (ZSIGALRM, signal_func);
131 if (timer_settime (timer_sig, 0, &itimer1, &old) != 0)
133 printf ("timer_settime for timer_sig failed: %m\n");
134 exit (1);
137 intr_sleep (3);
139 if (timer_delete (timer_sig) != 0)
141 printf ("timer_delete for timer_sig failed: %m\n");
142 exit (1);
144 if (timer_delete (timer_thr1) != 0)
146 printf ("timer_delete for timer_thr1 failed: %m\n");
147 exit (1);
150 intr_sleep (3);
152 if (timer_delete (timer_thr2) != 0)
154 printf ("timer_delete for timer_thr2 failed: %m\n");
155 exit (1);
158 return 0;