tlist.h 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. /*
  2. * Copyright (c) 2003-2004 MontaVista Software, Inc.
  3. * Copyright (c) 2006-2007, 2009 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 <unistd.h>
  44. #include <corosync/list.h>
  45. #ifndef HZ
  46. #define HZ 100 /* 10ms */
  47. #endif
  48. #ifndef TIMER_HANDLE
  49. typedef void * timer_handle;
  50. #define TIMER_HANDLE
  51. #endif
  52. #define TIMERLIST_MS_IN_SEC 1000ULL
  53. #define TIMERLIST_US_IN_SEC 1000000ULL
  54. #define TIMERLIST_NS_IN_SEC 1000000000ULL
  55. #define TIMERLIST_US_IN_MSEC 1000ULL
  56. #define TIMERLIST_NS_IN_MSEC 1000000ULL
  57. #define TIMERLIST_NS_IN_USEC 1000ULL
  58. struct timerlist {
  59. struct list_head timer_head;
  60. struct list_head *timer_iter;
  61. };
  62. struct timerlist_timer {
  63. struct list_head list;
  64. unsigned long long expire_time;
  65. int is_absolute_timer;
  66. void (*timer_fn)(void *data);
  67. void *data;
  68. timer_handle handle_addr;
  69. };
  70. static inline void timerlist_init (struct timerlist *timerlist)
  71. {
  72. list_init (&timerlist->timer_head);
  73. }
  74. static inline unsigned long long timerlist_nano_from_epoch (void)
  75. {
  76. unsigned long long nano_from_epoch;
  77. struct timeval time_from_epoch;
  78. gettimeofday (&time_from_epoch, 0);
  79. nano_from_epoch = ((time_from_epoch.tv_sec * TIMERLIST_NS_IN_SEC) +
  80. (time_from_epoch.tv_usec * TIMERLIST_NS_IN_USEC));
  81. return (nano_from_epoch);
  82. }
  83. #if defined _POSIX_MONOTONIC_CLOCK && _POSIX_MONOTONIC_CLOCK >= 0
  84. static inline unsigned long long timerlist_nano_current_get (void)
  85. {
  86. unsigned long long nano_monotonic;
  87. struct timespec ts;
  88. clock_gettime (CLOCK_MONOTONIC, &ts);
  89. nano_monotonic = (ts.tv_sec * TIMERLIST_NS_IN_SEC) + (unsigned long long )ts.tv_nsec;
  90. return (nano_monotonic);
  91. }
  92. static inline unsigned long long timerlist_nano_monotonic_hz (void) {
  93. unsigned long long nano_monotonic_hz;
  94. struct timespec ts;
  95. clock_getres (CLOCK_MONOTONIC, &ts);
  96. nano_monotonic_hz = TIMERLIST_NS_IN_SEC / ((ts.tv_sec * TIMERLIST_NS_IN_SEC) + ts.tv_nsec);
  97. return (nano_monotonic_hz);
  98. }
  99. #else
  100. #warning "Your system doesn't support monotonic timer. gettimeofday will be used"
  101. static inline unsigned long long timerlist_nano_current_get (void)
  102. {
  103. return (timerlist_nano_from_epoch ());
  104. }
  105. static inline unsigned long long timerlist_nano_monotonic_hz (void) {
  106. return HZ;
  107. }
  108. #endif
  109. static inline void timerlist_add (struct timerlist *timerlist, struct timerlist_timer *timer)
  110. {
  111. struct list_head *timer_list = 0;
  112. struct timerlist_timer *timer_from_list;
  113. int found;
  114. for (found = 0, timer_list = timerlist->timer_head.next;
  115. timer_list != &timerlist->timer_head;
  116. timer_list = timer_list->next) {
  117. timer_from_list = list_entry (timer_list,
  118. struct timerlist_timer, list);
  119. if (timer_from_list->expire_time > timer->expire_time) {
  120. list_add (&timer->list, timer_list->prev);
  121. found = 1;
  122. break; /* for timer iteration */
  123. }
  124. }
  125. if (found == 0) {
  126. list_add (&timer->list, timerlist->timer_head.prev);
  127. }
  128. }
  129. static inline int timerlist_add_absolute (struct timerlist *timerlist,
  130. void (*timer_fn) (void *data),
  131. void *data,
  132. unsigned long long nano_from_epoch,
  133. timer_handle *handle)
  134. {
  135. struct timerlist_timer *timer;
  136. timer = (struct timerlist_timer *)malloc (sizeof (struct timerlist_timer));
  137. if (timer == 0) {
  138. errno = ENOMEM;
  139. return (-1);
  140. }
  141. timer->expire_time = nano_from_epoch;
  142. timer->is_absolute_timer = 1;
  143. timer->data = data;
  144. timer->timer_fn = timer_fn;
  145. timer->handle_addr = handle;
  146. timerlist_add (timerlist, timer);
  147. *handle = timer;
  148. return (0);
  149. }
  150. static inline int timerlist_add_duration (struct timerlist *timerlist,
  151. void (*timer_fn) (void *data),
  152. void *data,
  153. unsigned long long nano_duration,
  154. timer_handle *handle)
  155. {
  156. struct timerlist_timer *timer;
  157. timer = (struct timerlist_timer *)malloc (sizeof (struct timerlist_timer));
  158. if (timer == 0) {
  159. errno = ENOMEM;
  160. return (-1);
  161. }
  162. timer->expire_time = timerlist_nano_current_get () + nano_duration;
  163. timer->is_absolute_timer = 0;
  164. timer->data = data;
  165. timer->timer_fn = timer_fn;
  166. timer->handle_addr = handle;
  167. timerlist_add (timerlist, timer);
  168. *handle = timer;
  169. return (0);
  170. }
  171. static inline void timerlist_del (struct timerlist *timerlist,
  172. timer_handle _timer_handle)
  173. {
  174. struct timerlist_timer *timer = (struct timerlist_timer *)_timer_handle;
  175. memset (timer->handle_addr, 0, sizeof (struct timerlist_timer *));
  176. /*
  177. * If the next timer after the currently expiring timer because
  178. * timerlist_del is called from a timer handler, get to the next
  179. * timer
  180. */
  181. if (timerlist->timer_iter == &timer->list) {
  182. timerlist->timer_iter = timerlist->timer_iter->next;
  183. }
  184. list_del (&timer->list);
  185. list_init (&timer->list);
  186. free (timer);
  187. }
  188. static inline unsigned long long timerlist_expire_time (struct timerlist *timerlist, timer_handle _timer_handle)
  189. {
  190. struct timerlist_timer *timer = (struct timerlist_timer *)_timer_handle;
  191. return (timer->expire_time);
  192. }
  193. static inline void timerlist_pre_dispatch (struct timerlist *timerlist, timer_handle _timer_handle)
  194. {
  195. struct timerlist_timer *timer = (struct timerlist_timer *)_timer_handle;
  196. memset (timer->handle_addr, 0, sizeof (struct timerlist_timer *));
  197. list_del (&timer->list);
  198. list_init (&timer->list);
  199. }
  200. static inline void timerlist_post_dispatch (struct timerlist *timerlist, timer_handle _timer_handle)
  201. {
  202. struct timerlist_timer *timer = (struct timerlist_timer *)_timer_handle;
  203. free (timer);
  204. }
  205. /**
  206. * returns the number of msec until the next timer will expire for use with poll
  207. */
  208. static inline unsigned long long timerlist_msec_duration_to_expire (struct timerlist *timerlist)
  209. {
  210. struct timerlist_timer *timer_from_list;
  211. volatile unsigned long long current_time;
  212. volatile unsigned long long msec_duration_to_expire;
  213. /*
  214. * empty list, no expire
  215. */
  216. if (timerlist->timer_head.next == &timerlist->timer_head) {
  217. return (-1);
  218. }
  219. timer_from_list = list_entry (timerlist->timer_head.next,
  220. struct timerlist_timer, list);
  221. if (timer_from_list->is_absolute_timer) {
  222. current_time = timerlist_nano_from_epoch ();
  223. } else {
  224. current_time = timerlist_nano_current_get ();
  225. }
  226. /*
  227. * timer at head of list is expired, zero msecs required
  228. */
  229. if (timer_from_list->expire_time < current_time) {
  230. return (0);
  231. }
  232. msec_duration_to_expire = ((timer_from_list->expire_time - current_time) / TIMERLIST_NS_IN_MSEC) +
  233. (1000 / HZ);
  234. return (msec_duration_to_expire);
  235. }
  236. /**
  237. * Expires any timers that should be expired
  238. */
  239. static inline void timerlist_expire (struct timerlist *timerlist)
  240. {
  241. struct timerlist_timer *timer_from_list;
  242. unsigned long long current_time_from_epoch;
  243. unsigned long long current_monotonic_time;
  244. unsigned long long current_time;
  245. current_monotonic_time = timerlist_nano_current_get ();
  246. current_time_from_epoch = current_time = timerlist_nano_from_epoch ();
  247. for (timerlist->timer_iter = timerlist->timer_head.next;
  248. timerlist->timer_iter != &timerlist->timer_head;) {
  249. timer_from_list = list_entry (timerlist->timer_iter,
  250. struct timerlist_timer, list);
  251. current_time = (timer_from_list->is_absolute_timer ? current_time_from_epoch : current_monotonic_time);
  252. if (timer_from_list->expire_time < current_time) {
  253. timerlist->timer_iter = timerlist->timer_iter->next;
  254. timerlist_pre_dispatch (timerlist, timer_from_list);
  255. timer_from_list->timer_fn (timer_from_list->data);
  256. timerlist_post_dispatch (timerlist, timer_from_list);
  257. } else {
  258. break; /* for timer iteration */
  259. }
  260. }
  261. timerlist->timer_iter = 0;
  262. }
  263. #endif /* TLIST_H_DEFINED */