timer-list.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  1. /*
  2. * Copyright (c) 2015-2020 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 <assert.h>
  35. #include <string.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->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 size_t
  69. timer_list_heap_index_left(size_t index)
  70. {
  71. return (2 * index + 1);
  72. }
  73. static size_t
  74. timer_list_heap_index_right(size_t index)
  75. {
  76. return (2 * index + 2);
  77. }
  78. static size_t
  79. timer_list_heap_index_parent(size_t index)
  80. {
  81. return ((index - 1) / 2);
  82. }
  83. static void
  84. timer_list_heap_sift_up(struct timer_list *tlist, size_t item_pos)
  85. {
  86. size_t parent_pos;
  87. struct timer_list_entry *parent_entry;
  88. struct timer_list_entry *item_entry;
  89. item_entry = tlist->entries[item_pos];
  90. parent_pos = timer_list_heap_index_parent(item_pos);
  91. while (item_pos > 0 &&
  92. (parent_entry = tlist->entries[parent_pos],
  93. timer_list_entry_cmp(parent_entry, item_entry, item_entry->epoch) > 0)) {
  94. /*
  95. * Swap item and parent
  96. */
  97. tlist->entries[parent_pos] = item_entry;
  98. tlist->entries[item_pos] = parent_entry;
  99. item_pos = parent_pos;
  100. parent_pos = timer_list_heap_index_parent(item_pos);
  101. }
  102. }
  103. static void
  104. timer_list_heap_sift_down(struct timer_list *tlist, size_t item_pos)
  105. {
  106. int cont;
  107. size_t left_pos, right_pos, smallest_pos;
  108. struct timer_list_entry *left_entry;
  109. struct timer_list_entry *right_entry;
  110. struct timer_list_entry *smallest_entry;
  111. struct timer_list_entry *tmp_entry;
  112. cont = 1;
  113. while (cont) {
  114. smallest_pos = item_pos;
  115. left_pos = timer_list_heap_index_left(item_pos);
  116. right_pos = timer_list_heap_index_right(item_pos);
  117. smallest_entry = tlist->entries[smallest_pos];
  118. if (left_pos < tlist->size &&
  119. (left_entry = tlist->entries[left_pos],
  120. timer_list_entry_cmp(left_entry, smallest_entry, smallest_entry->epoch) < 0)) {
  121. smallest_entry = left_entry;
  122. smallest_pos = left_pos;
  123. }
  124. if (right_pos < tlist->size &&
  125. (right_entry = tlist->entries[right_pos],
  126. timer_list_entry_cmp(right_entry, smallest_entry, smallest_entry->epoch) < 0)) {
  127. smallest_entry = right_entry;
  128. smallest_pos = right_pos;
  129. }
  130. if (smallest_pos == item_pos) {
  131. /*
  132. * Item is smallest (or has no childs) -> heap property is restored
  133. */
  134. cont = 0;
  135. } else {
  136. /*
  137. * Swap item with smallest child
  138. */
  139. tmp_entry = tlist->entries[item_pos];
  140. tlist->entries[item_pos] = smallest_entry;
  141. tlist->entries[smallest_pos] = tmp_entry;
  142. item_pos = smallest_pos;
  143. }
  144. }
  145. }
  146. static int
  147. timer_list_heap_delete(struct timer_list *tlist, struct timer_list_entry *entry)
  148. {
  149. size_t entry_pos;
  150. size_t i;
  151. struct timer_list_entry *replacement_entry;
  152. int cmp_entries;
  153. entry_pos = tlist->size;
  154. /*
  155. * Find the element index
  156. */
  157. for (i = 0; i < tlist->size; i++) {
  158. if (tlist->entries[i] == entry) {
  159. entry_pos = i;
  160. break ;
  161. }
  162. }
  163. if (entry_pos == tlist->size) {
  164. /*
  165. * Item not found
  166. */
  167. return (-1);
  168. }
  169. /*
  170. * Swap element with last element
  171. */
  172. replacement_entry = tlist->entries[tlist->size - 1];
  173. tlist->entries[entry_pos] = tlist->entries[tlist->size - 1];
  174. /*
  175. * And "remove" last element (= entry)
  176. */
  177. tlist->size--;
  178. /*
  179. * Up (or down) heapify based on replacement item size
  180. */
  181. cmp_entries = timer_list_entry_cmp(replacement_entry, entry, entry->epoch);
  182. if (cmp_entries < 0) {
  183. timer_list_heap_sift_up(tlist, entry_pos);
  184. } else if (cmp_entries > 0) {
  185. timer_list_heap_sift_down(tlist, entry_pos);
  186. }
  187. return (0);
  188. }
  189. /*
  190. * Check if heap is valid.
  191. * - Shape property is always fullfiled because of storage in array
  192. * - Check heap property
  193. */
  194. int
  195. timer_list_debug_is_valid_heap(struct timer_list *tlist)
  196. {
  197. size_t i;
  198. size_t left_pos, right_pos;
  199. struct timer_list_entry *left_entry;
  200. struct timer_list_entry *right_entry;
  201. struct timer_list_entry *cur_entry;
  202. for (i = 0; i < tlist->size; i++) {
  203. cur_entry = tlist->entries[i];
  204. left_pos = timer_list_heap_index_left(i);
  205. right_pos = timer_list_heap_index_right(i);
  206. if (left_pos < tlist->size &&
  207. (left_entry = tlist->entries[left_pos],
  208. timer_list_entry_cmp(left_entry, cur_entry, cur_entry->epoch) < 0)) {
  209. return (0);
  210. }
  211. if (right_pos < tlist->size &&
  212. (right_entry = tlist->entries[right_pos],
  213. timer_list_entry_cmp(right_entry, cur_entry, cur_entry->epoch) < 0)) {
  214. return (0);
  215. }
  216. }
  217. return (1);
  218. }
  219. static int
  220. timer_list_insert_into_list(struct timer_list *tlist, struct timer_list_entry *new_entry)
  221. {
  222. size_t new_size;
  223. struct timer_list_entry **new_entries;
  224. /*
  225. * This can overflow and it's not a problem
  226. */
  227. new_entry->expire_time = new_entry->epoch + PR_MillisecondsToInterval(new_entry->interval);
  228. /*
  229. * Heap insert
  230. */
  231. if (tlist->size + 1 > tlist->allocated) {
  232. new_size = (tlist->allocated + 1) * 2;
  233. new_entries = realloc(tlist->entries, new_size * sizeof(tlist->entries[0]));
  234. if (new_entries == NULL) {
  235. return (-1);
  236. }
  237. tlist->allocated = new_size;
  238. tlist->entries = new_entries;
  239. }
  240. tlist->entries[tlist->size] = new_entry;
  241. tlist->size++;
  242. timer_list_heap_sift_up(tlist, tlist->size - 1);
  243. return (0);
  244. }
  245. struct timer_list_entry *
  246. timer_list_add(struct timer_list *tlist, PRUint32 interval, timer_list_cb_fn func, void *data1,
  247. void *data2)
  248. {
  249. struct timer_list_entry *new_entry;
  250. if (interval < 1 || interval > TIMER_LIST_MAX_INTERVAL || func == NULL) {
  251. return (NULL);
  252. }
  253. if (!TAILQ_EMPTY(&tlist->free_list)) {
  254. /*
  255. * Use free list entry
  256. */
  257. new_entry = TAILQ_FIRST(&tlist->free_list);
  258. TAILQ_REMOVE(&tlist->free_list, new_entry, entries);
  259. } else {
  260. /*
  261. * Alloc new entry
  262. */
  263. new_entry = malloc(sizeof(*new_entry));
  264. if (new_entry == NULL) {
  265. return (NULL);
  266. }
  267. }
  268. memset(new_entry, 0, sizeof(*new_entry));
  269. new_entry->epoch = PR_IntervalNow();
  270. new_entry->interval = interval;
  271. new_entry->func = func;
  272. new_entry->user_data1 = data1;
  273. new_entry->user_data2 = data2;
  274. new_entry->is_active = 1;
  275. if (timer_list_insert_into_list(tlist, new_entry) != 0) {
  276. TAILQ_INSERT_HEAD(&tlist->free_list, new_entry, entries);
  277. return (NULL);
  278. }
  279. return (new_entry);
  280. }
  281. void
  282. timer_list_reschedule(struct timer_list *tlist, struct timer_list_entry *entry)
  283. {
  284. int res;
  285. if (entry->is_active) {
  286. res = timer_list_heap_delete(tlist, entry);
  287. assert(res == 0);
  288. entry->epoch = PR_IntervalNow();
  289. timer_list_insert_into_list(tlist, entry);
  290. }
  291. }
  292. void
  293. timer_list_expire(struct timer_list *tlist)
  294. {
  295. PRIntervalTime now;
  296. struct timer_list_entry *entry;
  297. int res;
  298. now = PR_IntervalNow();
  299. while (tlist->size > 0 &&
  300. (entry = tlist->entries[0],
  301. timer_list_entry_time_to_expire(entry, now) == 0)) {
  302. /*
  303. * Expired
  304. */
  305. res = entry->func(entry->user_data1, entry->user_data2);
  306. if (res == 0) {
  307. /*
  308. * Move item to free list
  309. */
  310. timer_list_delete(tlist, entry);
  311. } else if (entry->is_active) {
  312. /*
  313. * Schedule again
  314. */
  315. res = timer_list_heap_delete(tlist, entry);
  316. assert(res == 0);
  317. entry->epoch = now;
  318. timer_list_insert_into_list(tlist, entry);
  319. }
  320. }
  321. }
  322. PRIntervalTime
  323. timer_list_time_to_expire(struct timer_list *tlist)
  324. {
  325. struct timer_list_entry *entry;
  326. if (tlist->size == 0) {
  327. return (PR_INTERVAL_NO_TIMEOUT);
  328. }
  329. entry = tlist->entries[0];
  330. return (timer_list_entry_time_to_expire(entry, PR_IntervalNow()));
  331. }
  332. uint32_t
  333. timer_list_time_to_expire_ms(struct timer_list *tlist)
  334. {
  335. struct timer_list_entry *entry;
  336. uint32_t u32;
  337. if (tlist->size == 0) {
  338. u32 = ~((uint32_t)0);
  339. return (u32);
  340. }
  341. entry = tlist->entries[0];
  342. return (PR_IntervalToMilliseconds(timer_list_entry_time_to_expire(entry, PR_IntervalNow())));
  343. }
  344. void
  345. timer_list_delete(struct timer_list *tlist, struct timer_list_entry *entry)
  346. {
  347. int res;
  348. if (entry->is_active) {
  349. /*
  350. * Remove item from heap and move it to free list
  351. */
  352. res = timer_list_heap_delete(tlist, entry);
  353. assert(res == 0);
  354. TAILQ_INSERT_HEAD(&tlist->free_list, entry, entries);
  355. entry->is_active = 0;
  356. }
  357. }
  358. void
  359. timer_list_free(struct timer_list *tlist)
  360. {
  361. struct timer_list_entry *entry;
  362. struct timer_list_entry *entry_next;
  363. size_t i;
  364. for (i = 0; i < tlist->size; i++) {
  365. free(tlist->entries[i]);
  366. }
  367. free(tlist->entries);
  368. entry = TAILQ_FIRST(&tlist->free_list);
  369. while (entry != NULL) {
  370. entry_next = TAILQ_NEXT(entry, entries);
  371. free(entry);
  372. entry = entry_next;
  373. }
  374. timer_list_init(tlist);
  375. }