tlist.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. /*
  2. * Copyright (c) 2003-2004 MontaVista Software, Inc.
  3. * Copyright (c) 2006-2007 Red Hat, Inc.
  4. *
  5. * All rights reserved.
  6. *
  7. * Author: Steven Dake (sdake@redhat.com)
  8. *
  9. * This software licensed under BSD license, the text of which follows:
  10. *
  11. * Redistribution and use in source and binary forms, with or without
  12. * modification, are permitted provided that the following conditions are met:
  13. *
  14. * - Redistributions of source code must retain the above copyright notice,
  15. * this list of conditions and the following disclaimer.
  16. * - Redistributions in binary form must reproduce the above copyright notice,
  17. * this list of conditions and the following disclaimer in the documentation
  18. * and/or other materials provided with the distribution.
  19. * - Neither the name of the MontaVista Software, Inc. nor the names of its
  20. * contributors may be used to endorse or promote products derived from this
  21. * software without specific prior written permission.
  22. *
  23. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  24. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  25. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  26. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  27. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  28. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  29. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  30. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  31. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  32. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  33. * THE POSSIBILITY OF SUCH DAMAGE.
  34. */
  35. #ifndef TLIST_H_DEFINED
  36. #define TLIST_H_DEFINED
  37. #include <sys/time.h>
  38. #include <stdio.h>
  39. #include <stdlib.h>
  40. #include <errno.h>
  41. #include <string.h>
  42. #include <sys/param.h>
  43. #include <corosync/list.h>
  44. #ifndef HZ
  45. #define HZ 100 /* 10ms */
  46. #endif
  47. #ifndef TIMER_HANDLE
  48. typedef void * timer_handle;
  49. #define TIMER_HANDLE
  50. #endif
  51. struct timerlist {
  52. struct list_head timer_head;
  53. struct list_head *timer_iter;
  54. };
  55. struct timerlist_timer {
  56. struct list_head list;
  57. unsigned long long nano_from_epoch;
  58. void (*timer_fn)(void *data);
  59. void *data;
  60. timer_handle handle_addr;
  61. };
  62. static inline void timerlist_init (struct timerlist *timerlist)
  63. {
  64. list_init (&timerlist->timer_head);
  65. }
  66. static inline unsigned long long timerlist_nano_from_epoch (void)
  67. {
  68. unsigned long long nano_from_epoch;
  69. struct timeval time_from_epoch;
  70. gettimeofday (&time_from_epoch, 0);
  71. nano_from_epoch = ((time_from_epoch.tv_sec * 1000000000ULL) + (time_from_epoch.tv_usec * 1000ULL));
  72. return (nano_from_epoch);
  73. }
  74. static inline void timerlist_add (struct timerlist *timerlist, struct timerlist_timer *timer)
  75. {
  76. struct list_head *timer_list = 0;
  77. struct timerlist_timer *timer_from_list;
  78. int found;
  79. for (found = 0, timer_list = timerlist->timer_head.next;
  80. timer_list != &timerlist->timer_head;
  81. timer_list = timer_list->next) {
  82. timer_from_list = list_entry (timer_list,
  83. struct timerlist_timer, list);
  84. if (timer_from_list->nano_from_epoch > timer->nano_from_epoch) {
  85. list_add (&timer->list, timer_list->prev);
  86. found = 1;
  87. break; /* for timer iteration */
  88. }
  89. }
  90. if (found == 0) {
  91. list_add (&timer->list, timerlist->timer_head.prev);
  92. }
  93. }
  94. static inline int timerlist_add_absolute (struct timerlist *timerlist,
  95. void (*timer_fn) (void *data),
  96. void *data,
  97. unsigned long long nano_from_epoch,
  98. timer_handle *handle)
  99. {
  100. struct timerlist_timer *timer;
  101. timer = (struct timerlist_timer *)malloc (sizeof (struct timerlist_timer));
  102. if (timer == 0) {
  103. errno = ENOMEM;
  104. return (-1);
  105. }
  106. timer->nano_from_epoch = nano_from_epoch;
  107. timer->data = data;
  108. timer->timer_fn = timer_fn;
  109. timer->handle_addr = handle;
  110. timerlist_add (timerlist, timer);
  111. *handle = timer;
  112. return (0);
  113. }
  114. static inline int timerlist_add_duration (struct timerlist *timerlist,
  115. void (*timer_fn) (void *data),
  116. void *data,
  117. unsigned long long nano_duration,
  118. timer_handle *handle)
  119. {
  120. struct timerlist_timer *timer;
  121. timer = (struct timerlist_timer *)malloc (sizeof (struct timerlist_timer));
  122. if (timer == 0) {
  123. errno = ENOMEM;
  124. return (-1);
  125. }
  126. timer->nano_from_epoch = timerlist_nano_from_epoch() + nano_duration;
  127. timer->data = data;
  128. timer->timer_fn = timer_fn;
  129. timer->handle_addr = handle;
  130. timerlist_add (timerlist, timer);
  131. *handle = timer;
  132. return (0);
  133. }
  134. static inline void timerlist_del (struct timerlist *timerlist, timer_handle timer_handle)
  135. {
  136. struct timerlist_timer *timer = (struct timerlist_timer *)timer_handle;
  137. memset (timer->handle_addr, 0, sizeof (struct timerlist_timer *));
  138. /*
  139. * If the next timer after the currently expiring timer because
  140. * timerlist_del is called from a timer handler, get to the next
  141. * timer
  142. */
  143. if (timerlist->timer_iter == &timer->list) {
  144. timerlist->timer_iter = timerlist->timer_iter->next;
  145. }
  146. list_del (&timer->list);
  147. list_init (&timer->list);
  148. free (timer);
  149. }
  150. static inline unsigned long long timerlist_expire_time (struct timerlist *timerlist, timer_handle timer_handle)
  151. {
  152. struct timerlist_timer *timer = (struct timerlist_timer *)timer_handle;
  153. return (timer->nano_from_epoch);
  154. }
  155. static inline void timerlist_pre_dispatch (struct timerlist *timerlist, timer_handle timer_handle)
  156. {
  157. struct timerlist_timer *timer = (struct timerlist_timer *)timer_handle;
  158. memset (timer->handle_addr, 0, sizeof (struct timerlist_timer *));
  159. list_del (&timer->list);
  160. list_init (&timer->list);
  161. }
  162. static inline void timerlist_post_dispatch (struct timerlist *timerlist, timer_handle timer_handle)
  163. {
  164. struct timerlist_timer *timer = (struct timerlist_timer *)timer_handle;
  165. free (timer);
  166. }
  167. /*
  168. * returns the number of msec until the next timer will expire for use with poll
  169. */
  170. static inline unsigned long long timerlist_msec_duration_to_expire (struct timerlist *timerlist)
  171. {
  172. struct timerlist_timer *timer_from_list;
  173. volatile unsigned long long nano_from_epoch;
  174. volatile unsigned long long msec_duration_to_expire;
  175. /*
  176. * empty list, no expire
  177. */
  178. if (timerlist->timer_head.next == &timerlist->timer_head) {
  179. return (-1);
  180. }
  181. timer_from_list = list_entry (timerlist->timer_head.next,
  182. struct timerlist_timer, list);
  183. nano_from_epoch = timerlist_nano_from_epoch();
  184. /*
  185. * timer at head of list is expired, zero msecs required
  186. */
  187. if (timer_from_list->nano_from_epoch < nano_from_epoch) {
  188. return (0);
  189. }
  190. msec_duration_to_expire = ((timer_from_list->nano_from_epoch - nano_from_epoch) / 1000000ULL) +
  191. (1000 / HZ);
  192. return (msec_duration_to_expire);
  193. }
  194. /*
  195. * Expires any timers that should be expired
  196. */
  197. static inline void timerlist_expire (struct timerlist *timerlist)
  198. {
  199. struct timerlist_timer *timer_from_list;
  200. unsigned long long nano_from_epoch;
  201. nano_from_epoch = timerlist_nano_from_epoch();
  202. for (timerlist->timer_iter = timerlist->timer_head.next;
  203. timerlist->timer_iter != &timerlist->timer_head;) {
  204. timer_from_list = list_entry (timerlist->timer_iter,
  205. struct timerlist_timer, list);
  206. if (timer_from_list->nano_from_epoch < nano_from_epoch) {
  207. timerlist->timer_iter = timerlist->timer_iter->next;
  208. timerlist_pre_dispatch (timerlist, timer_from_list);
  209. timer_from_list->timer_fn (timer_from_list->data);
  210. timerlist_post_dispatch (timerlist, timer_from_list);
  211. } else {
  212. break; /* for timer iteration */
  213. }
  214. }
  215. timerlist->timer_iter = 0;
  216. }
  217. #endif /* TLIST_H_DEFINED */