xrp_threaded_queue.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. /*
  2. * Copyright (c) 2016 - 2018 Cadence Design Systems Inc.
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining
  5. * a copy of this software and associated documentation files (the
  6. * "Software"), to deal in the Software without restriction, including
  7. * without limitation the rights to use, copy, modify, merge, publish,
  8. * distribute, sublicense, and/or sell copies of the Software, and to
  9. * permit persons to whom the Software is furnished to do so, subject to
  10. * the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be included
  13. * in all copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  16. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  17. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  18. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  19. * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  20. * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  21. * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  22. */
  23. #include <stdio.h>
  24. #include "xrp_debug.h"
  25. #include "xrp_host_common.h"
  26. #include "xrp_threaded_queue.h"
  27. #include "dsp_common.h"
  28. static struct xrp_queue_item *_xrp_dequeue_request(struct xrp_request_queue *queue)
  29. {
  30. struct xrp_queue_item *rq = queue->request_queue.head;
  31. if (!rq)
  32. return NULL;
  33. if (rq == queue->request_queue.tail)
  34. queue->request_queue.tail = NULL;
  35. queue->request_queue.head = rq->next;
  36. return rq;
  37. }
  38. static int xrp_queue_process(struct xrp_request_queue *queue)
  39. {
  40. struct xrp_queue_item *rq;
  41. int exit = 0;
  42. queue->sync_exit = &exit;
  43. xrp_cond_lock(&queue->request_queue_cond);
  44. for (;;) {
  45. rq = _xrp_dequeue_request(queue);
  46. if (rq || queue->exit)
  47. break;
  48. xrp_cond_wait(&queue->request_queue_cond);
  49. }
  50. xrp_cond_unlock(&queue->request_queue_cond);
  51. if (!rq)
  52. return 0;
  53. // printf("%s,queue:%p get item\n",__FUNCTION__,queue);
  54. queue->fn(rq, queue->context);
  55. // printf("%s,queue:%p done item\n",__FUNCTION__,queue);
  56. return !exit;
  57. }
  58. static void *xrp_queue_thread(void *p)
  59. {
  60. struct xrp_request_queue *queue = p;
  61. DSP_PRINT(DEBUG,"queue:%p runing....\n",queue);
  62. while (xrp_queue_process(queue)) {
  63. }
  64. DSP_PRINT(DEBUG,"queue:%p exit\n",queue);
  65. return NULL;
  66. }
  67. void xrp_queue_init(struct xrp_request_queue *queue, int priority,
  68. void *context,
  69. void (*fn)(struct xrp_queue_item *rq, void *context))
  70. {
  71. xrp_cond_init(&queue->request_queue_cond);
  72. queue->context = context;
  73. queue->fn = fn;
  74. xrp_thread_create(&queue->thread, priority, xrp_queue_thread, queue);
  75. }
  76. void xrp_queue_destroy(struct xrp_request_queue *queue)
  77. {
  78. xrp_cond_lock(&queue->request_queue_cond);
  79. queue->exit = 1;
  80. xrp_cond_broadcast(&queue->request_queue_cond);
  81. xrp_cond_unlock(&queue->request_queue_cond);
  82. if (!xrp_thread_join(&queue->thread)) {
  83. *queue->sync_exit = 1;
  84. xrp_thread_detach(&queue->thread);
  85. DSP_PRINT(DEBUG,"queue thread release\n");
  86. }
  87. xrp_cond_lock(&queue->request_queue_cond);
  88. if (queue->request_queue.head != NULL)
  89. DSP_PRINT(DEBUG,"releasing non-empty queue\n");
  90. xrp_cond_unlock(&queue->request_queue_cond);
  91. xrp_cond_destroy(&queue->request_queue_cond);
  92. }
  93. void xrp_queue_push(struct xrp_request_queue *queue,
  94. struct xrp_queue_item *rq)
  95. {
  96. xrp_cond_lock(&queue->request_queue_cond);
  97. rq->next = NULL;
  98. if (queue->request_queue.tail) {
  99. queue->request_queue.tail->next = rq;
  100. } else {
  101. queue->request_queue.head = rq;
  102. xrp_cond_broadcast(&queue->request_queue_cond);
  103. }
  104. queue->request_queue.tail = rq;
  105. xrp_cond_unlock(&queue->request_queue_cond);
  106. }
  107. static void xrp_impl_event_init(struct xrp_event *event)
  108. {
  109. xrp_cond_init(&event->impl.cond);
  110. event->status = XRP_STATUS_PENDING;
  111. }
  112. struct xrp_event *xrp_event_create(void)
  113. {
  114. struct xrp_event *event = alloc_refcounted(sizeof(*event));
  115. if (!event)
  116. return NULL;
  117. xrp_impl_event_init(event);
  118. return event;
  119. }
  120. void xrp_wait(struct xrp_event *event, enum xrp_status *status)
  121. {
  122. xrp_cond_lock(&event->impl.cond);
  123. while (event->status == XRP_STATUS_PENDING)
  124. xrp_cond_wait(&event->impl.cond);
  125. xrp_cond_unlock(&event->impl.cond);
  126. set_status(status, XRP_STATUS_SUCCESS);
  127. // printf("%s,get event %p\n",__FUNCTION__,event);
  128. }
  129. size_t xrp_wait_any(struct xrp_event **event, size_t n_events,
  130. enum xrp_status *status)
  131. {
  132. size_t i, rv;
  133. struct xrp_event group;
  134. struct xrp_event_link *link;
  135. if (!n_events) {
  136. set_status(status, XRP_STATUS_FAILURE);
  137. return 0;
  138. }
  139. link = calloc(n_events, sizeof(struct xrp_event_link));
  140. xrp_impl_event_init(&group);
  141. for (i = 0; i < n_events; ++i) {
  142. xrp_cond_lock(&event[i]->impl.cond);
  143. if (event[i]->status == XRP_STATUS_PENDING) {
  144. link[i].group = event[i]->group;
  145. link[i].next = event[i]->link;
  146. if (event[i]->link)
  147. event[i]->link->prev = link + i;
  148. event[i]->group = &group;
  149. event[i]->link = link + i;
  150. } else {
  151. xrp_cond_unlock(&event[i]->impl.cond);
  152. break;
  153. }
  154. xrp_cond_unlock(&event[i]->impl.cond);
  155. }
  156. rv = i;
  157. if (i == n_events)
  158. xrp_wait(&group, NULL);
  159. else
  160. n_events = i;
  161. for (i = 0; i < n_events; ++i) {
  162. xrp_cond_lock(&event[i]->impl.cond);
  163. if (event[i]->group == &group) {
  164. event[i]->group = link[i].group;
  165. event[i]->link = link[i].next;
  166. }
  167. if (link[i].next) {
  168. link[i].next->prev = link[i].prev;
  169. }
  170. if (link[i].prev) {
  171. if (link[i].prev->group == &group) {
  172. link[i].prev->group = link[i].group;
  173. link[i].prev->next = link[i].next;
  174. } else {
  175. pr_debug("%s: inconsistent link state\n");
  176. }
  177. }
  178. if (event[i]->status != XRP_STATUS_PENDING)
  179. rv = i;
  180. xrp_cond_unlock(&event[i]->impl.cond);
  181. }
  182. xrp_impl_release_event(&group);
  183. free(link);
  184. set_status(status, XRP_STATUS_SUCCESS);
  185. return rv;
  186. }
  187. void xrp_impl_broadcast_event(struct xrp_event *event, enum xrp_status status)
  188. {
  189. struct xrp_event *group;
  190. struct xrp_event_link *link;
  191. // printf("%s, event %p! entry\n",__func__,event);
  192. xrp_cond_lock(&event->impl.cond);
  193. event->status = status;
  194. xrp_cond_broadcast(&event->impl.cond);
  195. group = event->group;
  196. link = event->link;
  197. while (link) {
  198. xrp_cond_lock(&group->impl.cond);
  199. group->status = status;
  200. xrp_cond_broadcast(&group->impl.cond);
  201. xrp_cond_unlock(&group->impl.cond);
  202. group = link->group;
  203. link = link->next;
  204. }
  205. xrp_cond_unlock(&event->impl.cond);
  206. // printf("%s, event %p! exit\n",__func__,event);
  207. }
  208. void xrp_impl_release_event(struct xrp_event *event)
  209. {
  210. xrp_cond_destroy(&event->impl.cond);
  211. }