posix-timer.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. /* Definitions for POSIX timer implementation on top of NPTL.
  2. Copyright (C) 2000, 2002, 2003, 2004 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 <limits.h>
  17. #include <signal.h>
  18. /* Double linked list. */
  19. struct list_links
  20. {
  21. struct list_links *next;
  22. struct list_links *prev;
  23. };
  24. /* Forward declaration. */
  25. struct timer_node;
  26. /* Definitions for an internal thread of the POSIX timer implementation. */
  27. struct thread_node
  28. {
  29. struct list_links links;
  30. pthread_attr_t attr;
  31. pthread_t id;
  32. unsigned int exists;
  33. struct list_links timer_queue;
  34. pthread_cond_t cond;
  35. struct timer_node *current_timer;
  36. pthread_t captured;
  37. clockid_t clock_id;
  38. };
  39. /* Internal representation of a timer. */
  40. struct timer_node
  41. {
  42. struct list_links links;
  43. struct sigevent event;
  44. clockid_t clock;
  45. struct itimerspec value;
  46. struct timespec expirytime;
  47. pthread_attr_t attr;
  48. unsigned int abstime;
  49. unsigned int armed;
  50. enum {
  51. TIMER_FREE, TIMER_INUSE, TIMER_DELETED
  52. } inuse;
  53. struct thread_node *thread;
  54. pid_t creator_pid;
  55. int refcount;
  56. int overrun_count;
  57. };
  58. /* The limit is not published if we are compiled with kernel timer support.
  59. But we still compiled in this implementation with its limit unless built
  60. to require the kernel support. */
  61. #ifndef TIMER_MAX
  62. # define TIMER_MAX 256
  63. #endif
  64. /* Static array with the structures for all the timers. */
  65. extern struct timer_node __timer_array[TIMER_MAX];
  66. /* Global lock to protect operation on the lists. */
  67. extern pthread_mutex_t __timer_mutex;
  68. /* Variable to protext initialization. */
  69. extern pthread_once_t __timer_init_once_control;
  70. /* Nonzero if initialization of timer implementation failed. */
  71. extern int __timer_init_failed;
  72. /* Node for the thread used to deliver signals. */
  73. extern struct thread_node __timer_signal_thread_rclk;
  74. /* Return pointer to timer structure corresponding to ID. */
  75. #define timer_id2ptr(timerid) ((struct timer_node *) timerid)
  76. #define timer_ptr2id(timerid) ((void *) timerid)
  77. /* Check whether timer is valid; global mutex must be held. */
  78. static inline int
  79. timer_valid (struct timer_node *timer)
  80. {
  81. return timer && timer->inuse == TIMER_INUSE;
  82. }
  83. /* Timer refcount functions; need global mutex. */
  84. extern void __timer_dealloc (struct timer_node *timer);
  85. static inline void
  86. timer_addref (struct timer_node *timer)
  87. {
  88. timer->refcount++;
  89. }
  90. static inline void
  91. timer_delref (struct timer_node *timer)
  92. {
  93. if (--timer->refcount == 0)
  94. __timer_dealloc (timer);
  95. }
  96. /* Timespec helper routines. */
  97. static inline int
  98. __attribute ((always_inline))
  99. timespec_compare (const struct timespec *left, const struct timespec *right)
  100. {
  101. if (left->tv_sec < right->tv_sec)
  102. return -1;
  103. if (left->tv_sec > right->tv_sec)
  104. return 1;
  105. if (left->tv_nsec < right->tv_nsec)
  106. return -1;
  107. if (left->tv_nsec > right->tv_nsec)
  108. return 1;
  109. return 0;
  110. }
  111. static inline void
  112. timespec_add (struct timespec *sum, const struct timespec *left,
  113. const struct timespec *right)
  114. {
  115. sum->tv_sec = left->tv_sec + right->tv_sec;
  116. sum->tv_nsec = left->tv_nsec + right->tv_nsec;
  117. if (sum->tv_nsec >= 1000000000)
  118. {
  119. ++sum->tv_sec;
  120. sum->tv_nsec -= 1000000000;
  121. }
  122. }
  123. static inline void
  124. timespec_sub (struct timespec *diff, const struct timespec *left,
  125. const struct timespec *right)
  126. {
  127. diff->tv_sec = left->tv_sec - right->tv_sec;
  128. diff->tv_nsec = left->tv_nsec - right->tv_nsec;
  129. if (diff->tv_nsec < 0)
  130. {
  131. --diff->tv_sec;
  132. diff->tv_nsec += 1000000000;
  133. }
  134. }
  135. /* We need one of the list functions in the other modules. */
  136. static inline void
  137. list_unlink_ip (struct list_links *list)
  138. {
  139. struct list_links *lnext = list->next, *lprev = list->prev;
  140. lnext->prev = lprev;
  141. lprev->next = lnext;
  142. /* The suffix ip means idempotent; list_unlink_ip can be called
  143. * two or more times on the same node.
  144. */
  145. list->next = list;
  146. list->prev = list;
  147. }
  148. /* Functions in the helper file. */
  149. extern void __timer_mutex_cancel_handler (void *arg);
  150. extern void __timer_init_once (void);
  151. extern struct timer_node *__timer_alloc (void);
  152. extern int __timer_thread_start (struct thread_node *thread);
  153. extern struct thread_node *__timer_thread_find_matching (const pthread_attr_t *desired_attr, clockid_t);
  154. extern struct thread_node *__timer_thread_alloc (const pthread_attr_t *desired_attr, clockid_t);
  155. extern void __timer_thread_dealloc (struct thread_node *thread);
  156. extern int __timer_thread_queue_timer (struct thread_node *thread,
  157. struct timer_node *insert);
  158. extern void __timer_thread_wakeup (struct thread_node *thread);