posix-timer.h 5.3 KB

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