queue.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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:48 zhangqianfu The first version
  37. *
  38. */
  39. #ifndef QUEUE_H
  40. #define QUEUE_H
  41. #include "cmsis_os2.h"
  42. //////////////////////////////LOOK OUT//////////////////////////////////////////
  43. ///this is a common queue, which means first in first out, if you want to specified
  44. ///its priority,please use message box
  45. ////////////////////////////////////////////////////////////////////////////////
  46. typedef struct
  47. {
  48. const char *name; ///< queue name
  49. int msg_buflen; ///< how many message could be cached
  50. void **msg_buf; ///< the message buf
  51. int cur_write; ///< point to the position to write
  52. int cur_read; ///< point to the position to read
  53. int msg_num; ///< how many message has been cached
  54. ///< the multi thread dealer, you could config it or not
  55. int sync_mode; ///< which means we do the sync or not
  56. osSemaphoreId_t sync_read; ///< read will pend here if no message here
  57. osSemaphoreId_t sync_write; ///< write will pend here if no space here
  58. osMutexId_t lock; ///< used to lock the queue operation
  59. }queue_t;
  60. /**
  61. * @brief: use this function to create a queue to communicate with other thread.
  62. * @param: name, this is a queue name
  63. * @param: len, this means how many message could be cached in the queue
  64. * @param: syncmode, if set to 1, it will create means use semp and mutex in the queue,
  65. * and this queue type could not be used in the interrupt; else you do the synchronize your self
  66. *
  67. * @return: the queue you created or NULL failed for some reason
  68. * */
  69. queue_t* queue_create(const char *name,int len,int syncmode);
  70. /**
  71. * @brief: use this function to push the message to the queue
  72. * @param: queue, the destination queue
  73. * @param: data, the data you want to cached
  74. * @param: timeout, if any space, we would not wait; else we will wait until any space release within the timeout
  75. * and its unit is mini second
  76. *
  77. * @return:0 success while -1 failed (timeout)
  78. * */
  79. int queue_push(queue_t *queue,void *data,int timeout);
  80. /**
  81. * @brief: use this function to pop a message from the queue
  82. * @param: queue, the source queue
  83. * @param: buf, used to storage the data we cached
  84. * @param: timeout, if any message, we would not wait; else we will wait until any message cached within the timeout
  85. * and its unit is mini second
  86. * @return:the data you cached
  87. * */
  88. int queue_pop(queue_t *queue,void **buf,int timeout);
  89. /**
  90. * @brief: use this function delete the queue you created
  91. * @param: queue, the queue we want to delete
  92. * @return: 0 success while -1 failed
  93. * */
  94. int queue_delete(queue_t *queue);
  95. #endif /* QUEUE_H */