tlist.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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. int timer_expire_get_tv (struct timerlist *timerlist, struct timeval *tv)
  118. {
  119. struct timeval current_time;
  120. struct timer *timer_from_list;
  121. /*
  122. * empty list, no expire
  123. */
  124. if (timerlist->timer_head.next == &timerlist->timer_head) {
  125. return (-1);
  126. }
  127. timer_from_list = list_entry (timerlist->timer_head.next,
  128. struct timer, list);
  129. gettimeofday (&current_time, 0);
  130. timeval_adjust_to_msec (&current_time);
  131. /*
  132. * timer at head of list is expired, zero msecs required
  133. */
  134. if ((timer_from_list->tv.tv_sec < current_time.tv_sec) ||
  135. ((timer_from_list->tv.tv_sec == current_time.tv_sec) &&
  136. (timer_from_list->tv.tv_usec <= current_time.tv_usec))) {
  137. tv->tv_sec = 0;
  138. tv->tv_usec = 0;
  139. }
  140. tv->tv_sec = timer_from_list->tv.tv_sec - current_time.tv_sec;
  141. tv->tv_usec = timer_from_list->tv.tv_usec - current_time.tv_usec;
  142. if (tv->tv_usec < 0) {
  143. tv->tv_sec -= 1;
  144. tv->tv_usec += 1000000;
  145. }
  146. if (tv->tv_sec < 0) {
  147. tv->tv_sec = 0;
  148. tv->tv_usec = 0;
  149. }
  150. timeval_adjust_to_msec (tv);
  151. return (0);
  152. }
  153. int timerlist_timeout_msec (struct timerlist *timerlist)
  154. {
  155. struct timeval current_time;
  156. struct timer *timer_from_list;
  157. int time_in_msec;
  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. return (0);
  175. }
  176. 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);
  177. return time_in_msec;
  178. }
  179. void timerlist_expire (struct timerlist *timerlist)
  180. {
  181. struct list_head *timer_list;
  182. struct timer *timer_from_list;
  183. struct timeval current_time;
  184. gettimeofday (&current_time, 0);
  185. timeval_adjust_to_msec (&current_time);
  186. for (timer_list = timerlist->timer_head.next;
  187. timer_list != &timerlist->timer_head;) {
  188. timer_from_list = list_entry (timer_list,
  189. struct timer, list);
  190. if ((timer_from_list->tv.tv_sec < current_time.tv_sec) ||
  191. ((timer_from_list->tv.tv_sec == current_time.tv_sec) &&
  192. (timer_from_list->tv.tv_usec <= current_time.tv_usec))) {
  193. timer_list = timer_list->next;
  194. list_del (&timer_from_list->list);
  195. /*
  196. * This list_init is here to allow multiple deletes
  197. * of a timer without corrupting memory
  198. */
  199. list_init (&timer_from_list->list);
  200. #ifdef DEBUG_MULTIPLE_DELETE
  201. timer_from_list->list.next = (struct list_head *)0xdeadbeef;
  202. timer_from_list->list.prev = (struct list_head *)0xdeadbeef;
  203. #endif
  204. timer_from_list->timer_fn (timer_from_list->data);
  205. } else {
  206. break; /* for timer iteration */
  207. }
  208. }
  209. }