PacketPassPriorityQueue.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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 int_comparator (void *user, int *prio1, int *prio2)
  35. {
  36. return B_COMPARE(*prio1, *prio2);
  37. }
  38. static void schedule (PacketPassPriorityQueue *m)
  39. {
  40. ASSERT(!m->sending_flow)
  41. ASSERT(!m->freeing)
  42. ASSERT(BHeap_GetFirst(&m->queued_heap))
  43. // get first queued flow
  44. PacketPassPriorityQueueFlow *qflow = UPPER_OBJECT(BHeap_GetFirst(&m->queued_heap), PacketPassPriorityQueueFlow, queued.heap_node);
  45. ASSERT(qflow->is_queued)
  46. // remove flow from queue
  47. BHeap_Remove(&m->queued_heap, &qflow->queued.heap_node);
  48. qflow->is_queued = 0;
  49. // schedule send
  50. PacketPassInterface_Sender_Send(m->output, qflow->queued.data, qflow->queued.data_len);
  51. m->sending_flow = qflow;
  52. }
  53. static void schedule_job_handler (PacketPassPriorityQueue *m)
  54. {
  55. ASSERT(!m->sending_flow)
  56. ASSERT(!m->freeing)
  57. DebugObject_Access(&m->d_obj);
  58. if (BHeap_GetFirst(&m->queued_heap)) {
  59. schedule(m);
  60. }
  61. }
  62. static void input_handler_send (PacketPassPriorityQueueFlow *flow, uint8_t *data, int data_len)
  63. {
  64. PacketPassPriorityQueue *m = flow->m;
  65. ASSERT(flow != m->sending_flow)
  66. ASSERT(!flow->is_queued)
  67. ASSERT(!m->freeing)
  68. DebugObject_Access(&flow->d_obj);
  69. // queue flow
  70. flow->queued.data = data;
  71. flow->queued.data_len = data_len;
  72. BHeap_Insert(&m->queued_heap, &flow->queued.heap_node);
  73. flow->is_queued = 1;
  74. if (!m->sending_flow && !BPending_IsSet(&m->schedule_job)) {
  75. schedule(m);
  76. }
  77. }
  78. static void output_handler_done (PacketPassPriorityQueue *m)
  79. {
  80. ASSERT(m->sending_flow)
  81. ASSERT(!BPending_IsSet(&m->schedule_job))
  82. ASSERT(!m->freeing)
  83. ASSERT(!m->sending_flow->is_queued)
  84. PacketPassPriorityQueueFlow *flow = m->sending_flow;
  85. // sending finished
  86. m->sending_flow = NULL;
  87. // schedule schedule
  88. BPending_Set(&m->schedule_job);
  89. // finish flow packet
  90. PacketPassInterface_Done(&flow->input);
  91. // call busy handler if set
  92. if (flow->handler_busy) {
  93. // handler is one-shot, unset it before calling
  94. PacketPassPriorityQueue_handler_busy handler = flow->handler_busy;
  95. flow->handler_busy = NULL;
  96. // call handler
  97. handler(flow->user);
  98. return;
  99. }
  100. }
  101. void PacketPassPriorityQueue_Init (PacketPassPriorityQueue *m, PacketPassInterface *output, BPendingGroup *pg, int use_cancel)
  102. {
  103. ASSERT(use_cancel == 0 || use_cancel == 1)
  104. ASSERT(!use_cancel || PacketPassInterface_HasCancel(output))
  105. // init arguments
  106. m->output = output;
  107. m->pg = pg;
  108. m->use_cancel = use_cancel;
  109. // init output
  110. PacketPassInterface_Sender_Init(m->output, (PacketPassInterface_handler_done)output_handler_done, m);
  111. // not sending
  112. m->sending_flow = NULL;
  113. // init queued heap
  114. BHeap_Init(&m->queued_heap, OFFSET_DIFF(PacketPassPriorityQueueFlow, priority, queued.heap_node), (BHeap_comparator)int_comparator, NULL);
  115. // not freeing
  116. m->freeing = 0;
  117. // init schedule job
  118. BPending_Init(&m->schedule_job, m->pg, (BPending_handler)schedule_job_handler, m);
  119. DebugObject_Init(&m->d_obj);
  120. DebugCounter_Init(&m->d_ctr);
  121. }
  122. void PacketPassPriorityQueue_Free (PacketPassPriorityQueue *m)
  123. {
  124. ASSERT(!BHeap_GetFirst(&m->queued_heap))
  125. ASSERT(!m->sending_flow)
  126. DebugCounter_Free(&m->d_ctr);
  127. DebugObject_Free(&m->d_obj);
  128. // free schedule job
  129. BPending_Free(&m->schedule_job);
  130. }
  131. void PacketPassPriorityQueue_PrepareFree (PacketPassPriorityQueue *m)
  132. {
  133. DebugObject_Access(&m->d_obj);
  134. // set freeing
  135. m->freeing = 1;
  136. }
  137. int PacketPassPriorityQueue_GetMTU (PacketPassPriorityQueue *m)
  138. {
  139. DebugObject_Access(&m->d_obj);
  140. return PacketPassInterface_GetMTU(m->output);
  141. }
  142. void PacketPassPriorityQueueFlow_Init (PacketPassPriorityQueueFlow *flow, PacketPassPriorityQueue *m, int priority)
  143. {
  144. ASSERT(!m->freeing)
  145. DebugObject_Access(&m->d_obj);
  146. // init arguments
  147. flow->m = m;
  148. flow->priority = priority;
  149. // have no canfree handler
  150. flow->handler_busy = NULL;
  151. // init input
  152. PacketPassInterface_Init(&flow->input, PacketPassInterface_GetMTU(flow->m->output), (PacketPassInterface_handler_send)input_handler_send, flow, m->pg);
  153. // is not queued
  154. flow->is_queued = 0;
  155. DebugObject_Init(&flow->d_obj);
  156. DebugCounter_Increment(&m->d_ctr);
  157. }
  158. void PacketPassPriorityQueueFlow_Free (PacketPassPriorityQueueFlow *flow)
  159. {
  160. PacketPassPriorityQueue *m = flow->m;
  161. ASSERT(m->freeing || flow != m->sending_flow)
  162. DebugCounter_Decrement(&m->d_ctr);
  163. DebugObject_Free(&flow->d_obj);
  164. // remove from current flow
  165. if (flow == m->sending_flow) {
  166. m->sending_flow = NULL;
  167. }
  168. // remove from queue
  169. if (flow->is_queued) {
  170. BHeap_Remove(&m->queued_heap, &flow->queued.heap_node);
  171. }
  172. // free input
  173. PacketPassInterface_Free(&flow->input);
  174. }
  175. void PacketPassPriorityQueueFlow_AssertFree (PacketPassPriorityQueueFlow *flow)
  176. {
  177. PacketPassPriorityQueue *m = flow->m;
  178. ASSERT(m->freeing || flow != m->sending_flow)
  179. DebugObject_Access(&flow->d_obj);
  180. }
  181. int PacketPassPriorityQueueFlow_IsBusy (PacketPassPriorityQueueFlow *flow)
  182. {
  183. PacketPassPriorityQueue *m = flow->m;
  184. ASSERT(!m->freeing)
  185. DebugObject_Access(&flow->d_obj);
  186. return (flow == m->sending_flow);
  187. }
  188. void PacketPassPriorityQueueFlow_RequestCancel (PacketPassPriorityQueueFlow *flow)
  189. {
  190. PacketPassPriorityQueue *m = flow->m;
  191. ASSERT(flow == m->sending_flow)
  192. ASSERT(m->use_cancel)
  193. ASSERT(!m->freeing)
  194. ASSERT(!BPending_IsSet(&m->schedule_job))
  195. DebugObject_Access(&flow->d_obj);
  196. // request cancel
  197. PacketPassInterface_Sender_RequestCancel(m->output);
  198. }
  199. void PacketPassPriorityQueueFlow_SetBusyHandler (PacketPassPriorityQueueFlow *flow, PacketPassPriorityQueue_handler_busy handler, void *user)
  200. {
  201. PacketPassPriorityQueue *m = flow->m;
  202. ASSERT(flow == m->sending_flow)
  203. ASSERT(!m->freeing)
  204. DebugObject_Access(&flow->d_obj);
  205. // set handler
  206. flow->handler_busy = handler;
  207. flow->user = user;
  208. }
  209. PacketPassInterface * PacketPassPriorityQueueFlow_GetInput (PacketPassPriorityQueueFlow *flow)
  210. {
  211. DebugObject_Access(&flow->d_obj);
  212. return &flow->input;
  213. }