tlist.c 7.9 KB

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