blob: 7417bcd5f02292febb2ac151efbc62db7ad1acbb [file] [log] [blame]
yuezonghe824eb0c2024-06-27 02:32:26 -07001/* Tests for POSIX timer implementation.
2 Copyright (C) 2000, 2002 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Kaz Kylheku <kaz@ashi.footprints.net>.
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 License as
8 published by the Free Software Foundation; either version 2.1 of the
9 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; 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. */
20
21#include <errno.h>
22#include <signal.h>
23#include <stdio.h>
24#include <time.h>
25#include <unistd.h>
26
27
28static void
29notify_func (union sigval sigval)
30{
31 puts ("notify_func");
32}
33
34
35static void
36signal_func (int sig)
37{
38 static const char text[] = "signal_func\n";
39 signal (sig, signal_func);
40 write (STDOUT_FILENO, text, sizeof text - 1);
41}
42
43static void
44intr_sleep (int sec)
45{
46 struct timespec ts;
47
48 ts.tv_sec = sec;
49 ts.tv_nsec = 0;
50
51 while (nanosleep (&ts, &ts) == -1 && errno == EINTR)
52 ;
53}
54
55#define ZSIGALRM 14
56
57
58int
59main (void)
60{
61 struct timespec ts;
62 timer_t timer_sig, timer_thr1, timer_thr2;
63 int retval;
64 struct sigevent sigev1 =
65 {
66 .sigev_notify = SIGEV_SIGNAL,
67 .sigev_signo = ZSIGALRM
68 };
69 struct sigevent sigev2;
70 struct itimerspec itimer1 = { { 0, 200000000 }, { 0, 200000000 } };
71 struct itimerspec itimer2 = { { 0, 100000000 }, { 0, 500000000 } };
72 struct itimerspec itimer3 = { { 0, 150000000 }, { 0, 300000000 } };
73 struct itimerspec old;
74
75 retval = clock_gettime (CLOCK_REALTIME, &ts);
76
77 sigev2.sigev_notify = SIGEV_THREAD;
78 sigev2.sigev_notify_function = notify_func;
79 sigev2.sigev_notify_attributes = NULL;
80
81 setvbuf (stdout, 0, _IOLBF, 0);
82
83 printf ("clock_gettime returned %d, timespec = { %ld, %ld }\n",
84 retval, ts.tv_sec, ts.tv_nsec);
85
86 retval = clock_getres (CLOCK_REALTIME, &ts);
87
88 printf ("clock_getres returned %d, timespec = { %ld, %ld }\n",
89 retval, ts.tv_sec, ts.tv_nsec);
90
91 timer_create (CLOCK_REALTIME, &sigev1, &timer_sig);
92 timer_create (CLOCK_REALTIME, &sigev2, &timer_thr1);
93 timer_create (CLOCK_REALTIME, &sigev2, &timer_thr2);
94
95 timer_settime (timer_thr1, 0, &itimer2, &old);
96 timer_settime (timer_thr2, 0, &itimer3, &old);
97
98 signal (ZSIGALRM, signal_func);
99
100 timer_settime (timer_sig, 0, &itimer1, &old);
101
102 timer_delete (-1);
103
104 intr_sleep (3);
105
106 timer_delete (timer_sig);
107 timer_delete (timer_thr1);
108
109 intr_sleep (3);
110
111 timer_delete (timer_thr2);
112
113 return 0;
114}