]>
Commit | Line | Data |
---|---|---|
76a50749 | 1 | /* Tests for POSIX timer implementation. |
04277e02 | 2 | Copyright (C) 2000-2019 Free Software Foundation, Inc. |
76a50749 UD |
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 | |
59ba27a6 | 17 | License along with the GNU C Library; see the file COPYING.LIB. If |
5a82c748 | 18 | not, see <https://www.gnu.org/licenses/>. */ |
76a50749 UD |
19 | |
20 | #include <errno.h> | |
21 | #include <signal.h> | |
22 | #include <stdio.h> | |
23 | #include <time.h> | |
24 | #include <unistd.h> | |
ffaa74cf | 25 | #include <stdlib.h> |
ceaa9889 | 26 | #include <stdint.h> |
76a50749 UD |
27 | |
28 | ||
29 | static void | |
5e826ab5 | 30 | notify_func1 (union sigval sigval) |
76a50749 | 31 | { |
5e826ab5 UD |
32 | puts ("notify_func1"); |
33 | } | |
34 | ||
35 | ||
36 | static void | |
37 | notify_func2 (union sigval sigval) | |
38 | { | |
39 | puts ("notify_func2"); | |
76a50749 UD |
40 | } |
41 | ||
42 | ||
43 | static void | |
44 | signal_func (int sig) | |
45 | { | |
46 | static const char text[] = "signal_func\n"; | |
47 | signal (sig, signal_func); | |
48 | write (STDOUT_FILENO, text, sizeof text - 1); | |
49 | } | |
50 | ||
51 | static void | |
52 | intr_sleep (int sec) | |
53 | { | |
54 | struct timespec ts; | |
55 | ||
56 | ts.tv_sec = sec; | |
57 | ts.tv_nsec = 0; | |
58 | ||
59 | while (nanosleep (&ts, &ts) == -1 && errno == EINTR) | |
60 | ; | |
61 | } | |
62 | ||
63 | #define ZSIGALRM 14 | |
64 | ||
65 | ||
66 | int | |
67 | main (void) | |
68 | { | |
69 | struct timespec ts; | |
70 | timer_t timer_sig, timer_thr1, timer_thr2; | |
71 | int retval; | |
72 | struct sigevent sigev1 = | |
73 | { | |
74 | .sigev_notify = SIGEV_SIGNAL, | |
75 | .sigev_signo = ZSIGALRM | |
76 | }; | |
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; | |
82 | ||
83 | retval = clock_gettime (CLOCK_REALTIME, &ts); | |
84 | ||
85 | sigev2.sigev_notify = SIGEV_THREAD; | |
5e826ab5 | 86 | sigev2.sigev_notify_function = notify_func1; |
76a50749 | 87 | sigev2.sigev_notify_attributes = NULL; |
27176677 UD |
88 | /* It is unnecessary to do the following but to set a good example |
89 | we do it anyhow. */ | |
c28422b5 | 90 | sigev2.sigev_value.sival_ptr = NULL; |
76a50749 UD |
91 | |
92 | setvbuf (stdout, 0, _IOLBF, 0); | |
93 | ||
37f46900 L |
94 | printf ("clock_gettime returned %d, timespec = { %jd, %jd }\n", |
95 | retval, (intmax_t) ts.tv_sec, (intmax_t) ts.tv_nsec); | |
76a50749 UD |
96 | |
97 | retval = clock_getres (CLOCK_REALTIME, &ts); | |
98 | ||
37f46900 L |
99 | printf ("clock_getres returned %d, timespec = { %jd, %jd }\n", |
100 | retval, (intmax_t) ts.tv_sec, (intmax_t) ts.tv_nsec); | |
76a50749 | 101 | |
5e826ab5 UD |
102 | if (timer_create (CLOCK_REALTIME, &sigev1, &timer_sig) != 0) |
103 | { | |
104 | printf ("timer_create for timer_sig failed: %m\n"); | |
105 | exit (1); | |
106 | } | |
107 | if (timer_create (CLOCK_REALTIME, &sigev2, &timer_thr1) != 0) | |
108 | { | |
109 | printf ("timer_create for timer_thr1 failed: %m\n"); | |
110 | exit (1); | |
111 | } | |
112 | sigev2.sigev_notify_function = notify_func2; | |
113 | if (timer_create (CLOCK_REALTIME, &sigev2, &timer_thr2) != 0) | |
114 | { | |
115 | printf ("timer_create for timer_thr2 failed: %m\n"); | |
116 | exit (1); | |
117 | } | |
118 | ||
119 | if (timer_settime (timer_thr1, 0, &itimer2, &old) != 0) | |
120 | { | |
121 | printf ("timer_settime for timer_thr1 failed: %m\n"); | |
122 | exit (1); | |
123 | } | |
124 | if (timer_settime (timer_thr2, 0, &itimer3, &old) != 0) | |
125 | { | |
126 | printf ("timer_settime for timer_thr2 failed: %m\n"); | |
127 | exit (1); | |
128 | } | |
76a50749 UD |
129 | |
130 | signal (ZSIGALRM, signal_func); | |
131 | ||
5e826ab5 UD |
132 | if (timer_settime (timer_sig, 0, &itimer1, &old) != 0) |
133 | { | |
134 | printf ("timer_settime for timer_sig failed: %m\n"); | |
135 | exit (1); | |
136 | } | |
76a50749 UD |
137 | |
138 | intr_sleep (3); | |
139 | ||
5e826ab5 UD |
140 | if (timer_delete (timer_sig) != 0) |
141 | { | |
142 | printf ("timer_delete for timer_sig failed: %m\n"); | |
143 | exit (1); | |
144 | } | |
145 | if (timer_delete (timer_thr1) != 0) | |
146 | { | |
147 | printf ("timer_delete for timer_thr1 failed: %m\n"); | |
148 | exit (1); | |
149 | } | |
76a50749 UD |
150 | |
151 | intr_sleep (3); | |
152 | ||
5e826ab5 UD |
153 | if (timer_delete (timer_thr2) != 0) |
154 | { | |
155 | printf ("timer_delete for timer_thr2 failed: %m\n"); | |
156 | exit (1); | |
157 | } | |
76a50749 UD |
158 | |
159 | return 0; | |
160 | } |