PacketPassPriorityQueue.c 8.0 KB

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