tlist.h 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. /*
  2. * Copyright (c) 2003-2004 MontaVista Software, Inc.
  3. * Copyright (c) 2006 Red Hat, Inc.
  4. *
  5. * All rights reserved.
  6. *
  7. * Author: Steven Dake (sdake@mvista.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 "../include/list.h"
  43. typedef void * timer_handle;
  44. struct timerlist {
  45. struct list_head timer_head;
  46. struct list_head *timer_iter;
  47. };
  48. struct timerlist_timer {
  49. struct list_head list;
  50. struct timeval tv;
  51. void (*timer_fn)(void *data);
  52. void *data;
  53. timer_handle handle_addr;
  54. };
  55. static inline void timerlist_init (struct timerlist *timerlist)
  56. {
  57. list_init (&timerlist->timer_head);
  58. }
  59. static inline void timeval_adjust_to_msec (struct timeval *tv) {
  60. tv->tv_usec = (tv->tv_usec / 1000) * 1000;
  61. }
  62. static inline void timerlist_add (struct timerlist *timerlist, struct timerlist_timer *timer)
  63. {
  64. struct list_head *timer_list = 0;
  65. struct timerlist_timer *timer_from_list;
  66. int found;
  67. timeval_adjust_to_msec (&timer->tv);
  68. //printf ("Adding timer %d %d\n", timer->tv.tv_sec, timer->tv.tv_usec);
  69. for (found = 0, timer_list = timerlist->timer_head.next;
  70. timer_list != &timerlist->timer_head;
  71. timer_list = timer_list->next) {
  72. timer_from_list = list_entry (timer_list,
  73. struct timerlist_timer, list);
  74. if ((timer_from_list->tv.tv_sec > timer->tv.tv_sec) ||
  75. ((timer_from_list->tv.tv_sec == timer->tv.tv_sec) &&
  76. (timer_from_list->tv.tv_usec > timer->tv.tv_usec))) {
  77. list_add (&timer->list, timer_list->prev);
  78. found = 1;
  79. break; /* for timer iteration */
  80. }
  81. }
  82. if (found == 0) {
  83. list_add (&timer->list, timerlist->timer_head.prev);
  84. }
  85. }
  86. static inline int timerlist_add_future (struct timerlist *timerlist,
  87. void (*timer_fn) (void *data),
  88. void *data,
  89. unsigned int msec_in_future,
  90. timer_handle *handle)
  91. {
  92. struct timerlist_timer *timer;
  93. struct timeval current_time;
  94. unsigned int seconds;
  95. unsigned int mseconds;
  96. timer = (struct timerlist_timer *)malloc (sizeof (struct timerlist_timer));
  97. if (timer == 0) {
  98. errno = ENOMEM;
  99. return (-1);
  100. }
  101. seconds = msec_in_future / 1000;
  102. mseconds = msec_in_future % 1000;
  103. gettimeofday (&current_time, 0);
  104. timeval_adjust_to_msec (&current_time);
  105. timer->tv.tv_sec = current_time.tv_sec + seconds;
  106. timer->tv.tv_usec = current_time.tv_usec + mseconds * 1000;
  107. if (timer->tv.tv_usec >= 1000000) {
  108. timer->tv.tv_sec++;
  109. timer->tv.tv_usec -= 1000000;
  110. }
  111. timer->data = data;
  112. timer->timer_fn = timer_fn;
  113. timer->handle_addr = handle;
  114. timerlist_add (timerlist, timer);
  115. *handle = timer;
  116. return (0);
  117. }
  118. static inline void timerlist_del (struct timerlist *timerlist, timer_handle timer_handle)
  119. {
  120. struct timerlist_timer *timer = (struct timerlist_timer *)timer_handle;
  121. memset (timer->handle_addr, 0, sizeof (struct timerlist_timer *));
  122. /*
  123. * If the next timer after the currently expiring timer because
  124. * timerlist_del is called from a timer handler, get to the enxt
  125. * timer
  126. */
  127. if (timerlist->timer_iter == &timer->list) {
  128. timerlist->timer_iter = timerlist->timer_iter->next;
  129. }
  130. list_del (&timer->list);
  131. list_init (&timer->list);
  132. free (timer);
  133. }
  134. static inline void timerlist_del_data (struct timerlist *timerlist, timer_handle timer_handle)
  135. {
  136. struct timerlist_timer *timer = (struct timerlist_timer *)timer_handle;
  137. if (timer->data) {
  138. free (timer->data);
  139. }
  140. timerlist_del(timerlist,timer_handle);
  141. }
  142. static inline void timerlist_pre_dispatch (struct timerlist *timerlist, timer_handle timer_handle)
  143. {
  144. struct timerlist_timer *timer = (struct timerlist_timer *)timer_handle;
  145. memset (timer->handle_addr, 0, sizeof (struct timerlist_timer *));
  146. list_del (&timer->list);
  147. list_init (&timer->list);
  148. }
  149. static inline void timerlist_post_dispatch (struct timerlist *timerlist, timer_handle timer_handle)
  150. {
  151. struct timerlist_timer *timer = (struct timerlist_timer *)timer_handle;
  152. free (timer);
  153. }
  154. #ifdef CODE_COVERAGE_COMPILE_OUT
  155. static inline int timer_expire_get_tv (struct timerlist *timerlist, struct timeval *tv)
  156. {
  157. struct timeval current_time;
  158. struct timerlist_timer *timer_from_list;
  159. /*
  160. * empty list, no expire
  161. */
  162. if (timerlist->timer_head.next == &timerlist->timer_head) {
  163. return (-1);
  164. }
  165. timer_from_list = list_entry (timerlist->timer_head.next,
  166. struct timerlist_timer, list);
  167. gettimeofday (&current_time, 0);
  168. timeval_adjust_to_msec (&current_time);
  169. /*
  170. * timer at head of list is expired, zero msecs required
  171. */
  172. if ((timer_from_list->tv.tv_sec < current_time.tv_sec) ||
  173. ((timer_from_list->tv.tv_sec == current_time.tv_sec) &&
  174. (timer_from_list->tv.tv_usec <= current_time.tv_usec))) {
  175. tv->tv_sec = 0;
  176. tv->tv_usec = 0;
  177. }
  178. tv->tv_sec = timer_from_list->tv.tv_sec - current_time.tv_sec;
  179. tv->tv_usec = timer_from_list->tv.tv_usec - current_time.tv_usec;
  180. if (tv->tv_usec < 0) {
  181. tv->tv_sec -= 1;
  182. tv->tv_usec += 1000000;
  183. }
  184. if (tv->tv_sec < 0) {
  185. tv->tv_sec = 0;
  186. tv->tv_usec = 0;
  187. }
  188. timeval_adjust_to_msec (tv);
  189. return (0);
  190. }
  191. #endif /* CODE_COVERAGE_COMPILE_OUT */
  192. static inline unsigned int timerlist_timeout_msec (struct timerlist *timerlist)
  193. {
  194. struct timeval current_time;
  195. struct timerlist_timer *timer_from_list;
  196. int time_in_msec;
  197. /*
  198. * empty list, no expire
  199. */
  200. if (timerlist->timer_head.next == &timerlist->timer_head) {
  201. return (-1);
  202. }
  203. timer_from_list = list_entry (timerlist->timer_head.next,
  204. struct timerlist_timer, list);
  205. gettimeofday (&current_time, 0);
  206. timeval_adjust_to_msec (&current_time);
  207. /*
  208. * timer at head of list is expired, zero msecs required
  209. */
  210. if ((timer_from_list->tv.tv_sec < current_time.tv_sec) ||
  211. ((timer_from_list->tv.tv_sec == current_time.tv_sec) &&
  212. (timer_from_list->tv.tv_usec <= current_time.tv_usec))) {
  213. return (0);
  214. }
  215. time_in_msec = ((timer_from_list->tv.tv_sec - current_time.tv_sec) * 1000) + ((timer_from_list->tv.tv_usec - current_time.tv_usec) / 1000);
  216. if (time_in_msec < 0) {
  217. return (0);
  218. }
  219. return time_in_msec;
  220. }
  221. static inline void timerlist_expire (struct timerlist *timerlist)
  222. {
  223. struct timerlist_timer *timer_from_list;
  224. struct timeval current_time;
  225. gettimeofday (&current_time, 0);
  226. timeval_adjust_to_msec (&current_time);
  227. for (timerlist->timer_iter = timerlist->timer_head.next;
  228. timerlist->timer_iter != &timerlist->timer_head;) {
  229. timer_from_list = list_entry (timerlist->timer_iter,
  230. struct timerlist_timer, list);
  231. if ((timer_from_list->tv.tv_sec < current_time.tv_sec) ||
  232. ((timer_from_list->tv.tv_sec == current_time.tv_sec) &&
  233. (timer_from_list->tv.tv_usec <= current_time.tv_usec))) {
  234. //printf ("Executing timer %d %d\n", timer_from_list->tv.tv_sec, timer_from_list->tv.tv_usec);
  235. timerlist->timer_iter = timerlist->timer_iter->next;
  236. timerlist_pre_dispatch (timerlist, timer_from_list);
  237. timer_from_list->timer_fn (timer_from_list->data);
  238. timerlist_post_dispatch (timerlist, timer_from_list);
  239. } else {
  240. break; /* for timer iteration */
  241. }
  242. }
  243. timerlist->timer_iter = 0;
  244. }
  245. #endif /* TLIST_H_DEFINED */