queue.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. /*----------------------------------------------------------------------------
  2. * Copyright (c) <2018>, <Huawei Technologies Co., Ltd>
  3. * All rights reserved.
  4. * Redistribution and use in source and binary forms, with or without modification,
  5. * are permitted provided that the following conditions are met:
  6. * 1. Redistributions of source code must retain the above copyright notice, this list of
  7. * conditions and the following disclaimer.
  8. * 2. Redistributions in binary form must reproduce the above copyright notice, this list
  9. * of conditions and the following disclaimer in the documentation and/or other materials
  10. * provided with the distribution.
  11. * 3. Neither the name of the copyright holder nor the names of its contributors may be used
  12. * to endorse or promote products derived from this software without specific prior written
  13. * permission.
  14. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  15. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  16. * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  17. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
  18. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  19. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  20. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  21. * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  22. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  23. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  24. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. *---------------------------------------------------------------------------*/
  26. /*----------------------------------------------------------------------------
  27. * Notice of Export Control Law
  28. * ===============================================
  29. * Huawei LiteOS may be subject to applicable export control laws and regulations, which might
  30. * include those applicable to Huawei LiteOS of U.S. and the country in which you are located.
  31. * Import, export and usage of Huawei LiteOS in any manner by you shall be in compliance with such
  32. * applicable export control laws and regulations.
  33. *---------------------------------------------------------------------------*/
  34. /**
  35. * DATE AUTHOR INSTRUCTION
  36. * 2019-09-16 18:27 zhangqianfu The first version
  37. *
  38. */
  39. #include <stdlib.h>
  40. #include <stdio.h>
  41. #include <string.h>
  42. #include "cmsis_os2.h"
  43. #include "queue.h"
  44. queue_t* queue_create(const char *name,int len,int syncmode)
  45. {
  46. queue_t *ret;
  47. ret = malloc(sizeof(queue_t) + len*sizeof(void *));
  48. if(NULL == ret)
  49. {
  50. goto EXIT_MEM;
  51. }
  52. (void) memset(ret,0,sizeof(queue_t));
  53. ret->name = name;
  54. ret->msg_buflen = len;
  55. ret->msg_buf = (void **)(uintptr_t)((uint8_t *)ret + sizeof(queue_t));
  56. ret->sync_mode = syncmode;
  57. if(0 == syncmode)
  58. {
  59. goto EXIT_OK;
  60. }
  61. ret->sync_read = osSemaphoreNew(len, 0, NULL);
  62. if(ret->sync_read == NULL)
  63. {
  64. goto EXIT_SYNCREAD;
  65. }
  66. ret->sync_write = osSemaphoreNew(len, len, NULL);
  67. if(ret->sync_write == NULL)
  68. {
  69. goto EXIT_SYNCWRITE;
  70. }
  71. ret->lock = osMutexNew(NULL);
  72. if(ret->lock == NULL)
  73. {
  74. goto EXIT_LOCK;
  75. }
  76. EXIT_OK:
  77. return ret;
  78. EXIT_LOCK:
  79. (void) osSemaphoreDelete(ret->sync_write);
  80. EXIT_SYNCWRITE:
  81. (void) osSemaphoreDelete(ret->sync_read);
  82. EXIT_SYNCREAD:
  83. free(ret);
  84. ret = NULL;
  85. EXIT_MEM:
  86. ret = NULL;
  87. return ret;
  88. }
  89. static int raw_queue_pushdata(queue_t *queue,void *data)
  90. {
  91. int ret = -1;
  92. if(queue->msg_num < queue->msg_buflen)
  93. {
  94. queue->msg_buf[queue->cur_write] = data;
  95. queue->cur_write = (queue->cur_write + 1)%queue->msg_buflen;
  96. queue->msg_num ++;
  97. ret = 0;
  98. }
  99. return ret;
  100. }
  101. ///< push a data to the queue, 0 success while -1 failed
  102. int queue_push(queue_t *queue,void *data,int timeout)
  103. {
  104. int ret = -1;
  105. if((NULL == queue) || (NULL == data))
  106. {
  107. return ret;
  108. }
  109. if(queue->sync_mode)
  110. {
  111. ret = osSemaphoreAcquire(queue->sync_write,timeout);
  112. if(ret == osOK)
  113. {
  114. ret =osMutexAcquire(queue->lock,osWaitForever);
  115. if(ret == osOK)
  116. {
  117. ret = raw_queue_pushdata(queue,data);
  118. (void) osMutexRelease(queue->lock);
  119. }
  120. if(0 == ret)
  121. {
  122. (void) osSemaphoreRelease(queue->sync_read);
  123. }
  124. }
  125. }
  126. else
  127. {
  128. ret = raw_queue_pushdata(queue,data);
  129. }
  130. return ret;
  131. }
  132. ///< pop a data from the queue
  133. static int raw_queue_pop(queue_t *queue,void **buf)
  134. {
  135. int ret = -1;
  136. if(queue->msg_num > 0)
  137. {
  138. *buf = queue->msg_buf[queue->cur_read];
  139. queue->cur_read = (queue->cur_read +1)%queue->msg_buflen;
  140. queue->msg_num--;
  141. ret = 0;
  142. }
  143. return ret;
  144. }
  145. int queue_pop(queue_t *queue,void **buf, int timeout)
  146. {
  147. int ret = -1;
  148. if((NULL == queue) || (NULL == buf))
  149. {
  150. return ret;
  151. }
  152. if(queue->sync_mode)
  153. {
  154. ret = osSemaphoreAcquire(queue->sync_read,timeout);
  155. if(ret == osOK)
  156. {
  157. ret = osMutexAcquire(queue->lock,osWaitForever);
  158. if(ret == osOK)
  159. {
  160. ret = raw_queue_pop(queue,buf);
  161. (void) osMutexRelease(queue->lock);
  162. }
  163. if(0 == ret)
  164. {
  165. (void) osSemaphoreRelease(queue->sync_write);
  166. }
  167. }
  168. }
  169. else
  170. {
  171. ret = raw_queue_pop(queue,buf);
  172. }
  173. return ret;
  174. }
  175. ///< delete the queue,if any data in the queue, which means could not kill it
  176. int queue_delete(queue_t *queue)
  177. {
  178. int ret = -1;
  179. if((NULL == queue))
  180. {
  181. return ret;
  182. }
  183. if(queue->sync_mode)
  184. {
  185. (void) osSemaphoreDelete(queue->sync_read);
  186. (void) osSemaphoreDelete(queue->sync_write);
  187. (void) osMutexDelete(queue->lock);
  188. }
  189. free(queue);
  190. ret = 0;
  191. return ret;
  192. }