tlist.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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. };
  46. void timerlist_init (struct timerlist *timerlist)
  47. {
  48. list_init (&timerlist->timer_head);
  49. }
  50. void timerlist_free (struct timerlist *timerlist)
  51. {
  52. }
  53. int timers_inuse = 0;
  54. static inline void timeval_adjust_to_msec (struct timeval *tv) {
  55. tv->tv_usec = (tv->tv_usec / 1000) * 1000;
  56. }
  57. void timerlist_add (struct timerlist *timerlist, struct timer *timer)
  58. {
  59. struct list_head *timer_list = 0;
  60. struct timer *timer_from_list;
  61. int found;
  62. timeval_adjust_to_msec (&timer->tv);
  63. for (found = 0, timer_list = timerlist->timer_head.next;
  64. timer_list != &timerlist->timer_head;
  65. timer_list = timer_list->next) {
  66. timer_from_list = list_entry (timer_list,
  67. struct timer, list);
  68. if ((timer_from_list->tv.tv_sec > timer->tv.tv_sec) ||
  69. ((timer_from_list->tv.tv_sec == timer->tv.tv_sec) &&
  70. (timer_from_list->tv.tv_usec > timer->tv.tv_usec))) {
  71. list_add (&timer->list, timer_list->prev);
  72. found = 1;
  73. break; /* for timer iteration */
  74. }
  75. }
  76. if (found == 0) {
  77. list_add (&timer->list, timerlist->timer_head.prev);
  78. timers_inuse++;
  79. }
  80. }
  81. timer_handle timerlist_add_future (struct timerlist *timerlist,
  82. void (*timer_fn) (void *data),
  83. void *data,
  84. int msec_in_future)
  85. {
  86. struct timer *timer;
  87. struct timeval current_time;
  88. int seconds;
  89. int mseconds;
  90. timer = (struct timer *)malloc (sizeof (struct timer));
  91. if (timer == 0) {
  92. errno = ENOMEM;
  93. return (0);
  94. }
  95. seconds = msec_in_future / 1000;
  96. mseconds = msec_in_future % 1000;
  97. gettimeofday (&current_time, 0);
  98. timeval_adjust_to_msec (&current_time);
  99. timer->tv.tv_sec = current_time.tv_sec + seconds;
  100. timer->tv.tv_usec = current_time.tv_usec + mseconds * 1000;
  101. if (timer->tv.tv_usec >= 1000000) {
  102. timer->tv.tv_sec++;
  103. timer->tv.tv_usec -= 1000000;
  104. }
  105. timer->data = data;
  106. timer->timer_fn = timer_fn;
  107. timerlist_add (timerlist, timer);
  108. return (timer);
  109. }
  110. void timerlist_del (struct timerlist *timerlist, timer_handle timer_handle)
  111. {
  112. struct timer *timer = (struct timer *)timer_handle;
  113. list_del (&timer->list);
  114. free (timer);
  115. timers_inuse--;
  116. }
  117. #ifdef CODE_COVERAGE_COMPILE_OUT
  118. int timer_expire_get_tv (struct timerlist *timerlist, struct timeval *tv)
  119. {
  120. struct timeval current_time;
  121. struct timer *timer_from_list;
  122. /*
  123. * empty list, no expire
  124. */
  125. if (timerlist->timer_head.next == &timerlist->timer_head) {
  126. return (-1);
  127. }
  128. timer_from_list = list_entry (timerlist->timer_head.next,
  129. struct timer, list);
  130. gettimeofday (&current_time, 0);
  131. timeval_adjust_to_msec (&current_time);
  132. /*
  133. * timer at head of list is expired, zero msecs required
  134. */
  135. if ((timer_from_list->tv.tv_sec < current_time.tv_sec) ||
  136. ((timer_from_list->tv.tv_sec == current_time.tv_sec) &&
  137. (timer_from_list->tv.tv_usec <= current_time.tv_usec))) {
  138. tv->tv_sec = 0;
  139. tv->tv_usec = 0;
  140. }
  141. tv->tv_sec = timer_from_list->tv.tv_sec - current_time.tv_sec;
  142. tv->tv_usec = timer_from_list->tv.tv_usec - current_time.tv_usec;
  143. if (tv->tv_usec < 0) {
  144. tv->tv_sec -= 1;
  145. tv->tv_usec += 1000000;
  146. }
  147. if (tv->tv_sec < 0) {
  148. tv->tv_sec = 0;
  149. tv->tv_usec = 0;
  150. }
  151. timeval_adjust_to_msec (tv);
  152. return (0);
  153. }
  154. #endif /* CODE_COVERAGE_COMPILE_OUT */
  155. int timerlist_timeout_msec (struct timerlist *timerlist)
  156. {
  157. struct timeval current_time;
  158. struct timer *timer_from_list;
  159. int time_in_msec;
  160. /*
  161. * empty list, no expire
  162. */
  163. if (timerlist->timer_head.next == &timerlist->timer_head) {
  164. return (-1);
  165. }
  166. timer_from_list = list_entry (timerlist->timer_head.next,
  167. struct timer, list);
  168. gettimeofday (&current_time, 0);
  169. timeval_adjust_to_msec (&current_time);
  170. /*
  171. * timer at head of list is expired, zero msecs required
  172. */
  173. if ((timer_from_list->tv.tv_sec < current_time.tv_sec) ||
  174. ((timer_from_list->tv.tv_sec == current_time.tv_sec) &&
  175. (timer_from_list->tv.tv_usec <= current_time.tv_usec))) {
  176. return (0);
  177. }
  178. 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);
  179. return time_in_msec;
  180. }
  181. void timerlist_expire (struct timerlist *timerlist)
  182. {
  183. struct list_head *timer_list;
  184. struct timer *timer_from_list;
  185. struct timeval current_time;
  186. gettimeofday (&current_time, 0);
  187. timeval_adjust_to_msec (&current_time);
  188. for (timer_list = timerlist->timer_head.next;
  189. timer_list != &timerlist->timer_head;) {
  190. timer_from_list = list_entry (timer_list,
  191. struct timer, list);
  192. if ((timer_from_list->tv.tv_sec < current_time.tv_sec) ||
  193. ((timer_from_list->tv.tv_sec == current_time.tv_sec) &&
  194. (timer_from_list->tv.tv_usec <= current_time.tv_usec))) {
  195. timer_list = timer_list->next;
  196. list_del (&timer_from_list->list);
  197. /*
  198. * This list_init is here to allow multiple deletes
  199. * of a timer without corrupting memory
  200. */
  201. list_init (&timer_from_list->list);
  202. #ifdef DEBUG_MULTIPLE_DELETE
  203. timer_from_list->list.next = (struct list_head *)0xdeadbeef;
  204. timer_from_list->list.prev = (struct list_head *)0xdeadbeef;
  205. #endif
  206. timer_from_list->timer_fn (timer_from_list->data);
  207. } else {
  208. break; /* for timer iteration */
  209. }
  210. }
  211. }