tlist.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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 <string.h>
  39. #include "../include/list.h"
  40. #include "tlist.h"
  41. struct timer {
  42. struct list_head list;
  43. struct timeval tv;
  44. void (*timer_fn)(void *data);
  45. void *data;
  46. timer_handle handle_addr;
  47. };
  48. void timerlist_init (struct timerlist *timerlist)
  49. {
  50. list_init (&timerlist->timer_head);
  51. }
  52. void timerlist_free (struct timerlist *timerlist)
  53. {
  54. }
  55. int timers_inuse = 0;
  56. static inline void timeval_adjust_to_msec (struct timeval *tv) {
  57. tv->tv_usec = (tv->tv_usec / 1000) * 1000;
  58. }
  59. void timerlist_add (struct timerlist *timerlist, struct timer *timer)
  60. {
  61. struct list_head *timer_list = 0;
  62. struct timer *timer_from_list;
  63. int found;
  64. timeval_adjust_to_msec (&timer->tv);
  65. //printf ("Adding timer %d %d\n", timer->tv.tv_sec, timer->tv.tv_usec);
  66. for (found = 0, timer_list = timerlist->timer_head.next;
  67. timer_list != &timerlist->timer_head;
  68. timer_list = timer_list->next) {
  69. timer_from_list = list_entry (timer_list,
  70. struct timer, list);
  71. if ((timer_from_list->tv.tv_sec > timer->tv.tv_sec) ||
  72. ((timer_from_list->tv.tv_sec == timer->tv.tv_sec) &&
  73. (timer_from_list->tv.tv_usec > timer->tv.tv_usec))) {
  74. list_add (&timer->list, timer_list->prev);
  75. found = 1;
  76. break; /* for timer iteration */
  77. }
  78. }
  79. if (found == 0) {
  80. list_add (&timer->list, timerlist->timer_head.prev);
  81. timers_inuse++;
  82. }
  83. }
  84. timer_handle timerlist_add_future (struct timerlist *timerlist,
  85. void (*timer_fn) (void *data),
  86. void *data,
  87. unsigned int msec_in_future,
  88. timer_handle *handle)
  89. {
  90. struct timer *timer;
  91. struct timeval current_time;
  92. unsigned int seconds;
  93. unsigned int mseconds;
  94. timer = (struct timer *)malloc (sizeof (struct timer));
  95. if (timer == 0) {
  96. errno = ENOMEM;
  97. return (0);
  98. }
  99. seconds = msec_in_future / 1000;
  100. mseconds = msec_in_future % 1000;
  101. gettimeofday (&current_time, 0);
  102. timeval_adjust_to_msec (&current_time);
  103. timer->tv.tv_sec = current_time.tv_sec + seconds;
  104. timer->tv.tv_usec = current_time.tv_usec + mseconds * 1000;
  105. if (timer->tv.tv_usec >= 1000000) {
  106. timer->tv.tv_sec++;
  107. timer->tv.tv_usec -= 1000000;
  108. }
  109. timer->data = data;
  110. timer->timer_fn = timer_fn;
  111. timer->handle_addr = handle;
  112. timerlist_add (timerlist, timer);
  113. *handle = timer;
  114. return (0);
  115. }
  116. void timerlist_del (struct timerlist *timerlist, timer_handle timer_handle)
  117. {
  118. struct timer *timer = (struct timer *)timer_handle;
  119. memset (timer->handle_addr, 0, sizeof (struct timer *));
  120. /*
  121. * If the next timer after the currently expiring timer because
  122. * timerlist_del is called from a timer handler, get to the enxt
  123. * timer
  124. */
  125. if (timerlist->timer_iter == &timer->list) {
  126. timerlist->timer_iter = timerlist->timer_iter->next;
  127. }
  128. list_del (&timer->list);
  129. list_init (&timer->list);
  130. timers_inuse--;
  131. free (timer);
  132. }
  133. void timerlist_del_data (struct timerlist *timerlist, timer_handle timer_handle)
  134. {
  135. struct timer *timer = (struct timer *)timer_handle;
  136. if (timer->data) {
  137. free (timer->data);
  138. }
  139. timerlist_del(timerlist,timer_handle);
  140. }
  141. static void timerlist_pre_dispatch (struct timerlist *timerlist, timer_handle timer_handle)
  142. {
  143. struct timer *timer = (struct timer *)timer_handle;
  144. memset (timer->handle_addr, 0, sizeof (struct timer *));
  145. list_del (&timer->list);
  146. list_init (&timer->list);
  147. timers_inuse--;
  148. }
  149. static void timerlist_post_dispatch (struct timerlist *timerlist, timer_handle timer_handle)
  150. {
  151. struct timer *timer = (struct timer *)timer_handle;
  152. free (timer);
  153. }
  154. #ifdef CODE_COVERAGE_COMPILE_OUT
  155. int timer_expire_get_tv (struct timerlist *timerlist, struct timeval *tv)
  156. {
  157. struct timeval current_time;
  158. struct 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 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. unsigned int timerlist_timeout_msec (struct timerlist *timerlist)
  193. {
  194. struct timeval current_time;
  195. struct timer *timer_from_list;
  196. unsigned 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 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. return time_in_msec;
  217. }
  218. void timerlist_expire (struct timerlist *timerlist)
  219. {
  220. struct timer *timer_from_list;
  221. struct timeval current_time;
  222. gettimeofday (&current_time, 0);
  223. timeval_adjust_to_msec (&current_time);
  224. for (timerlist->timer_iter = timerlist->timer_head.next;
  225. timerlist->timer_iter != &timerlist->timer_head;) {
  226. timer_from_list = list_entry (timerlist->timer_iter,
  227. struct timer, list);
  228. if ((timer_from_list->tv.tv_sec < current_time.tv_sec) ||
  229. ((timer_from_list->tv.tv_sec == current_time.tv_sec) &&
  230. (timer_from_list->tv.tv_usec <= current_time.tv_usec))) {
  231. //printf ("Executing timer %d %d\n", timer_from_list->tv.tv_sec, timer_from_list->tv.tv_usec);
  232. timerlist->timer_iter = timerlist->timer_iter->next;
  233. timerlist_pre_dispatch (timerlist, timer_from_list);
  234. timer_from_list->timer_fn (timer_from_list->data);
  235. timerlist_post_dispatch (timerlist, timer_from_list);
  236. } else {
  237. break; /* for timer iteration */
  238. }
  239. }
  240. timerlist->timer_iter = 0;
  241. }