event_queue.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /*
  2. * Copyright 2022 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
  52. uint32_t ossl_event_get_type(const OSSL_EVENT *event)
  53. {
  54. return event->type;
  55. }
  56. static ossl_unused ossl_inline
  57. uint32_t ossl_event_get_priority(const OSSL_EVENT *event)
  58. {
  59. return event->priority;
  60. }
  61. static ossl_unused ossl_inline
  62. OSSL_TIME ossl_event_get_when(const OSSL_EVENT *event)
  63. {
  64. return event->when;
  65. }
  66. static ossl_unused ossl_inline
  67. void *ossl_event_get0_ctx(const OSSL_EVENT *event)
  68. {
  69. return event->ctx;
  70. }
  71. static ossl_unused ossl_inline
  72. void *ossl_event_get0_payload(const OSSL_EVENT *event, size_t *length)
  73. {
  74. if (length != NULL)
  75. *length = event->payload_size;
  76. return event->payload;
  77. }
  78. /*
  79. * Create and free a queue.
  80. */
  81. OSSL_EVENT_QUEUE *ossl_event_queue_new(void);
  82. void ossl_event_queue_free(OSSL_EVENT_QUEUE *queue);
  83. /*
  84. * Schedule a new event into an event queue.
  85. *
  86. * The event parameters are taken from the function arguments.
  87. *
  88. * The function reutrns NULL on failure and the added event on success.
  89. */
  90. OSSL_EVENT *ossl_event_queue_add_new(OSSL_EVENT_QUEUE *queue,
  91. uint32_t type, uint32_t priority,
  92. OSSL_TIME when, void *ctx,
  93. void *payload, size_t payload_size)
  94. ;
  95. /*
  96. * Schedule an event into an event queue.
  97. *
  98. * The event parameters are taken from the function arguments.
  99. *
  100. * The function reutrns 0 on failure and 1 on success.
  101. */
  102. int ossl_event_queue_add(OSSL_EVENT_QUEUE *queue, OSSL_EVENT *event,
  103. uint32_t type, uint32_t priority,
  104. OSSL_TIME when, void *ctx,
  105. void *payload, size_t payload_size);
  106. /*
  107. * Delete an event from the queue.
  108. * This will cause the early deletion function to be called if it is non-NULL.
  109. * A pointer to the event structure is returned.
  110. */
  111. int ossl_event_queue_remove(OSSL_EVENT_QUEUE *queue, OSSL_EVENT *event);
  112. /*
  113. * Free a dynamic event.
  114. * Is a NOP for a static event.
  115. */
  116. void ossl_event_free(OSSL_EVENT *event);
  117. /*
  118. * Return the time until the next event for the specified event, if the event's
  119. * time is past, zero is returned. Once activated, the event reference becomes
  120. * invalid and this function becomes undefined.
  121. */
  122. OSSL_TIME ossl_event_time_until(const OSSL_EVENT *event);
  123. /*
  124. * Return the time until the next event in the queue.
  125. * If the next event is in the past, zero is returned.
  126. */
  127. OSSL_TIME ossl_event_queue_time_until_next(const OSSL_EVENT_QUEUE *queue);
  128. /*
  129. * Postpone an event to trigger at the specified time.
  130. * If the event has triggered, this function's behaviour is undefined.
  131. */
  132. int ossl_event_queue_postpone_until(OSSL_EVENT_QUEUE *queue,
  133. OSSL_EVENT *event,
  134. OSSL_TIME when);
  135. /*
  136. * Return the next event to process.
  137. */
  138. int ossl_event_queue_get1_next_event(OSSL_EVENT_QUEUE *queue,
  139. OSSL_EVENT **event);
  140. #endif