timer-list.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. /*
  2. * Copyright (c) 2015 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 <assert.h>
  36. #include "timer-list.h"
  37. void
  38. timer_list_init(struct timer_list *tlist)
  39. {
  40. memset(tlist, 0, sizeof(*tlist));
  41. TAILQ_INIT(&tlist->list);
  42. TAILQ_INIT(&tlist->free_list);
  43. }
  44. struct timer_list_entry *
  45. timer_list_add(struct timer_list *tlist, PRUint32 interval, timer_list_cb_fn func, void *data1,
  46. void *data2)
  47. {
  48. struct timer_list_entry *entry;
  49. assert(tlist->list_expire_in_progress == 0);
  50. if (interval > (0xffffffffUL / 4)) {
  51. return (NULL);
  52. }
  53. if (!TAILQ_EMPTY(&tlist->free_list)) {
  54. /*
  55. * Use free list entry
  56. */
  57. entry = TAILQ_FIRST(&tlist->free_list);
  58. TAILQ_REMOVE(&tlist->free_list, entry, entries);
  59. } else {
  60. /*
  61. * Alloc new entry
  62. */
  63. entry = malloc(sizeof(*entry));
  64. if (entry == NULL) {
  65. return (NULL);
  66. }
  67. }
  68. memset(entry, 0, sizeof(*entry));
  69. entry->epoch = PR_IntervalNow();
  70. entry->interval = interval;
  71. entry->func = func;
  72. entry->user_data1 = data1;
  73. entry->user_data2 = data2;
  74. TAILQ_INSERT_TAIL(&tlist->list, entry, entries);
  75. return (entry);
  76. }
  77. void
  78. timer_list_expire(struct timer_list *tlist)
  79. {
  80. PRIntervalTime now;
  81. struct timer_list_entry *entry;
  82. struct timer_list_entry *entry_next;
  83. PRUint32 delta;
  84. int res;
  85. tlist->list_expire_in_progress = 1;
  86. now = PR_IntervalNow();
  87. entry = TAILQ_FIRST(&tlist->list);
  88. while (entry != NULL) {
  89. entry_next = TAILQ_NEXT(entry, entries);
  90. delta = PR_IntervalToMilliseconds(now - entry->epoch);
  91. if (delta >= entry->interval) {
  92. /*
  93. * Expired
  94. */
  95. res = entry->func(entry->user_data1, entry->user_data2);
  96. if (res == 0) {
  97. /*
  98. * Move item to free list
  99. */
  100. TAILQ_REMOVE(&tlist->list, entry, entries);
  101. TAILQ_INSERT_HEAD(&tlist->free_list, entry, entries);
  102. } else {
  103. /*
  104. * Schedule again
  105. */
  106. entry->epoch = now;
  107. }
  108. }
  109. entry = entry_next;
  110. }
  111. tlist->list_expire_in_progress = 0;
  112. }
  113. PRIntervalTime
  114. timer_list_time_to_expire(struct timer_list *tlist)
  115. {
  116. PRIntervalTime now;
  117. struct timer_list_entry *entry;
  118. PRUint32 delta;
  119. PRUint32 timeout;
  120. PRUint32 min_timeout;
  121. int min_timeout_set;
  122. min_timeout_set = 0;
  123. now = PR_IntervalNow();
  124. TAILQ_FOREACH(entry, &tlist->list, entries) {
  125. delta = PR_IntervalToMilliseconds(now - entry->epoch);
  126. if (delta >= entry->interval) {
  127. /*
  128. * One of timer already expired
  129. */
  130. return (PR_INTERVAL_NO_WAIT);
  131. }
  132. timeout = entry->interval - delta;
  133. if (!min_timeout_set) {
  134. min_timeout_set = 1;
  135. min_timeout = timeout;
  136. }
  137. if (timeout < min_timeout) {
  138. min_timeout = timeout;
  139. }
  140. }
  141. if (!min_timeout_set) {
  142. return (PR_INTERVAL_NO_TIMEOUT);
  143. }
  144. return (PR_MillisecondsToInterval(min_timeout));
  145. }
  146. void
  147. timer_list_delete(struct timer_list *tlist, struct timer_list_entry *entry)
  148. {
  149. assert(tlist->list_expire_in_progress == 0);
  150. /*
  151. * Move item to free list
  152. */
  153. TAILQ_REMOVE(&tlist->list, entry, entries);
  154. TAILQ_INSERT_HEAD(&tlist->free_list, entry, entries);
  155. }
  156. void
  157. timer_list_free(struct timer_list *tlist)
  158. {
  159. struct timer_list_entry *entry;
  160. struct timer_list_entry *entry_next;
  161. entry = TAILQ_FIRST(&tlist->list);
  162. while (entry != NULL) {
  163. entry_next = TAILQ_NEXT(entry, entries);
  164. free(entry);
  165. entry = entry_next;
  166. }
  167. entry = TAILQ_FIRST(&tlist->free_list);
  168. while (entry != NULL) {
  169. entry_next = TAILQ_NEXT(entry, entries);
  170. free(entry);
  171. entry = entry_next;
  172. }
  173. timer_list_init(tlist);
  174. }