2
0

event_queue.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. /*
  2. * Copyright 2022-2023 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #include <stdlib.h>
  10. #include "internal/event_queue.h"
  11. #include "ssl_local.h"
  12. struct ossl_event_queue_st {
  13. PRIORITY_QUEUE_OF(OSSL_EVENT) *timed_events;
  14. PRIORITY_QUEUE_OF(OSSL_EVENT) *now_events;
  15. };
  16. static int event_compare_times(const OSSL_EVENT *a, const OSSL_EVENT *b)
  17. {
  18. return ossl_time_compare(a->when, b->when);
  19. }
  20. static int event_compare_priority(const OSSL_EVENT *a, const OSSL_EVENT *b)
  21. {
  22. if (a->priority > b->priority)
  23. return -1;
  24. if (a->priority < b->priority)
  25. return 1;
  26. return 0;
  27. }
  28. OSSL_EVENT_QUEUE *ossl_event_queue_new(void)
  29. {
  30. OSSL_EVENT_QUEUE *r = OPENSSL_malloc(sizeof(*r));
  31. if (r != NULL) {
  32. r->timed_events = ossl_pqueue_OSSL_EVENT_new(&event_compare_times);
  33. r->now_events = ossl_pqueue_OSSL_EVENT_new(&event_compare_priority);
  34. if (r->timed_events == NULL || r->now_events == NULL) {
  35. ossl_event_queue_free(r);
  36. return NULL;
  37. }
  38. }
  39. return r;
  40. }
  41. void ossl_event_free(OSSL_EVENT *event)
  42. {
  43. if (event != NULL) {
  44. if (event->flag_dynamic)
  45. OPENSSL_free(event);
  46. else
  47. event->queue = NULL;
  48. }
  49. }
  50. static void event_queue_free(PRIORITY_QUEUE_OF(OSSL_EVENT) *queue)
  51. {
  52. OSSL_EVENT *e;
  53. if (queue != NULL) {
  54. while ((e = ossl_pqueue_OSSL_EVENT_pop(queue)) != NULL)
  55. ossl_event_free(e);
  56. ossl_pqueue_OSSL_EVENT_free(queue);
  57. }
  58. }
  59. void ossl_event_queue_free(OSSL_EVENT_QUEUE *queue)
  60. {
  61. if (queue != NULL) {
  62. event_queue_free(queue->now_events);
  63. event_queue_free(queue->timed_events);
  64. OPENSSL_free(queue);
  65. }
  66. }
  67. static ossl_inline
  68. int event_queue_add(OSSL_EVENT_QUEUE *queue, OSSL_EVENT *event)
  69. {
  70. PRIORITY_QUEUE_OF(OSSL_EVENT) *pq =
  71. ossl_time_compare(event->when, ossl_time_now()) <= 0
  72. ? queue->now_events
  73. : queue->timed_events;
  74. if (ossl_pqueue_OSSL_EVENT_push(pq, event, &event->ref)) {
  75. event->queue = pq;
  76. return 1;
  77. }
  78. return 0;
  79. }
  80. static ossl_inline
  81. void ossl_event_set(OSSL_EVENT *event, uint32_t type, uint32_t priority,
  82. OSSL_TIME when, void *ctx,
  83. void *payload, size_t payload_size)
  84. {
  85. event->type = type;
  86. event->priority = priority;
  87. event->when = when;
  88. event->ctx = ctx;
  89. event->payload = payload;
  90. event->payload_size = payload_size;
  91. }
  92. OSSL_EVENT *ossl_event_queue_add_new(OSSL_EVENT_QUEUE *queue,
  93. uint32_t type, uint32_t priority,
  94. OSSL_TIME when, void *ctx,
  95. void *payload, size_t payload_size)
  96. {
  97. OSSL_EVENT *e = OPENSSL_malloc(sizeof(*e));
  98. if (e == NULL || queue == NULL) {
  99. OPENSSL_free(e);
  100. return NULL;
  101. }
  102. ossl_event_set(e, type, priority, when, ctx, payload, payload_size);
  103. e->flag_dynamic = 1;
  104. if (event_queue_add(queue, e))
  105. return e;
  106. OPENSSL_free(e);
  107. return NULL;
  108. }
  109. int ossl_event_queue_add(OSSL_EVENT_QUEUE *queue, OSSL_EVENT *event,
  110. uint32_t type, uint32_t priority,
  111. OSSL_TIME when, void *ctx,
  112. void *payload, size_t payload_size)
  113. {
  114. if (event == NULL || queue == NULL)
  115. return 0;
  116. ossl_event_set(event, type, priority, when, ctx, payload, payload_size);
  117. event->flag_dynamic = 0;
  118. return event_queue_add(queue, event);
  119. }
  120. int ossl_event_queue_remove(OSSL_EVENT_QUEUE *queue, OSSL_EVENT *event)
  121. {
  122. if (event != NULL && event->queue != NULL) {
  123. ossl_pqueue_OSSL_EVENT_remove(event->queue, event->ref);
  124. event->queue = NULL;
  125. }
  126. return 1;
  127. }
  128. OSSL_TIME ossl_event_time_until(const OSSL_EVENT *event)
  129. {
  130. if (event == NULL)
  131. return ossl_time_infinite();
  132. return ossl_time_subtract(event->when, ossl_time_now());
  133. }
  134. OSSL_TIME ossl_event_queue_time_until_next(const OSSL_EVENT_QUEUE *queue)
  135. {
  136. if (queue == NULL)
  137. return ossl_time_infinite();
  138. if (ossl_pqueue_OSSL_EVENT_num(queue->now_events) > 0)
  139. return ossl_time_zero();
  140. return ossl_event_time_until(ossl_pqueue_OSSL_EVENT_peek(queue->timed_events));
  141. }
  142. int ossl_event_queue_postpone_until(OSSL_EVENT_QUEUE *queue,
  143. OSSL_EVENT *event,
  144. OSSL_TIME when)
  145. {
  146. if (ossl_event_queue_remove(queue, event)) {
  147. event->when = when;
  148. return event_queue_add(queue, event);
  149. }
  150. return 0;
  151. }
  152. int ossl_event_queue_get1_next_event(OSSL_EVENT_QUEUE *queue,
  153. OSSL_EVENT **event)
  154. {
  155. OSSL_TIME now = ossl_time_now();
  156. OSSL_EVENT *e;
  157. /* Check for expired timer based events and convert them to now events */
  158. while ((e = ossl_pqueue_OSSL_EVENT_peek(queue->timed_events)) != NULL
  159. && ossl_time_compare(e->when, now) <= 0) {
  160. e = ossl_pqueue_OSSL_EVENT_pop(queue->timed_events);
  161. if (!ossl_pqueue_OSSL_EVENT_push(queue->now_events, e, &e->ref)) {
  162. e->queue = NULL;
  163. return 0;
  164. }
  165. }
  166. /*
  167. * Get next event from the now queue.
  168. * The pop returns NULL when there is none.
  169. */
  170. *event = ossl_pqueue_OSSL_EVENT_pop(queue->now_events);
  171. return 1;
  172. }