tlist.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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. list_del (&timer->list);
  120. timers_inuse--;
  121. free (timer);
  122. }
  123. static void timerlist_pre_dispatch (struct timerlist *timerlist, timer_handle timer_handle)
  124. {
  125. struct timer *timer = (struct timer *)timer_handle;
  126. *(unsigned int *)timer->handle_addr = 0;
  127. list_del (&timer->list);
  128. timers_inuse--;
  129. }
  130. static void timerlist_post_dispatch (struct timerlist *timerlist, timer_handle timer_handle)
  131. {
  132. struct timer *timer = (struct timer *)timer_handle;
  133. free (timer);
  134. }
  135. #ifdef CODE_COVERAGE_COMPILE_OUT
  136. int timer_expire_get_tv (struct timerlist *timerlist, struct timeval *tv)
  137. {
  138. struct timeval current_time;
  139. struct timer *timer_from_list;
  140. /*
  141. * empty list, no expire
  142. */
  143. if (timerlist->timer_head.next == &timerlist->timer_head) {
  144. return (-1);
  145. }
  146. timer_from_list = list_entry (timerlist->timer_head.next,
  147. struct timer, list);
  148. gettimeofday (&current_time, 0);
  149. timeval_adjust_to_msec (&current_time);
  150. /*
  151. * timer at head of list is expired, zero msecs required
  152. */
  153. if ((timer_from_list->tv.tv_sec < current_time.tv_sec) ||
  154. ((timer_from_list->tv.tv_sec == current_time.tv_sec) &&
  155. (timer_from_list->tv.tv_usec <= current_time.tv_usec))) {
  156. tv->tv_sec = 0;
  157. tv->tv_usec = 0;
  158. }
  159. tv->tv_sec = timer_from_list->tv.tv_sec - current_time.tv_sec;
  160. tv->tv_usec = timer_from_list->tv.tv_usec - current_time.tv_usec;
  161. if (tv->tv_usec < 0) {
  162. tv->tv_sec -= 1;
  163. tv->tv_usec += 1000000;
  164. }
  165. if (tv->tv_sec < 0) {
  166. tv->tv_sec = 0;
  167. tv->tv_usec = 0;
  168. }
  169. timeval_adjust_to_msec (tv);
  170. return (0);
  171. }
  172. #endif /* CODE_COVERAGE_COMPILE_OUT */
  173. unsigned int timerlist_timeout_msec (struct timerlist *timerlist)
  174. {
  175. struct timeval current_time;
  176. struct timer *timer_from_list;
  177. unsigned int time_in_msec;
  178. /*
  179. * empty list, no expire
  180. */
  181. if (timerlist->timer_head.next == &timerlist->timer_head) {
  182. return (-1);
  183. }
  184. timer_from_list = list_entry (timerlist->timer_head.next,
  185. struct timer, list);
  186. gettimeofday (&current_time, 0);
  187. timeval_adjust_to_msec (&current_time);
  188. /*
  189. * timer at head of list is expired, zero msecs required
  190. */
  191. if ((timer_from_list->tv.tv_sec < current_time.tv_sec) ||
  192. ((timer_from_list->tv.tv_sec == current_time.tv_sec) &&
  193. (timer_from_list->tv.tv_usec <= current_time.tv_usec))) {
  194. return (0);
  195. }
  196. 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);
  197. return time_in_msec;
  198. }
  199. void timerlist_expire (struct timerlist *timerlist)
  200. {
  201. struct list_head *timer_list;
  202. struct timer *timer_from_list;
  203. struct timeval current_time;
  204. gettimeofday (&current_time, 0);
  205. timeval_adjust_to_msec (&current_time);
  206. for (timer_list = timerlist->timer_head.next;
  207. timer_list != &timerlist->timer_head;) {
  208. timer_from_list = list_entry (timer_list,
  209. struct timer, list);
  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. //printf ("Executing timer %d %d\n", timer_from_list->tv.tv_sec, timer_from_list->tv.tv_usec);
  214. timer_list = timer_list->next;
  215. timerlist_pre_dispatch (timerlist, timer_from_list);
  216. timer_from_list->timer_fn (timer_from_list->data);
  217. timerlist_post_dispatch (timerlist, timer_from_list);
  218. } else {
  219. break; /* for timer iteration */
  220. }
  221. }
  222. }