bh_queue.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. /*
  2. * Copyright (C) 2019 Intel Corporation. All rights reserved.
  3. * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. */
  5. #include "bh_queue.h"
  6. typedef struct bh_queue_node {
  7. struct bh_queue_node *next;
  8. struct bh_queue_node *prev;
  9. unsigned short tag;
  10. unsigned int len;
  11. void *body;
  12. bh_msg_cleaner msg_cleaner;
  13. } bh_queue_node;
  14. struct bh_queue {
  15. bh_queue_mutex queue_lock;
  16. bh_queue_cond queue_wait_cond;
  17. unsigned int cnt;
  18. unsigned int max;
  19. unsigned int drops;
  20. bh_queue_node *head;
  21. bh_queue_node *tail;
  22. bool exit_loop_run;
  23. };
  24. char *
  25. bh_message_payload(bh_message_t message)
  26. {
  27. return message->body;
  28. }
  29. uint32
  30. bh_message_payload_len(bh_message_t message)
  31. {
  32. return message->len;
  33. }
  34. int
  35. bh_message_type(bh_message_t message)
  36. {
  37. return message->tag;
  38. }
  39. bh_queue *
  40. bh_queue_create()
  41. {
  42. int ret;
  43. bh_queue *queue = bh_queue_malloc(sizeof(bh_queue));
  44. if (queue) {
  45. memset(queue, 0, sizeof(bh_queue));
  46. queue->max = DEFAULT_QUEUE_LENGTH;
  47. ret = bh_queue_mutex_init(&queue->queue_lock);
  48. if (ret != 0) {
  49. bh_queue_free(queue);
  50. return NULL;
  51. }
  52. ret = bh_queue_cond_init(&queue->queue_wait_cond);
  53. if (ret != 0) {
  54. bh_queue_mutex_destroy(&queue->queue_lock);
  55. bh_queue_free(queue);
  56. return NULL;
  57. }
  58. }
  59. return queue;
  60. }
  61. void
  62. bh_queue_destroy(bh_queue *queue)
  63. {
  64. bh_queue_node *node;
  65. if (!queue)
  66. return;
  67. bh_queue_mutex_lock(&queue->queue_lock);
  68. while (queue->head) {
  69. node = queue->head;
  70. queue->head = node->next;
  71. bh_free_msg(node);
  72. }
  73. bh_queue_mutex_unlock(&queue->queue_lock);
  74. bh_queue_cond_destroy(&queue->queue_wait_cond);
  75. bh_queue_mutex_destroy(&queue->queue_lock);
  76. bh_queue_free(queue);
  77. }
  78. bool
  79. bh_post_msg2(bh_queue *queue, bh_queue_node *msg)
  80. {
  81. if (queue->cnt >= queue->max) {
  82. queue->drops++;
  83. bh_free_msg(msg);
  84. return false;
  85. }
  86. bh_queue_mutex_lock(&queue->queue_lock);
  87. if (queue->cnt == 0) {
  88. bh_assert(queue->head == NULL);
  89. bh_assert(queue->tail == NULL);
  90. queue->head = queue->tail = msg;
  91. msg->next = msg->prev = NULL;
  92. queue->cnt = 1;
  93. bh_queue_cond_signal(&queue->queue_wait_cond);
  94. }
  95. else {
  96. msg->next = NULL;
  97. msg->prev = queue->tail;
  98. queue->tail->next = msg;
  99. queue->tail = msg;
  100. queue->cnt++;
  101. }
  102. bh_queue_mutex_unlock(&queue->queue_lock);
  103. return true;
  104. }
  105. bool
  106. bh_post_msg(bh_queue *queue, unsigned short tag, void *body, unsigned int len)
  107. {
  108. bh_queue_node *msg = bh_new_msg(tag, body, len, NULL);
  109. if (msg == NULL) {
  110. queue->drops++;
  111. if (len != 0 && body)
  112. BH_FREE(body);
  113. return false;
  114. }
  115. if (!bh_post_msg2(queue, msg)) {
  116. // bh_post_msg2 already freed the msg for failure
  117. return false;
  118. }
  119. return true;
  120. }
  121. bh_queue_node *
  122. bh_new_msg(unsigned short tag, void *body, unsigned int len, void *handler)
  123. {
  124. bh_queue_node *msg =
  125. (bh_queue_node *)bh_queue_malloc(sizeof(bh_queue_node));
  126. if (msg == NULL)
  127. return NULL;
  128. memset(msg, 0, sizeof(bh_queue_node));
  129. msg->len = len;
  130. msg->body = body;
  131. msg->tag = tag;
  132. msg->msg_cleaner = (bh_msg_cleaner)handler;
  133. return msg;
  134. }
  135. void
  136. bh_free_msg(bh_queue_node *msg)
  137. {
  138. if (msg->msg_cleaner) {
  139. msg->msg_cleaner(msg->body);
  140. bh_queue_free(msg);
  141. return;
  142. }
  143. // note: sometimes we just use the payload pointer for an integer value
  144. // len!=0 is the only indicator about the body is an allocated buffer.
  145. if (msg->body && msg->len)
  146. bh_queue_free(msg->body);
  147. bh_queue_free(msg);
  148. }
  149. bh_message_t
  150. bh_get_msg(bh_queue *queue, uint64 timeout_us)
  151. {
  152. bh_queue_node *msg = NULL;
  153. bh_queue_mutex_lock(&queue->queue_lock);
  154. if (queue->cnt == 0) {
  155. bh_assert(queue->head == NULL);
  156. bh_assert(queue->tail == NULL);
  157. if (timeout_us == 0) {
  158. bh_queue_mutex_unlock(&queue->queue_lock);
  159. return NULL;
  160. }
  161. bh_queue_cond_timedwait(&queue->queue_wait_cond, &queue->queue_lock,
  162. timeout_us);
  163. }
  164. if (queue->cnt == 0) {
  165. bh_assert(queue->head == NULL);
  166. bh_assert(queue->tail == NULL);
  167. }
  168. else if (queue->cnt == 1) {
  169. bh_assert(queue->head == queue->tail);
  170. msg = queue->head;
  171. queue->head = queue->tail = NULL;
  172. queue->cnt = 0;
  173. }
  174. else {
  175. msg = queue->head;
  176. queue->head = queue->head->next;
  177. queue->head->prev = NULL;
  178. queue->cnt--;
  179. }
  180. bh_queue_mutex_unlock(&queue->queue_lock);
  181. return msg;
  182. }
  183. unsigned
  184. bh_queue_get_message_count(bh_queue *queue)
  185. {
  186. if (!queue)
  187. return 0;
  188. return queue->cnt;
  189. }
  190. void
  191. bh_queue_enter_loop_run(bh_queue *queue, bh_queue_handle_msg_callback handle_cb,
  192. void *arg)
  193. {
  194. if (!queue)
  195. return;
  196. while (!queue->exit_loop_run) {
  197. bh_queue_node *message = bh_get_msg(queue, BHT_WAIT_FOREVER);
  198. if (message) {
  199. handle_cb(message, arg);
  200. bh_free_msg(message);
  201. }
  202. }
  203. }
  204. void
  205. bh_queue_exit_loop_run(bh_queue *queue)
  206. {
  207. if (queue) {
  208. queue->exit_loop_run = true;
  209. bh_queue_cond_signal(&queue->queue_wait_cond);
  210. }
  211. }