xos_msgq.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. /** @file */
  2. // xos_msgq.h - XOS Message Queue API and data structures.
  3. // Copyright (c) 2003-2015 Cadence Design Systems, Inc.
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining
  6. // a copy of this software and associated documentation files (the
  7. // "Software"), to deal in the Software without restriction, including
  8. // without limitation the rights to use, copy, modify, merge, publish,
  9. // distribute, sublicense, and/or sell copies of the Software, and to
  10. // permit persons to whom the Software is furnished to do so, subject to
  11. // the following conditions:
  12. //
  13. // The above copyright notice and this permission notice shall be included
  14. // in all copies or substantial portions of the Software.
  15. //
  16. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  17. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  18. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  19. // IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  20. // CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  21. // TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  22. // SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  23. // NOTE: Do not include this file directly in your application. Including
  24. // xos.h will automatically include this file.
  25. #ifndef __XOS_MSGQ_H__
  26. #define __XOS_MSGQ_H__
  27. #include "xos_types.h"
  28. #ifdef __cplusplus
  29. extern "C" {
  30. #endif
  31. //-----------------------------------------------------------------------------
  32. // XosMsgQueue is a multi-writer multi-reader message queue implementation.
  33. // It is completely thread-safe and can be used by interrupt handlers.
  34. // Interrupt handlers are guaranteed not to block when trying to send or
  35. // receive a message. Messages are copied into the queue. The queue contains
  36. // storage for a fixed number of messages defined at queue creation time.
  37. // Messages must be a multiple of 4 bytes long (padded if necessary) and the
  38. // message buffers must be 4-byte aligned.
  39. //-----------------------------------------------------------------------------
  40. //-----------------------------------------------------------------------------
  41. // Message Queue flags.
  42. //-----------------------------------------------------------------------------
  43. #define XOS_MSGQ_WAIT_PRIORITY 0x0000 ///< Wake waiters in priority order (default)
  44. #define XOS_MSGQ_WAIT_FIFO 0x0001 ///< Wake waiters in FIFO order
  45. #define XOS_MSGQ_FULL 0x0002 // Queue is full
  46. #define XOS_MSGQ_DELETED 0x8000 // Queue is deleted
  47. //-----------------------------------------------------------------------------
  48. ///
  49. /// XosMsgQueue object.
  50. ///
  51. //-----------------------------------------------------------------------------
  52. typedef struct XosMsgQueue {
  53. uint16_t flags; ///< queue flags
  54. uint16_t count; ///< # of messages queue can hold
  55. uint32_t msize; ///< message size in bytes
  56. uint16_t head; ///< write pointer
  57. uint16_t tail; ///< read pointer
  58. XosThreadQueue readq; ///< reader wait queue
  59. XosThreadQueue writeq; ///< writer wait queue
  60. #if XOS_MSGQ_DEBUG
  61. uint32_t sig; // debug signature
  62. #endif
  63. #if XOS_OPT_MSGQ_STATS
  64. uint32_t num_send; ///< # of messages put to queue
  65. uint32_t num_recv; ///< # of messages taken from queue
  66. uint32_t num_send_blks; ///< # of times thread blocked on send
  67. uint32_t num_recv_blks; ///< # of times thread blocked on recv
  68. #endif
  69. uint32_t msg[1]; ///< first word of message buffer
  70. } XosMsgQueue;
  71. //-----------------------------------------------------------------------------
  72. ///
  73. /// Use these macros to statically or dynamically allocate a message queue.
  74. /// XOS_MSGQ_ALLOC allocates a static queue, while XOS_MSGQ_SIZE can be used
  75. /// to allocate memory via malloc() etc.
  76. ///
  77. /// Static: this allocates a queue named "testq", containing 10 messages,
  78. /// each 16 bytes long.
  79. ///
  80. /// XOS_MSGQ_ALLOC(testq, 10, 16);
  81. ///
  82. /// Dynamic: this allocates a queue named "testq", containing 10 messages,
  83. /// each 16 bytes long.
  84. ///
  85. /// XosMsgQueue * testq = malloc( XOS_MSGQ_SIZE(10, 16) );
  86. ///
  87. /// \param name The queue name, i.e. the name of the pointer
  88. /// to the queue. Used as the queue handle in
  89. /// queue API calls.
  90. ///
  91. /// \param num Number of messages to allocate in queue. Must be > 0.
  92. ///
  93. /// \param size Message size in bytes. Must be > 0 and multiple of 4.
  94. ///
  95. //-----------------------------------------------------------------------------
  96. #define XOS_MSGQ_ALLOC(name, num, size) \
  97. static uint8_t name ## _buf[ sizeof(XosMsgQueue) + ((num) * (size)) ]; \
  98. XosMsgQueue * name = (XosMsgQueue *) name ## _buf;
  99. #define XOS_MSGQ_SIZE(num, size) \
  100. (sizeof(XosMsgQueue) + ((num) * (size)))
  101. //-----------------------------------------------------------------------------
  102. ///
  103. /// Create the message queue object. Memory for the queue must be allocated by
  104. /// the caller, either statically or via dynamic allocation. See the macros
  105. /// XOS_MSGQ_ALLOC and XOS_MSGQ_SIZE for examples.
  106. ///
  107. /// \param msgq Handle (pointer) to message queue.
  108. ///
  109. /// \param num Number of messages allocated in queue. Must be > 0.
  110. ///
  111. /// \param size Message size in bytes. Must be > 0 and multiple of 4.
  112. ///
  113. /// \param flags Queue flags:
  114. /// - XOS_MSGQ_WAIT_FIFO - blocked threads will be
  115. /// woken in FIFO order.
  116. /// - XOS_MSGQ_WAIT_PRIORITY - blocked threads will
  117. /// be woken in priority order (default).
  118. ///
  119. /// \return Returns XOS_OK on success, else error code.
  120. ///
  121. //-----------------------------------------------------------------------------
  122. int32_t
  123. xos_msgq_create(XosMsgQueue * msgq, uint16_t num, uint32_t size, uint16_t flags);
  124. //-----------------------------------------------------------------------------
  125. ///
  126. /// Destroys the specified queue. Any waiting threads are unblocked with an
  127. /// error return. Any messages in the queue will be lost.
  128. ///
  129. /// \param msgq Pointer to message queue.
  130. ///
  131. /// \return Returns XOS_OK on success, else error code.
  132. ///
  133. //-----------------------------------------------------------------------------
  134. int32_t
  135. xos_msgq_delete(XosMsgQueue * msgq);
  136. //-----------------------------------------------------------------------------
  137. ///
  138. /// Put a message into the queue. The message contents are copied into the next
  139. /// available message slot. If no space is available, this function will block
  140. /// if called from a thread, but will return immediately if called from an
  141. /// interrupt handler.
  142. ///
  143. /// \param msgq Pointer to message queue.
  144. ///
  145. /// \param msg Pointer to message buffer.
  146. ///
  147. /// \return Returns XOS_OK on success, else error code.
  148. ///
  149. //-----------------------------------------------------------------------------
  150. int32_t
  151. xos_msgq_put(XosMsgQueue * msgq, const uint32_t * msg);
  152. //-----------------------------------------------------------------------------
  153. ///
  154. /// Put a message into the queue. The message contents are copied into the next
  155. /// available message slot. If no space is available, this function will block
  156. /// if called from a thread, but will return immediately if called from an
  157. /// interrupt handler. The thread will be unblocked when space frees up in the
  158. /// queue or the timeout expires.
  159. ///
  160. /// \param msgq Pointer to message queue.
  161. ///
  162. /// \param msg Pointer to message buffer.
  163. ///
  164. /// \param to_cycles Timeout in cycles. Convert from time to cycles
  165. /// using the helper functions provided in xos_timer.
  166. /// A value of zero indicates no timeout.
  167. ///
  168. /// \return Returns XOS_OK on success, XOS_ERR_TIMEOUT on timeout, else error code.
  169. ///
  170. /// NOTE: If XOS_OPT_WAIT_TIMEOUT is not enabled, then the timeout value is
  171. /// ignored, and no timeout will occur.
  172. ///
  173. //-----------------------------------------------------------------------------
  174. int32_t
  175. xos_msgq_put_timeout(XosMsgQueue * msgq, const uint32_t * msg, uint64_t to_cycles);
  176. //-----------------------------------------------------------------------------
  177. ///
  178. /// Get a message from the queue. The message contents are copied into the
  179. /// buffer that must be provided. If no message is available, this function
  180. /// will block if called from a thread, but will return immediately if called
  181. /// from an interrupt handler.
  182. ///
  183. /// \param msgq Pointer to message queue.
  184. ///
  185. /// \param msg Pointer to message buffer.
  186. ///
  187. /// \return Returns XOS_OK on success, else error code.
  188. ///
  189. //-----------------------------------------------------------------------------
  190. int32_t
  191. xos_msgq_get(XosMsgQueue * msgq, uint32_t * msg);
  192. //-----------------------------------------------------------------------------
  193. ///
  194. /// Get a message from the queue. The message contents are copied into the
  195. /// buffer that must be provided. If no message is available, this function
  196. /// will block if called from a thread, but will return immediately if called
  197. /// from an interrupt handler. The thread will be unblocked when a message
  198. /// arrives in the queue or the timeout expires.
  199. ///
  200. /// \param msgq Pointer to message queue.
  201. ///
  202. /// \param msg Pointer to message buffer.
  203. ///
  204. /// \param to_cycles Timeout in cycles. Convert from time to cycles
  205. /// using the helper functions provided in xos_timer.
  206. /// A value of zero indicates no timeout.
  207. ///
  208. /// \return Returns XOS_OK on success, XOS_ERR_TIMEOUT on timeout, else error code.
  209. ///
  210. /// NOTE: If XOS_OPT_WAIT_TIMEOUT is not enabled, then the timeout value is
  211. /// ignored, and no timeout will occur.
  212. ///
  213. //-----------------------------------------------------------------------------
  214. int32_t
  215. xos_msgq_get_timeout(XosMsgQueue * msgq, uint32_t * msg, uint64_t to_cycles);
  216. //-----------------------------------------------------------------------------
  217. ///
  218. /// Check if the queue is empty.
  219. ///
  220. /// \param msgq Pointer to message queue.
  221. ///
  222. /// \return Returns nonzero if queue is empty, zero if queue is not empty.
  223. ///
  224. //-----------------------------------------------------------------------------
  225. int32_t
  226. xos_msgq_empty(XosMsgQueue * msgq);
  227. //-----------------------------------------------------------------------------
  228. ///
  229. /// Check if the queue is full.
  230. ///
  231. /// \param msgq Pointer to message queue.
  232. ///
  233. /// \return Returns nonzero if queue is full, zero if queue is not full.
  234. ///
  235. //-----------------------------------------------------------------------------
  236. int32_t
  237. xos_msgq_full(XosMsgQueue * msgq);
  238. #ifdef __cplusplus
  239. }
  240. #endif
  241. #endif // __XOS_MSGQ_H__