timer-list.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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_expire(struct timer_list *tlist)
  126. {
  127. PRIntervalTime now;
  128. struct timer_list_entry *entry;
  129. int res;
  130. now = PR_IntervalNow();
  131. while ((entry = TAILQ_FIRST(&tlist->list)) != NULL &&
  132. timer_list_entry_time_to_expire(entry, now) == 0) {
  133. /*
  134. * Expired
  135. */
  136. res = entry->func(entry->user_data1, entry->user_data2);
  137. if (res == 0) {
  138. /*
  139. * Move item to free list
  140. */
  141. timer_list_delete(tlist, entry);
  142. } else if (entry->is_active) {
  143. /*
  144. * Schedule again
  145. */
  146. entry->epoch = now;
  147. TAILQ_REMOVE(&tlist->list, entry, entries);
  148. timer_list_insert_into_list(tlist, entry);
  149. }
  150. }
  151. }
  152. PRIntervalTime
  153. timer_list_time_to_expire(struct timer_list *tlist)
  154. {
  155. struct timer_list_entry *entry;
  156. entry = TAILQ_FIRST(&tlist->list);
  157. if (entry == NULL) {
  158. return (PR_INTERVAL_NO_TIMEOUT);
  159. }
  160. return (timer_list_entry_time_to_expire(entry, PR_IntervalNow()));
  161. }
  162. void
  163. timer_list_delete(struct timer_list *tlist, struct timer_list_entry *entry)
  164. {
  165. if (entry->is_active) {
  166. /*
  167. * Move item to free list
  168. */
  169. TAILQ_REMOVE(&tlist->list, entry, entries);
  170. TAILQ_INSERT_HEAD(&tlist->free_list, entry, entries);
  171. entry->is_active = 0;
  172. }
  173. }
  174. void
  175. timer_list_free(struct timer_list *tlist)
  176. {
  177. struct timer_list_entry *entry;
  178. struct timer_list_entry *entry_next;
  179. entry = TAILQ_FIRST(&tlist->list);
  180. while (entry != NULL) {
  181. entry_next = TAILQ_NEXT(entry, entries);
  182. free(entry);
  183. entry = entry_next;
  184. }
  185. entry = TAILQ_FIRST(&tlist->free_list);
  186. while (entry != NULL) {
  187. entry_next = TAILQ_NEXT(entry, entries);
  188. free(entry);
  189. entry = entry_next;
  190. }
  191. timer_list_init(tlist);
  192. }