PacketPassPriorityQueue.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. /**
  2. * @file PacketPassPriorityQueue.c
  3. * @author Ambroz Bizjak <ambrop7@gmail.com>
  4. *
  5. * @section LICENSE
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. Neither the name of the author nor the
  15. * names of its contributors may be used to endorse or promote products
  16. * derived from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  27. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. */
  29. #include <stdlib.h>
  30. #include <misc/debug.h>
  31. #include <misc/offset.h>
  32. #include <misc/compare.h>
  33. #include <flow/PacketPassPriorityQueue.h>
  34. static int compare_flows (PacketPassPriorityQueueFlow *f1, PacketPassPriorityQueueFlow *f2)
  35. {
  36. int cmp = B_COMPARE(f1->priority, f2->priority);
  37. if (cmp) {
  38. return cmp;
  39. }
  40. return B_COMPARE((uintptr_t)f1, (uintptr_t)f2);
  41. }
  42. #include "PacketPassPriorityQueue_tree.h"
  43. #include <structure/SAvl_impl.h>
  44. static void schedule (PacketPassPriorityQueue *m)
  45. {
  46. ASSERT(!m->sending_flow)
  47. ASSERT(!m->freeing)
  48. ASSERT(!PacketPassPriorityQueue__Tree_IsEmpty(&m->queued_tree))
  49. // get first queued flow
  50. PacketPassPriorityQueueFlow *qflow = PacketPassPriorityQueue__Tree_GetFirst(&m->queued_tree, 0);
  51. ASSERT(qflow->is_queued)
  52. // remove flow from queue
  53. PacketPassPriorityQueue__Tree_Remove(&m->queued_tree, 0, qflow);
  54. qflow->is_queued = 0;
  55. // schedule send
  56. PacketPassInterface_Sender_Send(m->output, qflow->queued.data, qflow->queued.data_len);
  57. m->sending_flow = qflow;
  58. }
  59. static void schedule_job_handler (PacketPassPriorityQueue *m)
  60. {
  61. ASSERT(!m->sending_flow)
  62. ASSERT(!m->freeing)
  63. DebugObject_Access(&m->d_obj);
  64. if (!PacketPassPriorityQueue__Tree_IsEmpty(&m->queued_tree)) {
  65. schedule(m);
  66. }
  67. }
  68. static void input_handler_send (PacketPassPriorityQueueFlow *flow, uint8_t *data, int data_len)
  69. {
  70. PacketPassPriorityQueue *m = flow->m;
  71. ASSERT(flow != m->sending_flow)
  72. ASSERT(!flow->is_queued)
  73. ASSERT(!m->freeing)
  74. DebugObject_Access(&flow->d_obj);
  75. // queue flow
  76. flow->queued.data = data;
  77. flow->queued.data_len = data_len;
  78. int res = PacketPassPriorityQueue__Tree_Insert(&m->queued_tree, 0, flow, NULL);
  79. ASSERT_EXECUTE(res)
  80. flow->is_queued = 1;
  81. if (!m->sending_flow && !BPending_IsSet(&m->schedule_job)) {
  82. schedule(m);
  83. }
  84. }
  85. static void output_handler_done (PacketPassPriorityQueue *m)
  86. {
  87. ASSERT(m->sending_flow)
  88. ASSERT(!BPending_IsSet(&m->schedule_job))
  89. ASSERT(!m->freeing)
  90. ASSERT(!m->sending_flow->is_queued)
  91. PacketPassPriorityQueueFlow *flow = m->sending_flow;
  92. // sending finished
  93. m->sending_flow = NULL;
  94. // schedule schedule
  95. BPending_Set(&m->schedule_job);
  96. // finish flow packet
  97. PacketPassInterface_Done(&flow->input);
  98. // call busy handler if set
  99. if (flow->handler_busy) {
  100. // handler is one-shot, unset it before calling
  101. PacketPassPriorityQueue_handler_busy handler = flow->handler_busy;
  102. flow->handler_busy = NULL;
  103. // call handler
  104. handler(flow->user);
  105. return;
  106. }
  107. }
  108. void PacketPassPriorityQueue_Init (PacketPassPriorityQueue *m, PacketPassInterface *output, BPendingGroup *pg, int use_cancel)
  109. {
  110. ASSERT(use_cancel == 0 || use_cancel == 1)
  111. ASSERT(!use_cancel || PacketPassInterface_HasCancel(output))
  112. // init arguments
  113. m->output = output;
  114. m->pg = pg;
  115. m->use_cancel = use_cancel;
  116. // init output
  117. PacketPassInterface_Sender_Init(m->output, (PacketPassInterface_handler_done)output_handler_done, m);
  118. // not sending
  119. m->sending_flow = NULL;
  120. // init queued tree
  121. PacketPassPriorityQueue__Tree_Init(&m->queued_tree);
  122. // not freeing
  123. m->freeing = 0;
  124. // init schedule job
  125. BPending_Init(&m->schedule_job, m->pg, (BPending_handler)schedule_job_handler, m);
  126. DebugObject_Init(&m->d_obj);
  127. DebugCounter_Init(&m->d_ctr);
  128. }
  129. void PacketPassPriorityQueue_Free (PacketPassPriorityQueue *m)
  130. {
  131. ASSERT(PacketPassPriorityQueue__Tree_IsEmpty(&m->queued_tree))
  132. ASSERT(!m->sending_flow)
  133. DebugCounter_Free(&m->d_ctr);
  134. DebugObject_Free(&m->d_obj);
  135. // free schedule job
  136. BPending_Free(&m->schedule_job);
  137. }
  138. void PacketPassPriorityQueue_PrepareFree (PacketPassPriorityQueue *m)
  139. {
  140. DebugObject_Access(&m->d_obj);
  141. // set freeing
  142. m->freeing = 1;
  143. }
  144. int PacketPassPriorityQueue_GetMTU (PacketPassPriorityQueue *m)
  145. {
  146. DebugObject_Access(&m->d_obj);
  147. return PacketPassInterface_GetMTU(m->output);
  148. }
  149. void PacketPassPriorityQueueFlow_Init (PacketPassPriorityQueueFlow *flow, PacketPassPriorityQueue *m, int priority)
  150. {
  151. ASSERT(!m->freeing)
  152. DebugObject_Access(&m->d_obj);
  153. // init arguments
  154. flow->m = m;
  155. flow->priority = priority;
  156. // have no canfree handler
  157. flow->handler_busy = NULL;
  158. // init input
  159. PacketPassInterface_Init(&flow->input, PacketPassInterface_GetMTU(flow->m->output), (PacketPassInterface_handler_send)input_handler_send, flow, m->pg);
  160. // is not queued
  161. flow->is_queued = 0;
  162. DebugObject_Init(&flow->d_obj);
  163. DebugCounter_Increment(&m->d_ctr);
  164. }
  165. void PacketPassPriorityQueueFlow_Free (PacketPassPriorityQueueFlow *flow)
  166. {
  167. PacketPassPriorityQueue *m = flow->m;
  168. ASSERT(m->freeing || flow != m->sending_flow)
  169. DebugCounter_Decrement(&m->d_ctr);
  170. DebugObject_Free(&flow->d_obj);
  171. // remove from current flow
  172. if (flow == m->sending_flow) {
  173. m->sending_flow = NULL;
  174. }
  175. // remove from queue
  176. if (flow->is_queued) {
  177. PacketPassPriorityQueue__Tree_Remove(&m->queued_tree, 0, flow);
  178. }
  179. // free input
  180. PacketPassInterface_Free(&flow->input);
  181. }
  182. void PacketPassPriorityQueueFlow_AssertFree (PacketPassPriorityQueueFlow *flow)
  183. {
  184. PacketPassPriorityQueue *m = flow->m;
  185. B_USE(m)
  186. ASSERT(m->freeing || flow != m->sending_flow)
  187. DebugObject_Access(&flow->d_obj);
  188. }
  189. int PacketPassPriorityQueueFlow_IsBusy (PacketPassPriorityQueueFlow *flow)
  190. {
  191. PacketPassPriorityQueue *m = flow->m;
  192. ASSERT(!m->freeing)
  193. DebugObject_Access(&flow->d_obj);
  194. return (flow == m->sending_flow);
  195. }
  196. void PacketPassPriorityQueueFlow_RequestCancel (PacketPassPriorityQueueFlow *flow)
  197. {
  198. PacketPassPriorityQueue *m = flow->m;
  199. ASSERT(flow == m->sending_flow)
  200. ASSERT(m->use_cancel)
  201. ASSERT(!m->freeing)
  202. ASSERT(!BPending_IsSet(&m->schedule_job))
  203. DebugObject_Access(&flow->d_obj);
  204. // request cancel
  205. PacketPassInterface_Sender_RequestCancel(m->output);
  206. }
  207. void PacketPassPriorityQueueFlow_SetBusyHandler (PacketPassPriorityQueueFlow *flow, PacketPassPriorityQueue_handler_busy handler, void *user)
  208. {
  209. PacketPassPriorityQueue *m = flow->m;
  210. B_USE(m)
  211. ASSERT(flow == m->sending_flow)
  212. ASSERT(!m->freeing)
  213. DebugObject_Access(&flow->d_obj);
  214. // set handler
  215. flow->handler_busy = handler;
  216. flow->user = user;
  217. }
  218. PacketPassInterface * PacketPassPriorityQueueFlow_GetInput (PacketPassPriorityQueueFlow *flow)
  219. {
  220. DebugObject_Access(&flow->d_obj);
  221. return &flow->input;
  222. }