1 /*
2  * Copyright (C) 2017 XRADIO TECHNOLOGY CO., LTD. All rights reserved.
3  *
4  *  Redistribution and use in source and binary forms, with or without
5  *  modification, are permitted provided that the following conditions
6  *  are met:
7  *    1. Redistributions of source code must retain the above copyright
8  *       notice, this list of conditions and the following disclaimer.
9  *    2. Redistributions in binary form must reproduce the above copyright
10  *       notice, this list of conditions and the following disclaimer in the
11  *       documentation and/or other materials provided with the
12  *       distribution.
13  *    3. Neither the name of XRADIO TECHNOLOGY CO., LTD. nor the names of
14  *       its contributors may be used to endorse or promote products derived
15  *       from this software without specific prior written permission.
16  *
17  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20  *  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21  *  OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22  *  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23  *  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24  *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25  *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26  *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27  *  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29 
30 #ifndef _KERNEL_OS_RTTHREAD_OS_QUEUE_H_
31 #define _KERNEL_OS_RTTHREAD_OS_QUEUE_H_
32 
33 #include "_os_common.h"
34 
35 #ifdef __cplusplus
36 extern "C" {
37 #endif
38 
39 /**
40  * @brief Queue object definition
41  */
42 typedef struct OS_Queue {
43     rt_mq_t     handle;
44     uint32_t    itemSize;
45 } OS_Queue_t;
46 
47 OS_Status OS_QueueCreate(OS_Queue_t *queue, uint32_t queueLen, uint32_t itemSize);
48 OS_Status OS_QueueDelete(OS_Queue_t *queue);
49 OS_Status OS_QueueSend(OS_Queue_t *queue, const void *item, OS_Time_t waitMS);
50 OS_Status OS_QueueReceive(OS_Queue_t *queue, void *item, OS_Time_t waitMS);
51 
52 /**
53  * @brief Check whether the queue object is valid or not
54  * @param[in] queue Pointer to the queue object
55  * @return 1 on valid, 0 on invalid
56  */
OS_QueueIsValid(OS_Queue_t * queue)57 static inline int OS_QueueIsValid(OS_Queue_t *queue)
58 {
59     return (queue->handle != OS_INVALID_HANDLE);
60 }
61 
62 /**
63  * @brief Set the queue object to invalid state
64  * @param[in] queue Pointer to the queue object
65  * @return None
66  */
OS_QueueSetInvalid(OS_Queue_t * queue)67 static inline void OS_QueueSetInvalid(OS_Queue_t *queue)
68 {
69     queue->handle = OS_INVALID_HANDLE;
70 }
71 
72 /**
73  * @brief Create and initialize a message queue object
74  * @note A message queue is a queue with each data item can store a pointer.
75  *       The size of each data item (message) is equal to sizeof(void *).
76  * @param[in] queue Pointer to the message queue object
77  * @param[in] queueLen The maximum number of items that the message queue can
78  *                     hold at any one time.
79  * @retval OS_Status, OS_OK on success
80  */
OS_MsgQueueCreate(OS_Queue_t * queue,uint32_t queueLen)81 static inline OS_Status OS_MsgQueueCreate(OS_Queue_t *queue, uint32_t queueLen)
82 {
83     return OS_QueueCreate(queue, queueLen, sizeof(void *));
84 }
85 
86 /**
87  * @brief Delete the message queue object
88  * @param[in] queue Pointer to the message queue object
89  * @retval OS_Status, OS_OK on success
90  */
OS_MsgQueueDelete(OS_Queue_t * queue)91 static inline OS_Status OS_MsgQueueDelete(OS_Queue_t *queue)
92 {
93     return OS_QueueDelete(queue);
94 }
95 
96 /**
97  * @brief Send (write) a message to the back of the message queue
98  * @param[in] queue Pointer to the message queue object
99  * @param[in] msg A message, which is a pointer, to be copied into the queue
100  * @param[in] waitMS The maximum amount of time the thread should remain in the
101  *                   blocked state to wait for space to become available on the
102  *                   message queue, should the message queue already be full.
103  *                   OS_WAIT_FOREVER for waiting forever, zero for no waiting.
104  * @retval OS_Status, OS_OK on success
105  */
OS_MsgQueueSend(OS_Queue_t * queue,void * msg,OS_Time_t waitMS)106 static inline OS_Status OS_MsgQueueSend(OS_Queue_t *queue, void *msg, OS_Time_t waitMS)
107 {
108     return OS_QueueSend(queue, &msg, waitMS);
109 }
110 
111 /**
112  * @brief Receive (read) a message from the message queue
113  * @param[in] queue Pointer to the message queue object
114  * @param[in] msg Pointer to the message buffer into which the received message
115  *                will be copied. A message is a pointer.
116  * @param[in] waitMS The maximum amount of time the thread should remain in the
117  *                   blocked state to wait for message to become available on
118  *                   the message queue, should the message queue already be
119  *                   empty.
120  *                   OS_WAIT_FOREVER for waiting forever, zero for no waiting.
121  * @retval OS_Status, OS_OK on success
122  */
OS_MsgQueueReceive(OS_Queue_t * queue,void ** msg,OS_Time_t waitMS)123 static inline OS_Status OS_MsgQueueReceive(OS_Queue_t *queue, void **msg, OS_Time_t waitMS)
124 {
125     return OS_QueueReceive(queue, msg, waitMS);
126 }
127 
128 #ifdef __cplusplus
129 }
130 #endif
131 
132 #endif /* _KERNEL_OS_RTTHREAD_OS_QUEUE_H_ */
133