bh_queue.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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. #include "bh_thread.h"
  7. #include "bh_time.h"
  8. #include "bh_common.h"
  9. typedef struct _bh_queue_node {
  10. struct _bh_queue_node * next;
  11. struct _bh_queue_node * prev;
  12. unsigned short tag;
  13. unsigned int len;
  14. void * body;
  15. bh_msg_cleaner msg_cleaner;
  16. } bh_queue_node;
  17. struct bh_queue {
  18. bh_queue_mutex queue_lock;
  19. bh_queue_cond queue_wait_cond;
  20. unsigned int cnt;
  21. unsigned int max;
  22. unsigned int drops;
  23. bh_queue_node * head;
  24. bh_queue_node * tail;
  25. bool exit_loop_run;
  26. };
  27. char * bh_message_payload(bh_message_t message)
  28. {
  29. return message->body;
  30. }
  31. uint32 bh_message_payload_len(bh_message_t message)
  32. {
  33. return message->len;
  34. }
  35. int 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 bh_queue_destroy(bh_queue *queue)
  62. {
  63. bh_queue_node *node;
  64. if (!queue)
  65. return;
  66. bh_queue_mutex_lock(&queue->queue_lock);
  67. while (queue->head) {
  68. node = queue->head;
  69. queue->head = node->next;
  70. bh_free_msg(node);
  71. }
  72. bh_queue_mutex_unlock(&queue->queue_lock);
  73. bh_queue_cond_destroy(&queue->queue_wait_cond);
  74. bh_queue_mutex_destroy(&queue->queue_lock);
  75. bh_queue_free(queue);
  76. }
  77. bool bh_post_msg2(bh_queue *queue, bh_queue_node *msg)
  78. {
  79. if (queue->cnt >= queue->max) {
  80. queue->drops++;
  81. bh_free_msg(msg);
  82. return false;
  83. }
  84. bh_queue_mutex_lock(&queue->queue_lock);
  85. if (queue->cnt == 0) {
  86. bh_assert(queue->head == NULL);
  87. bh_assert(queue->tail == NULL);
  88. queue->head = queue->tail = msg;
  89. msg->next = msg->prev = NULL;
  90. queue->cnt = 1;
  91. bh_queue_cond_signal(&queue->queue_wait_cond);
  92. } else {
  93. msg->next = NULL;
  94. msg->prev = queue->tail;
  95. queue->tail->next = msg;
  96. queue->tail = msg;
  97. queue->cnt++;
  98. }
  99. bh_queue_mutex_unlock(&queue->queue_lock);
  100. return true;
  101. }
  102. bool bh_post_msg(bh_queue *queue, unsigned short tag, void *body,
  103. unsigned int len)
  104. {
  105. bh_queue_node *msg = bh_new_msg(tag, body, len, NULL);
  106. if (msg == NULL) {
  107. queue->drops++;
  108. if (len != 0 && body)
  109. BH_FREE(body);
  110. return false;
  111. }
  112. if (!bh_post_msg2(queue, msg)) {
  113. // bh_post_msg2 already freed the msg for failure
  114. return false;
  115. }
  116. return true;
  117. }
  118. bh_queue_node * bh_new_msg(unsigned short tag, void *body, unsigned int len,
  119. void * handler)
  120. {
  121. bh_queue_node *msg = (bh_queue_node*) bh_queue_malloc(
  122. sizeof(bh_queue_node));
  123. if (msg == NULL)
  124. return NULL;
  125. memset(msg, 0, sizeof(bh_queue_node));
  126. msg->len = len;
  127. msg->body = body;
  128. msg->tag = tag;
  129. msg->msg_cleaner = (bh_msg_cleaner) handler;
  130. return msg;
  131. }
  132. void bh_free_msg(bh_queue_node *msg)
  133. {
  134. if (msg->msg_cleaner) {
  135. msg->msg_cleaner(msg->body);
  136. bh_queue_free(msg);
  137. return;
  138. }
  139. // note: sometime we just use the payload pointer for a integer value
  140. // len!=0 is the only indicator about the body is an allocated buffer.
  141. if (msg->body && msg->len)
  142. bh_queue_free(msg->body);
  143. bh_queue_free(msg);
  144. }
  145. bh_message_t bh_get_msg(bh_queue *queue, int timeout)
  146. {
  147. bh_queue_node *msg = NULL;
  148. bh_queue_mutex_lock(&queue->queue_lock);
  149. if (queue->cnt == 0) {
  150. bh_assert(queue->head == NULL);
  151. bh_assert(queue->tail == NULL);
  152. if (timeout == 0) {
  153. bh_queue_mutex_unlock(&queue->queue_lock);
  154. return NULL;
  155. }
  156. bh_queue_cond_timedwait(&queue->queue_wait_cond, &queue->queue_lock,
  157. timeout);
  158. }
  159. if (queue->cnt == 0) {
  160. bh_assert(queue->head == NULL);
  161. bh_assert(queue->tail == NULL);
  162. } else if (queue->cnt == 1) {
  163. bh_assert(queue->head == queue->tail);
  164. msg = queue->head;
  165. queue->head = queue->tail = NULL;
  166. queue->cnt = 0;
  167. } else {
  168. msg = queue->head;
  169. queue->head = queue->head->next;
  170. queue->head->prev = NULL;
  171. queue->cnt--;
  172. }
  173. bh_queue_mutex_unlock(&queue->queue_lock);
  174. return msg;
  175. }
  176. unsigned bh_queue_get_message_count(bh_queue *queue)
  177. {
  178. if (!queue)
  179. return 0;
  180. return queue->cnt;
  181. }
  182. void bh_queue_enter_loop_run(bh_queue *queue,
  183. bh_queue_handle_msg_callback handle_cb,
  184. void *arg)
  185. {
  186. if (!queue)
  187. return;
  188. while (!queue->exit_loop_run) {
  189. bh_queue_node * message = bh_get_msg(queue, (int)BH_WAIT_FOREVER);
  190. if (message) {
  191. handle_cb(message, arg);
  192. bh_free_msg(message);
  193. }
  194. }
  195. }
  196. void bh_queue_exit_loop_run(bh_queue *queue)
  197. {
  198. if (queue) {
  199. queue->exit_loop_run = true;
  200. bh_queue_cond_signal(&queue->queue_wait_cond);
  201. }
  202. }