event_queue.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /*
  2. * Copyright 2022-2026 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. #ifndef OSSL_INTERNAL_EVENT_QUEUE_H
  10. #define OSSL_INTERNAL_EVENT_QUEUE_H
  11. #pragma once
  12. #include "internal/priority_queue.h"
  13. #include "internal/time.h"
  14. /*
  15. * Opaque type holding an event.
  16. */
  17. typedef struct ossl_event_st OSSL_EVENT;
  18. DEFINE_PRIORITY_QUEUE_OF(OSSL_EVENT);
  19. /*
  20. * Public type representing an event queue, the underlying structure being
  21. * opaque.
  22. */
  23. typedef struct ossl_event_queue_st OSSL_EVENT_QUEUE;
  24. /*
  25. * Public type representing a event queue entry.
  26. * It is (internally) public so that it can be embedded into other structures,
  27. * it should otherwise be treated as opaque.
  28. */
  29. struct ossl_event_st {
  30. uint32_t type; /* What type of event this is */
  31. uint32_t priority; /* What priority this event has */
  32. OSSL_TIME when; /* When the event is scheduled to happen */
  33. void *ctx; /* User argument passed to call backs */
  34. void *payload; /* Event specific data of unknown kind */
  35. size_t payload_size; /* Length (in bytes) of event specific data */
  36. /* These fields are for internal use only */
  37. PRIORITY_QUEUE_OF(OSSL_EVENT) * queue; /* Queue containing this event */
  38. size_t ref; /* ID for this event */
  39. unsigned int flag_dynamic : 1; /* Malloced or not? */
  40. };
  41. /*
  42. * Utility function to populate an event structure and add it to the queue
  43. */
  44. int ossl_event_queue_add(OSSL_EVENT_QUEUE *queue, OSSL_EVENT *event,
  45. uint32_t type, uint32_t priority,
  46. OSSL_TIME when, void *ctx,
  47. void *payload, size_t payload_size);
  48. /*
  49. * Utility functions to extract event fields
  50. */
  51. static ossl_unused ossl_inline uint32_t
  52. ossl_event_get_type(const OSSL_EVENT *event)
  53. {
  54. return event->type;
  55. }
  56. static ossl_unused ossl_inline uint32_t
  57. ossl_event_get_priority(const OSSL_EVENT *event)
  58. {
  59. return event->priority;
  60. }
  61. static ossl_unused ossl_inline OSSL_TIME
  62. ossl_event_get_when(const OSSL_EVENT *event)
  63. {
  64. return event->when;
  65. }
  66. static ossl_unused ossl_inline void *ossl_event_get0_ctx(const OSSL_EVENT *event)
  67. {
  68. return event->ctx;
  69. }
  70. static ossl_unused ossl_inline void *ossl_event_get0_payload(const OSSL_EVENT *event, size_t *length)
  71. {
  72. if (length != NULL)
  73. *length = event->payload_size;
  74. return event->payload;
  75. }
  76. /*
  77. * Create and free a queue.
  78. */
  79. OSSL_EVENT_QUEUE *ossl_event_queue_new(void);
  80. void ossl_event_queue_free(OSSL_EVENT_QUEUE *queue);
  81. /*
  82. * Schedule a new event into an event queue.
  83. *
  84. * The event parameters are taken from the function arguments.
  85. *
  86. * The function returns NULL on failure and the added event on success.
  87. */
  88. OSSL_EVENT *ossl_event_queue_add_new(OSSL_EVENT_QUEUE *queue,
  89. uint32_t type, uint32_t priority,
  90. OSSL_TIME when, void *ctx,
  91. void *payload, size_t payload_size);
  92. /*
  93. * Schedule an event into an event queue.
  94. *
  95. * The event parameters are taken from the function arguments.
  96. *
  97. * The function returns 0 on failure and 1 on success.
  98. */
  99. int ossl_event_queue_add(OSSL_EVENT_QUEUE *queue, OSSL_EVENT *event,
  100. uint32_t type, uint32_t priority,
  101. OSSL_TIME when, void *ctx,
  102. void *payload, size_t payload_size);
  103. /*
  104. * Delete an event from the queue.
  105. * This will cause the early deletion function to be called if it is non-NULL.
  106. * A pointer to the event structure is returned.
  107. */
  108. int ossl_event_queue_remove(OSSL_EVENT_QUEUE *queue, OSSL_EVENT *event);
  109. /*
  110. * Free a dynamic event.
  111. * Is a NOP for a static event.
  112. */
  113. void ossl_event_free(OSSL_EVENT *event);
  114. /*
  115. * Return the time until the next event for the specified event, if the event's
  116. * time is past, zero is returned. Once activated, the event reference becomes
  117. * invalid and this function becomes undefined.
  118. */
  119. OSSL_TIME ossl_event_time_until(const OSSL_EVENT *event);
  120. /*
  121. * Return the time until the next event in the queue.
  122. * If the next event is in the past, zero is returned.
  123. */
  124. OSSL_TIME ossl_event_queue_time_until_next(const OSSL_EVENT_QUEUE *queue);
  125. /*
  126. * Postpone an event to trigger at the specified time.
  127. * If the event has triggered, this function's behaviour is undefined.
  128. */
  129. int ossl_event_queue_postpone_until(OSSL_EVENT_QUEUE *queue,
  130. OSSL_EVENT *event,
  131. OSSL_TIME when);
  132. /*
  133. * Return the next event to process.
  134. */
  135. int ossl_event_queue_get1_next_event(OSSL_EVENT_QUEUE *queue,
  136. OSSL_EVENT **event);
  137. #endif