1 /* Tests for POSIX timer implementation.
2 Copyright (C) 2004-2014 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Jakub Jelinek <jakub@redhat.com>, 2004
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/>. */
30 # define TEST_CLOCK CLOCK_REALTIME
33 pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
34 pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
36 timer_t timer_none, timer_sig1, timer_sig2, timer_thr1, timer_thr2;
38 int thr1_cnt, thr1_err;
39 union sigval thr1_sigval;
40 struct timespec thr1_ts;
43 thr1 (union sigval sigval)
45 pthread_mutex_lock (&lock);
46 thr1_err = clock_gettime (TEST_CLOCK, &thr1_ts);
49 struct itimerspec it = { };
50 thr1_err |= timer_settime (timer_thr1, 0, &it, NULL);
54 pthread_cond_signal (&cond);
55 pthread_mutex_unlock (&lock);
58 int thr2_cnt, thr2_err;
59 union sigval thr2_sigval;
60 size_t thr2_guardsize;
61 struct timespec thr2_ts;
64 thr2 (union sigval sigval)
68 size_t guardsize = -1;
69 int ret = pthread_getattr_np (pthread_self (), &nattr);
73 printf ("*** pthread_getattr_np failed: %m\n");
78 ret = pthread_attr_getguardsize (&nattr, &guardsize);
82 printf ("*** pthread_attr_getguardsize failed: %m\n");
85 if (pthread_attr_destroy (&nattr) != 0)
87 puts ("*** pthread_attr_destroy failed");
91 pthread_mutex_lock (&lock);
92 thr2_err = clock_gettime (TEST_CLOCK, &thr2_ts) | err;
95 struct itimerspec it = { };
96 thr2_err |= timer_settime (timer_thr2, 0, &it, NULL);
100 thr2_guardsize = guardsize;
101 pthread_cond_signal (&cond);
102 pthread_mutex_unlock (&lock);
105 volatile int sig1_cnt, sig1_err;
106 volatile union sigval sig1_sigval;
107 struct timespec sig1_ts;
110 sig1_handler (int sig, siginfo_t *info, void *ctx)
113 if (sig != SIGRTMIN) err |= 1 << 0;
114 if (info->si_signo != SIGRTMIN) err |= 1 << 1;
115 if (info->si_code != SI_TIMER) err |= 1 << 2;
116 if (clock_gettime (TEST_CLOCK, &sig1_ts) != 0)
120 struct itimerspec it = { };
121 if (timer_settime (timer_sig1, 0, &it, NULL))
125 sig1_sigval = info->si_value;
129 volatile int sig2_cnt, sig2_err;
130 volatile union sigval sig2_sigval;
131 struct timespec sig2_ts;
134 sig2_handler (int sig, siginfo_t *info, void *ctx)
137 if (sig != SIGRTMIN + 1) err |= 1 << 0;
138 if (info->si_signo != SIGRTMIN + 1) err |= 1 << 1;
139 if (info->si_code != SI_TIMER) err |= 1 << 2;
140 if (clock_gettime (TEST_CLOCK, &sig2_ts) != 0)
144 struct itimerspec it = { };
145 if (timer_settime (timer_sig2, 0, &it, NULL))
149 sig2_sigval = info->si_value;
153 /* Check if end is later or equal to start + nsec. */
155 check_ts (const char *name, const struct timespec *start,
156 const struct timespec *end, long msec)
158 struct timespec ts = *start;
160 ts.tv_sec += msec / 1000000;
161 ts.tv_nsec += (msec % 1000000) * 1000;
162 if (ts.tv_nsec >= 1000000000)
165 ts.tv_nsec -= 1000000000;
167 if (end->tv_sec < ts.tv_sec
168 || (end->tv_sec == ts.tv_sec && end->tv_nsec < ts.tv_nsec))
171 *** timer %s invoked too soon: %ld.%09ld instead of expected %ld.%09ld\n",
172 name, (long) end->tv_sec, end->tv_nsec,
173 (long) ts.tv_sec, ts.tv_nsec);
181 #define TEST_FUNCTION do_test ()
187 #ifdef TEST_CLOCK_MISSING
188 const char *missing = TEST_CLOCK_MISSING (TEST_CLOCK);
191 printf ("%s missing, skipping test\n", missing);
197 if (clock_gettime (TEST_CLOCK, &ts) != 0)
199 printf ("*** clock_gettime failed: %m\n");
203 printf ("clock_gettime returned timespec = { %ld, %ld }\n",
204 (long) ts.tv_sec, ts.tv_nsec);
206 if (clock_getres (TEST_CLOCK, &ts) != 0)
208 printf ("*** clock_getres failed: %m\n");
212 printf ("clock_getres returned timespec = { %ld, %ld }\n",
213 (long) ts.tv_sec, ts.tv_nsec);
216 memset (&ev, 0x11, sizeof (ev));
217 ev.sigev_notify = SIGEV_NONE;
218 if (timer_create (TEST_CLOCK, &ev, &timer_none) != 0)
220 printf ("*** timer_create for timer_none failed: %m\n");
224 struct sigaction sa = { .sa_sigaction = sig1_handler,
225 .sa_flags = SA_SIGINFO };
226 sigemptyset (&sa.sa_mask);
227 sigaction (SIGRTMIN, &sa, NULL);
228 sa.sa_sigaction = sig2_handler;
229 sigaction (SIGRTMIN + 1, &sa, NULL);
231 memset (&ev, 0x22, sizeof (ev));
232 ev.sigev_notify = SIGEV_SIGNAL;
233 ev.sigev_signo = SIGRTMIN;
234 ev.sigev_value.sival_ptr = &ev;
235 if (timer_create (TEST_CLOCK, &ev, &timer_sig1) != 0)
237 printf ("*** timer_create for timer_sig1 failed: %m\n");
241 memset (&ev, 0x33, sizeof (ev));
242 ev.sigev_notify = SIGEV_SIGNAL;
243 ev.sigev_signo = SIGRTMIN + 1;
244 ev.sigev_value.sival_int = 163;
245 if (timer_create (TEST_CLOCK, &ev, &timer_sig2) != 0)
247 printf ("*** timer_create for timer_sig2 failed: %m\n");
251 memset (&ev, 0x44, sizeof (ev));
252 ev.sigev_notify = SIGEV_THREAD;
253 ev.sigev_notify_function = thr1;
254 ev.sigev_notify_attributes = NULL;
255 ev.sigev_value.sival_ptr = &ev;
256 if (timer_create (TEST_CLOCK, &ev, &timer_thr1) != 0)
258 printf ("*** timer_create for timer_thr1 failed: %m\n");
262 pthread_attr_t nattr;
263 if (pthread_attr_init (&nattr)
264 || pthread_attr_setguardsize (&nattr, 0))
266 puts ("*** pthread_attr_t setup failed");
270 memset (&ev, 0x55, sizeof (ev));
271 ev.sigev_notify = SIGEV_THREAD;
272 ev.sigev_notify_function = thr2;
273 ev.sigev_notify_attributes = &nattr;
274 ev.sigev_value.sival_int = 111;
275 if (timer_create (TEST_CLOCK, &ev, &timer_thr2) != 0)
277 printf ("*** timer_create for timer_thr2 failed: %m\n");
281 int ret = timer_getoverrun (timer_thr1);
285 printf ("*** timer_getoverrun failed: %m\n");
287 printf ("*** timer_getoverrun returned %d != 0\n", ret);
291 struct itimerspec it;
292 it.it_value.tv_sec = 0;
293 it.it_value.tv_nsec = -26;
294 it.it_interval.tv_sec = 0;
295 it.it_interval.tv_nsec = 0;
296 if (timer_settime (timer_sig1, 0, &it, NULL) == 0)
298 puts ("*** timer_settime with negative tv_nsec unexpectedly succeeded");
301 else if (errno != EINVAL)
303 printf ("*** timer_settime with negative tv_nsec did not fail with "
308 it.it_value.tv_nsec = 100000;
309 it.it_interval.tv_nsec = 1000000000;
310 if (timer_settime (timer_sig2, 0, &it, NULL) == 0)
313 *** timer_settime with tv_nsec 1000000000 unexpectedly succeeded");
316 else if (errno != EINVAL)
318 printf ("*** timer_settime with tv_nsec 1000000000 did not fail with "
324 it.it_value.tv_nsec = 0;
325 it.it_interval.tv_nsec = -26;
326 if (timer_settime (timer_thr1, 0, &it, NULL) != 0)
329 !!! timer_settime with it_value 0 it_interval invalid failed: %m\n");
330 /* FIXME: is this mandated by POSIX?
334 it.it_interval.tv_nsec = 3000000000;
335 if (timer_settime (timer_thr2, 0, &it, NULL) != 0)
338 !!! timer_settime with it_value 0 it_interval invalid failed: %m\n");
339 /* FIXME: is this mandated by POSIX?
344 struct timespec startts;
345 if (clock_gettime (TEST_CLOCK, &startts) != 0)
347 printf ("*** clock_gettime failed: %m\n");
351 it.it_value.tv_nsec = 100000000;
352 it.it_interval.tv_nsec = 0;
353 if (timer_settime (timer_none, 0, &it, NULL) != 0)
355 printf ("*** timer_settime timer_none failed: %m\n");
359 it.it_value.tv_nsec = 200000000;
360 if (timer_settime (timer_thr1, 0, &it, NULL) != 0)
362 printf ("*** timer_settime timer_thr1 failed: %m\n");
366 it.it_value.tv_nsec = 300000000;
367 if (timer_settime (timer_thr2, 0, &it, NULL) != 0)
369 printf ("*** timer_settime timer_thr2 failed: %m\n");
373 it.it_value.tv_nsec = 400000000;
374 if (timer_settime (timer_sig1, 0, &it, NULL) != 0)
376 printf ("*** timer_settime timer_sig1 failed: %m\n");
380 it.it_value.tv_nsec = 500000000;
381 if (TEMP_FAILURE_RETRY (timer_settime (timer_sig2, 0, &it, NULL)) != 0)
383 printf ("*** timer_settime timer_sig2 failed: %m\n");
387 pthread_mutex_lock (&lock);
388 while (thr1_cnt == 0 || thr2_cnt == 0)
389 pthread_cond_wait (&cond, &lock);
390 pthread_mutex_unlock (&lock);
392 while (sig1_cnt == 0 || sig2_cnt == 0)
395 ts.tv_nsec = 100000000;
396 nanosleep (&ts, NULL);
399 pthread_mutex_lock (&lock);
403 printf ("*** thr1 not called exactly once, but %d times\n", thr1_cnt);
408 puts ("*** an error occurred in thr1");
411 else if (thr1_sigval.sival_ptr != &ev)
413 printf ("*** thr1_sigval.sival_ptr %p != %p\n",
414 thr1_sigval.sival_ptr, &ev);
417 else if (check_ts ("thr1", &startts, &thr1_ts, 200000))
422 printf ("*** thr2 not called exactly once, but %d times\n", thr2_cnt);
427 puts ("*** an error occurred in thr2");
430 else if (thr2_sigval.sival_int != 111)
432 printf ("*** thr2_sigval.sival_ptr %d != 111\n", thr2_sigval.sival_int);
435 else if (check_ts ("thr2", &startts, &thr2_ts, 300000))
437 else if (thr2_guardsize != 0)
439 printf ("*** thr2 guardsize %zd != 0\n", thr2_guardsize);
443 pthread_mutex_unlock (&lock);
447 printf ("*** sig1 not called exactly once, but %d times\n", sig1_cnt);
452 printf ("*** errors occurred in sig1 handler %x\n", sig1_err);
455 else if (sig1_sigval.sival_ptr != &ev)
457 printf ("*** sig1_sigval.sival_ptr %p != %p\n",
458 sig1_sigval.sival_ptr, &ev);
461 else if (check_ts ("sig1", &startts, &sig1_ts, 400000))
466 printf ("*** sig2 not called exactly once, but %d times\n", sig2_cnt);
471 printf ("*** errors occurred in sig2 handler %x\n", sig2_err);
474 else if (sig2_sigval.sival_int != 163)
476 printf ("*** sig2_sigval.sival_ptr %d != 163\n", sig2_sigval.sival_int);
479 else if (check_ts ("sig2", &startts, &sig2_ts, 500000))
482 if (timer_gettime (timer_none, &it) != 0)
484 printf ("*** timer_gettime timer_none failed: %m\n");
487 else if (it.it_value.tv_sec || it.it_value.tv_nsec
488 || it.it_interval.tv_sec || it.it_interval.tv_nsec)
491 *** timer_gettime timer_none returned { %ld.%09ld, %ld.%09ld }\n",
492 (long) it.it_value.tv_sec, it.it_value.tv_nsec,
493 (long) it.it_interval.tv_sec, it.it_interval.tv_nsec);
497 if (clock_gettime (TEST_CLOCK, &startts) != 0)
499 printf ("*** clock_gettime failed: %m\n");
503 it.it_value.tv_sec = 1;
504 it.it_value.tv_nsec = 0;
505 it.it_interval.tv_sec = 0;
506 it.it_interval.tv_nsec = 100000000;
507 if (timer_settime (timer_none, 0, &it, NULL) != 0)
509 printf ("*** timer_settime timer_none failed: %m\n");
513 it.it_value.tv_nsec = 100000000;
514 it.it_interval.tv_nsec = 200000000;
515 if (timer_settime (timer_thr1, 0, &it, NULL) != 0)
517 printf ("*** timer_settime timer_thr1 failed: %m\n");
521 it.it_value.tv_nsec = 200000000;
522 it.it_interval.tv_nsec = 300000000;
523 if (timer_settime (timer_thr2, 0, &it, NULL) != 0)
525 printf ("*** timer_settime timer_thr2 failed: %m\n");
529 it.it_value.tv_nsec = 300000000;
530 it.it_interval.tv_nsec = 400000000;
531 if (timer_settime (timer_sig1, 0, &it, NULL) != 0)
533 printf ("*** timer_settime timer_sig1 failed: %m\n");
537 it.it_value.tv_nsec = 400000000;
538 it.it_interval.tv_nsec = 500000000;
539 if (TEMP_FAILURE_RETRY (timer_settime (timer_sig2, 0, &it, NULL)) != 0)
541 printf ("*** timer_settime timer_sig2 failed: %m\n");
545 pthread_mutex_lock (&lock);
546 while (thr1_cnt < 6 || thr2_cnt < 6)
547 pthread_cond_wait (&cond, &lock);
548 pthread_mutex_unlock (&lock);
550 while (sig1_cnt < 6 || sig2_cnt < 6)
553 ts.tv_nsec = 100000000;
554 nanosleep (&ts, NULL);
557 pthread_mutex_lock (&lock);
561 puts ("*** an error occurred in thr1");
564 else if (check_ts ("thr1", &startts, &thr1_ts, 1100000 + 4 * 200000))
569 puts ("*** an error occurred in thr2");
572 else if (check_ts ("thr2", &startts, &thr2_ts, 1200000 + 4 * 300000))
574 else if (thr2_guardsize != 0)
576 printf ("*** thr2 guardsize %zd != 0\n", thr2_guardsize);
580 pthread_mutex_unlock (&lock);
584 printf ("*** errors occurred in sig1 handler %x\n", sig1_err);
587 else if (check_ts ("sig1", &startts, &sig1_ts, 1300000 + 4 * 400000))
592 printf ("*** errors occurred in sig2 handler %x\n", sig2_err);
595 else if (check_ts ("sig2", &startts, &sig2_ts, 1400000 + 4 * 500000))
598 if (timer_gettime (timer_none, &it) != 0)
600 printf ("*** timer_gettime timer_none failed: %m\n");
603 else if (it.it_interval.tv_sec || it.it_interval.tv_nsec != 100000000)
606 !!! second timer_gettime timer_none returned it_interval %ld.%09ld\n",
607 (long) it.it_interval.tv_sec, it.it_interval.tv_nsec);
608 /* FIXME: For now disabled.
612 if (timer_delete (timer_none) != 0)
614 printf ("*** timer_delete for timer_none failed: %m\n");
618 if (timer_delete (timer_sig1) != 0)
620 printf ("*** timer_delete for timer_sig1 failed: %m\n");
624 if (timer_delete (timer_sig2) != 0)
626 printf ("*** timer_delete for timer_sig2 failed: %m\n");
630 if (timer_delete (timer_thr1) != 0)
632 printf ("*** timer_delete for timer_thr1 failed: %m\n");
636 if (timer_delete (timer_thr2) != 0)
638 printf ("*** timer_delete for timer_thr2 failed: %m\n");
644 # define TEST_FUNCTION 0
647 #include "../test-skeleton.c"