tst-timer.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /* Tests for POSIX timer implementation.
  2. Copyright (C) 2000-2019 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. Contributed by Kaz Kylheku <kaz@ashi.footprints.net>.
  5. The GNU C Library is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU Lesser General Public License as
  7. published by the Free Software Foundation; either version 2.1 of the
  8. License, or (at your option) any later version.
  9. The GNU C Library is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. Lesser General Public License for more details.
  13. You should have received a copy of the GNU Lesser General Public
  14. License along with the GNU C Library; see the file COPYING.LIB. If
  15. not, see <http://www.gnu.org/licenses/>. */
  16. #include <errno.h>
  17. #include <signal.h>
  18. #include <stdio.h>
  19. #include <time.h>
  20. #include <unistd.h>
  21. #include <stdlib.h>
  22. #include <stdint.h>
  23. static void
  24. notify_func1 (union sigval sigval)
  25. {
  26. puts ("notify_func1");
  27. }
  28. static void
  29. notify_func2 (union sigval sigval)
  30. {
  31. puts ("notify_func2");
  32. }
  33. static void
  34. signal_func (int sig)
  35. {
  36. static const char text[] = "signal_func\n";
  37. signal (sig, signal_func);
  38. write (STDOUT_FILENO, text, sizeof text - 1);
  39. }
  40. static void
  41. intr_sleep (int sec)
  42. {
  43. struct timespec ts;
  44. ts.tv_sec = sec;
  45. ts.tv_nsec = 0;
  46. while (nanosleep (&ts, &ts) == -1 && errno == EINTR)
  47. ;
  48. }
  49. #define ZSIGALRM 14
  50. int
  51. main (void)
  52. {
  53. struct timespec ts;
  54. timer_t timer_sig, timer_thr1, timer_thr2;
  55. int retval;
  56. struct sigevent sigev1 =
  57. {
  58. .sigev_notify = SIGEV_SIGNAL,
  59. .sigev_signo = ZSIGALRM
  60. };
  61. struct sigevent sigev2;
  62. struct itimerspec itimer1 = { { 0, 200000000 }, { 0, 200000000 } };
  63. struct itimerspec itimer2 = { { 0, 100000000 }, { 0, 500000000 } };
  64. struct itimerspec itimer3 = { { 0, 150000000 }, { 0, 300000000 } };
  65. struct itimerspec old;
  66. retval = clock_gettime (CLOCK_REALTIME, &ts);
  67. sigev2.sigev_notify = SIGEV_THREAD;
  68. sigev2.sigev_notify_function = notify_func1;
  69. sigev2.sigev_notify_attributes = NULL;
  70. /* It is unnecessary to do the following but to set a good example
  71. we do it anyhow. */
  72. sigev2.sigev_value.sival_ptr = NULL;
  73. setvbuf (stdout, 0, _IOLBF, 0);
  74. printf ("clock_gettime returned %d, timespec = { %jd, %jd }\n",
  75. retval, (intmax_t) ts.tv_sec, (intmax_t) ts.tv_nsec);
  76. retval = clock_getres (CLOCK_REALTIME, &ts);
  77. printf ("clock_getres returned %d, timespec = { %jd, %jd }\n",
  78. retval, (intmax_t) ts.tv_sec, (intmax_t) ts.tv_nsec);
  79. if (timer_create (CLOCK_REALTIME, &sigev1, &timer_sig) != 0)
  80. {
  81. printf ("timer_create for timer_sig failed: %m\n");
  82. exit (1);
  83. }
  84. if (timer_create (CLOCK_REALTIME, &sigev2, &timer_thr1) != 0)
  85. {
  86. printf ("timer_create for timer_thr1 failed: %m\n");
  87. exit (1);
  88. }
  89. sigev2.sigev_notify_function = notify_func2;
  90. if (timer_create (CLOCK_REALTIME, &sigev2, &timer_thr2) != 0)
  91. {
  92. printf ("timer_create for timer_thr2 failed: %m\n");
  93. exit (1);
  94. }
  95. if (timer_settime (timer_thr1, 0, &itimer2, &old) != 0)
  96. {
  97. printf ("timer_settime for timer_thr1 failed: %m\n");
  98. exit (1);
  99. }
  100. if (timer_settime (timer_thr2, 0, &itimer3, &old) != 0)
  101. {
  102. printf ("timer_settime for timer_thr2 failed: %m\n");
  103. exit (1);
  104. }
  105. signal (ZSIGALRM, signal_func);
  106. if (timer_settime (timer_sig, 0, &itimer1, &old) != 0)
  107. {
  108. printf ("timer_settime for timer_sig failed: %m\n");
  109. exit (1);
  110. }
  111. intr_sleep (3);
  112. if (timer_delete (timer_sig) != 0)
  113. {
  114. printf ("timer_delete for timer_sig failed: %m\n");
  115. exit (1);
  116. }
  117. if (timer_delete (timer_thr1) != 0)
  118. {
  119. printf ("timer_delete for timer_thr1 failed: %m\n");
  120. exit (1);
  121. }
  122. intr_sleep (3);
  123. if (timer_delete (timer_thr2) != 0)
  124. {
  125. printf ("timer_delete for timer_thr2 failed: %m\n");
  126. exit (1);
  127. }
  128. return 0;
  129. }