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