timer-list.c 4.9 KB

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