PacketPassPriorityQueue.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /**
  2. * @file PacketPassPriorityQueue.h
  3. * @author Ambroz Bizjak <ambrop7@gmail.com>
  4. *
  5. * @section LICENSE
  6. *
  7. * This file is part of BadVPN.
  8. *
  9. * BadVPN is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2
  11. * as published by the Free Software Foundation.
  12. *
  13. * BadVPN is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License along
  19. * with this program; if not, write to the Free Software Foundation, Inc.,
  20. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  21. *
  22. * @section DESCRIPTION
  23. *
  24. * Priority queue using {@link PacketPassInterface}.
  25. */
  26. #ifndef BADVPN_FLOW_PACKETPASSPRIORITYQUEUE_H
  27. #define BADVPN_FLOW_PACKETPASSPRIORITYQUEUE_H
  28. #include <stdint.h>
  29. #include <system/DebugObject.h>
  30. #include <system/BPending.h>
  31. #include <misc/debugcounter.h>
  32. #include <structure/BHeap.h>
  33. #include <flow/PacketPassInterface.h>
  34. typedef void (*PacketPassPriorityQueue_handler_busy) (void *user);
  35. struct PacketPassPriorityQueueFlow_s;
  36. /**
  37. * Priority queue using {@link PacketPassInterface}.
  38. */
  39. typedef struct {
  40. PacketPassInterface *output;
  41. struct PacketPassPriorityQueueFlow_s *sending_flow;
  42. int sending_len;
  43. BHeap queued_heap;
  44. int freeing;
  45. int use_cancel;
  46. BPending schedule_job;
  47. BPendingGroup *pg;
  48. DebugCounter d_ctr;
  49. DebugObject d_obj;
  50. } PacketPassPriorityQueue;
  51. typedef struct PacketPassPriorityQueueFlow_s {
  52. PacketPassPriorityQueue *m;
  53. PacketPassPriorityQueue_handler_busy handler_busy;
  54. void *user;
  55. PacketPassInterface input;
  56. int priority;
  57. int is_queued;
  58. struct {
  59. BHeapNode heap_node;
  60. uint8_t *data;
  61. int data_len;
  62. } queued;
  63. DebugObject d_obj;
  64. } PacketPassPriorityQueueFlow;
  65. /**
  66. * Initializes the queue.
  67. *
  68. * @param m the object
  69. * @param output output interface
  70. * @param pg pending group
  71. * @param use_cancel whether cancel functionality is required. Must be 0 or 1.
  72. * If 1, output must support cancel functionality.
  73. */
  74. void PacketPassPriorityQueue_Init (PacketPassPriorityQueue *m, PacketPassInterface *output, BPendingGroup *pg, int use_cancel);
  75. /**
  76. * Frees the queue.
  77. * All flows must have been freed.
  78. *
  79. * @param m the object
  80. */
  81. void PacketPassPriorityQueue_Free (PacketPassPriorityQueue *m);
  82. /**
  83. * Prepares for freeing the entire queue. Must be called to allow freeing
  84. * the flows in the process of freeing the entire queue.
  85. * After this function is called, flows and the queue must be freed
  86. * before any further I/O.
  87. * May be called multiple times.
  88. * The queue enters freeing state.
  89. *
  90. * @param m the object
  91. */
  92. void PacketPassPriorityQueue_PrepareFree (PacketPassPriorityQueue *m);
  93. /**
  94. * Initializes a queue flow.
  95. * Queue must not be in freeing state.
  96. * Must not be called from queue calls to output.
  97. *
  98. * @param flow the object
  99. * @param m queue to attach to
  100. * @param priority flow priority. Lower value means higher priority.
  101. */
  102. void PacketPassPriorityQueueFlow_Init (PacketPassPriorityQueueFlow *flow, PacketPassPriorityQueue *m, int priority);
  103. /**
  104. * Frees a queue flow.
  105. * Unless the queue is in freeing state:
  106. * - The flow must not be busy as indicated by {@link PacketPassPriorityQueueFlow_IsBusy}.
  107. * - Must not be called from queue calls to output.
  108. *
  109. * @param flow the object
  110. */
  111. void PacketPassPriorityQueueFlow_Free (PacketPassPriorityQueueFlow *flow);
  112. /**
  113. * Does nothing.
  114. * It must be possible to free the flow (see {@link PacketPassPriorityQueueFlow}).
  115. *
  116. * @param flow the object
  117. */
  118. void PacketPassPriorityQueueFlow_AssertFree (PacketPassPriorityQueueFlow *flow);
  119. /**
  120. * Determines if the flow is busy. If the flow is considered busy, it must not
  121. * be freed.
  122. * Queue must not be in freeing state.
  123. * Must not be called from queue calls to output.
  124. *
  125. * @param flow the object
  126. * @return 0 if not busy, 1 is busy
  127. */
  128. int PacketPassPriorityQueueFlow_IsBusy (PacketPassPriorityQueueFlow *flow);
  129. /**
  130. * Cancels the packet that is currently being sent to output in order
  131. * to allow freeing the flow.
  132. * Cancel functionality must be enabled for the queue.
  133. * The flow must be busy as indicated by {@link PacketPassPriorityQueueFlow_IsBusy}.
  134. * Queue must not be in freeing state.
  135. * Must not be called from queue calls to output.
  136. * Will call Cancel on output. Will not invoke any input I/O.
  137. * After this, {@link PacketPassPriorityQueueFlow_IsBusy} will report the flow as not busy.
  138. * The flow's input's Done will never be called (the flow will become inoperable).
  139. *
  140. * @param flow the object
  141. */
  142. void PacketPassPriorityQueueFlow_Release (PacketPassPriorityQueueFlow *flow);
  143. /**
  144. * Sets up a callback to be called when the flow is no longer busy.
  145. * The flow must be busy as indicated by {@link PacketPassPriorityQueueFlow_IsBusy}.
  146. * Queue must not be in freeing state.
  147. * Must not be called from queue calls to output.
  148. *
  149. * @param flow the object
  150. * @param handler callback function. NULL to disable.
  151. * @param user value passed to callback function. Ignored if handler is NULL.
  152. */
  153. void PacketPassPriorityQueueFlow_SetBusyHandler (PacketPassPriorityQueueFlow *flow, PacketPassPriorityQueue_handler_busy handler, void *user);
  154. /**
  155. * Returns the input interface of the flow.
  156. *
  157. * @param flow the object
  158. * @return input interface
  159. */
  160. PacketPassInterface * PacketPassPriorityQueueFlow_GetInput (PacketPassPriorityQueueFlow *flow);
  161. #endif