timer-list.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. /*
  2. * Copyright (c) 2015-2016 Red Hat, Inc.
  3. *
  4. * All rights reserved.
  5. *
  6. * Author: Jan Friesse (jfriesse@redhat.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 Red Hat, 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 <string.h>
  35. #include "timer-list.h"
  36. void
  37. timer_list_init(struct timer_list *tlist)
  38. {
  39. memset(tlist, 0, sizeof(*tlist));
  40. TAILQ_INIT(&tlist->list);
  41. TAILQ_INIT(&tlist->free_list);
  42. }
  43. static PRIntervalTime
  44. timer_list_entry_time_to_expire(const struct timer_list_entry *entry, PRIntervalTime current_time)
  45. {
  46. PRIntervalTime diff, half_interval;
  47. diff = entry->expire_time - current_time;
  48. half_interval = ~0;
  49. half_interval /= 2;
  50. if (diff > half_interval) {
  51. return (0);
  52. }
  53. return (diff);
  54. }
  55. static int
  56. timer_list_entry_cmp(const struct timer_list_entry *entry1,
  57. const struct timer_list_entry *entry2, PRIntervalTime current_time)
  58. {
  59. PRIntervalTime diff1, diff2;
  60. int res;
  61. diff1 = timer_list_entry_time_to_expire(entry1, current_time);
  62. diff2 = timer_list_entry_time_to_expire(entry2, current_time);
  63. res = 0;
  64. if (diff1 < diff2) res = -1;
  65. if (diff1 > diff2) res = 1;
  66. return (res);
  67. }
  68. static void
  69. timer_list_insert_into_list(struct timer_list *tlist, struct timer_list_entry *new_entry)
  70. {
  71. struct timer_list_entry *entry;
  72. /*
  73. * This can overflow and it's not a problem
  74. */
  75. new_entry->expire_time = new_entry->epoch + PR_MillisecondsToInterval(new_entry->interval);
  76. entry = TAILQ_FIRST(&tlist->list);
  77. while (entry != NULL) {
  78. if (timer_list_entry_cmp(entry, new_entry, new_entry->epoch) > 0) {
  79. /*
  80. * Insert new entry right before current entry
  81. */
  82. TAILQ_INSERT_BEFORE(entry, new_entry, entries);
  83. break;
  84. }
  85. entry = TAILQ_NEXT(entry, entries);
  86. }
  87. if (entry == NULL) {
  88. TAILQ_INSERT_TAIL(&tlist->list, new_entry, entries);
  89. }
  90. }
  91. struct timer_list_entry *
  92. timer_list_add(struct timer_list *tlist, PRUint32 interval, timer_list_cb_fn func, void *data1,
  93. void *data2)
  94. {
  95. struct timer_list_entry *new_entry;
  96. if (interval < 1 || interval > TIMER_LIST_MAX_INTERVAL) {
  97. return (NULL);
  98. }
  99. if (!TAILQ_EMPTY(&tlist->free_list)) {
  100. /*
  101. * Use free list entry
  102. */
  103. new_entry = TAILQ_FIRST(&tlist->free_list);
  104. TAILQ_REMOVE(&tlist->free_list, new_entry, entries);
  105. } else {
  106. /*
  107. * Alloc new entry
  108. */
  109. new_entry = malloc(sizeof(*new_entry));
  110. if (new_entry == NULL) {
  111. return (NULL);
  112. }
  113. }
  114. memset(new_entry, 0, sizeof(*new_entry));
  115. new_entry->epoch = PR_IntervalNow();
  116. new_entry->interval = interval;
  117. new_entry->func = func;
  118. new_entry->user_data1 = data1;
  119. new_entry->user_data2 = data2;
  120. new_entry->is_active = 1;
  121. timer_list_insert_into_list(tlist, new_entry);
  122. return (new_entry);
  123. }
  124. void
  125. timer_list_reschedule(struct timer_list *tlist, struct timer_list_entry *entry)
  126. {
  127. if (entry->is_active) {
  128. entry->epoch = PR_IntervalNow();
  129. TAILQ_REMOVE(&tlist->list, entry, entries);
  130. timer_list_insert_into_list(tlist, entry);
  131. }
  132. }
  133. void
  134. timer_list_expire(struct timer_list *tlist)
  135. {
  136. PRIntervalTime now;
  137. struct timer_list_entry *entry;
  138. int res;
  139. now = PR_IntervalNow();
  140. while ((entry = TAILQ_FIRST(&tlist->list)) != NULL &&
  141. timer_list_entry_time_to_expire(entry, now) == 0) {
  142. /*
  143. * Expired
  144. */
  145. res = entry->func(entry->user_data1, entry->user_data2);
  146. if (res == 0) {
  147. /*
  148. * Move item to free list
  149. */
  150. timer_list_delete(tlist, entry);
  151. } else if (entry->is_active) {
  152. /*
  153. * Schedule again
  154. */
  155. entry->epoch = now;
  156. TAILQ_REMOVE(&tlist->list, entry, entries);
  157. timer_list_insert_into_list(tlist, entry);
  158. }
  159. }
  160. }
  161. PRIntervalTime
  162. timer_list_time_to_expire(struct timer_list *tlist)
  163. {
  164. struct timer_list_entry *entry;
  165. entry = TAILQ_FIRST(&tlist->list);
  166. if (entry == NULL) {
  167. return (PR_INTERVAL_NO_TIMEOUT);
  168. }
  169. return (timer_list_entry_time_to_expire(entry, PR_IntervalNow()));
  170. }
  171. uint32_t
  172. timer_list_time_to_expire_ms(struct timer_list *tlist)
  173. {
  174. struct timer_list_entry *entry;
  175. uint32_t u32;
  176. entry = TAILQ_FIRST(&tlist->list);
  177. if (entry == NULL) {
  178. u32 = ~((uint32_t)0);
  179. return (u32);
  180. }
  181. return (PR_IntervalToMilliseconds(timer_list_entry_time_to_expire(entry, PR_IntervalNow())));
  182. }
  183. void
  184. timer_list_delete(struct timer_list *tlist, struct timer_list_entry *entry)
  185. {
  186. if (entry->is_active) {
  187. /*
  188. * Move item to free list
  189. */
  190. TAILQ_REMOVE(&tlist->list, entry, entries);
  191. TAILQ_INSERT_HEAD(&tlist->free_list, entry, entries);
  192. entry->is_active = 0;
  193. }
  194. }
  195. void
  196. timer_list_free(struct timer_list *tlist)
  197. {
  198. struct timer_list_entry *entry;
  199. struct timer_list_entry *entry_next;
  200. entry = TAILQ_FIRST(&tlist->list);
  201. while (entry != NULL) {
  202. entry_next = TAILQ_NEXT(entry, entries);
  203. free(entry);
  204. entry = entry_next;
  205. }
  206. entry = TAILQ_FIRST(&tlist->free_list);
  207. while (entry != NULL) {
  208. entry_next = TAILQ_NEXT(entry, entries);
  209. free(entry);
  210. entry = entry_next;
  211. }
  212. timer_list_init(tlist);
  213. }