Mistake on this page?
Report an issue in GitHub or email us
Queue.h
1 /* mbed Microcontroller Library
2  * Copyright (c) 2006-2019 ARM Limited
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a copy
5  * of this software and associated documentation files (the "Software"), to deal
6  * in the Software without restriction, including without limitation the rights
7  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8  * copies of the Software, and to permit persons to whom the Software is
9  * furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20  * SOFTWARE.
21  */
22 #ifndef QUEUE_H
23 #define QUEUE_H
24 
25 #include "rtos/mbed_rtos_types.h"
26 #include "rtos/mbed_rtos1_types.h"
27 #include "rtos/mbed_rtos_storage.h"
28 #include "platform/mbed_error.h"
29 #include "platform/NonCopyable.h"
30 
31 #if MBED_CONF_RTOS_PRESENT || defined(DOXYGEN_ONLY)
32 
33 namespace rtos {
34 /** \addtogroup rtos-public-api */
35 /** @{*/
36 
37 /**
38  * \defgroup rtos_Queue Queue class
39  * @{
40  */
41 
42 /** The Queue class represents a collection of objects that are stored first by
43  * order of priority, and then in first-in, first-out (FIFO) order.
44  *
45  * You can use a queue when you need to store data and then access it in the same
46  * order that it has been stored. The order in which you retrieve the data is in
47  * order of descending priority. If multiple elements have the same priority,
48  * they are retrieved in FIFO order.
49  *
50  * The object type stored in the queue can be an integer, pointer or a generic
51  * type given by the template parameter T.
52  *
53  * @tparam T Specifies the type of elements stored in the queue.
54  * @tparam queue_sz Maximum number of messages that you can store in the queue.
55  *
56  * @note Memory considerations: The queue control structures are created on the
57  * current thread's stack, both for the Mbed OS and underlying RTOS
58  * objects (static or dynamic RTOS memory pools are not being used).
59  *
60  */
61 template<typename T, uint32_t queue_sz>
62 class Queue : private mbed::NonCopyable<Queue<T, queue_sz> > {
63 public:
64  /** Create and initialize a message Queue of objects of the parameterized
65  * type `T` and maximum capacity specified by `queue_sz`.
66  *
67  * @note You cannot call this function from ISR context.
68  */
70  {
71  osMessageQueueAttr_t attr = { 0 };
72  attr.mq_mem = _queue_mem;
73  attr.mq_size = sizeof(_queue_mem);
74  attr.cb_mem = &_obj_mem;
75  attr.cb_size = sizeof(_obj_mem);
76  _id = osMessageQueueNew(queue_sz, sizeof(T *), &attr);
77  MBED_ASSERT(_id);
78  }
79 
80  /** Queue destructor
81  *
82  * @note You cannot call this function from ISR context.
83  */
85  {
86  osMessageQueueDelete(_id);
87  }
88 
89  /** Check if the queue is empty.
90  *
91  * @return True if the queue is empty, false if not
92  *
93  * @note You may call this function from ISR context.
94  */
95  bool empty() const
96  {
97  return osMessageQueueGetCount(_id) == 0;
98  }
99 
100  /** Check if the queue is full.
101  *
102  * @return True if the queue is full, false if not
103  *
104  * @note You may call this function from ISR context.
105  */
106  bool full() const
107  {
108  return osMessageQueueGetSpace(_id) == 0;
109  }
110 
111  /** Get number of queued messages in the queue.
112  *
113  * @return Number of items in the queue
114  *
115  * @note You may call this function from ISR context.
116  */
117  uint32_t count() const
118  {
119  return osMessageQueueGetCount(_id);
120  }
121 
122  /** Inserts the given element to the end of the queue.
123  *
124  * This function puts the message pointed to by `data` into the queue. The
125  * parameter `prio` is used to sort the message according to their priority
126  * (higher numbers indicate higher priority) on insertion.
127  *
128  * The timeout indicated by the parameter `millisec` specifies how long the
129  * function blocks waiting for the message to be inserted into the
130  * queue.
131  *
132  * The parameter `millisec` can have the following values:
133  * - When the timeout is 0 (the default), the function returns instantly.
134  * - When the timeout is osWaitForever, the function waits for an
135  * infinite time.
136  * - For all other values, the function waits for the given number of
137  * milliseconds.
138  *
139  * @param data Pointer to the element to insert into the queue.
140  * @param millisec Timeout for the operation to be executed, or 0 in case
141  * of no timeout. (default: 0)
142  * @param prio Priority of the operation or 0 in case of default.
143  * (default: 0)
144  *
145  * @return Status code that indicates the execution status of the function:
146  * @a osOK The message has been successfully inserted
147  * into the queue.
148  * @a osErrorTimeout The message could not be inserted into the
149  * queue in the given time.
150  * @a osErrorResource The message could not be inserted because
151  * the queue is full.
152  * @a osErrorParameter Internal error or nonzero timeout specified
153  * in an ISR.
154  *
155  * @note You may call this function from ISR context if the millisec
156  * parameter is set to 0.
157  *
158  */
159  osStatus put(T *data, uint32_t millisec = 0, uint8_t prio = 0)
160  {
161  return osMessageQueuePut(_id, &data, prio, millisec);
162  }
163 
164  /** Get a message or wait for a message from the queue.
165  *
166  * This function retrieves a message from the queue. The message is stored
167  * in the value field of the returned `osEvent` object.
168  *
169  * The timeout specified by the parameter `millisec` specifies how long the
170  * function waits to retrieve the message from the queue.
171  *
172  * The timeout parameter can have the following values:
173  * - When the timeout is 0, the function returns instantly.
174  * - When the timeout is osWaitForever (default), the function waits
175  * infinite time until the message is retrieved.
176  * - When the timeout is any other value, the function waits for the
177  * specified time before returning a timeout error.
178  *
179  * Messages are retrieved in descending priority order. If two messages
180  * share the same priority level, they are retrieved in first-in, first-out
181  * (FIFO) order.
182  *
183  * @param millisec Timeout value.
184  * (default: osWaitForever).
185  *
186  * @return Event information that includes the message in event. Message
187  * value and the status code in event.status:
188  * @a osEventMessage Message successfully received.
189  * @a osOK No message is available in the queue, and no
190  * timeout was specified.
191  * @a osEventTimeout No message was received before a timeout
192  * event occurred.
193  * @a osErrorParameter A parameter is invalid or outside of a
194  * permitted range.
195  *
196  * @note You may call this function from ISR context if the millisec
197  * parameter is set to 0.
198  */
199  osEvent get(uint32_t millisec = osWaitForever)
200  {
201  osEvent event;
202  T *data = nullptr;
203  osStatus_t res = osMessageQueueGet(_id, &data, nullptr, millisec);
204 
205  switch (res) {
206  case osOK:
207  event.status = (osStatus)osEventMessage;
208  event.value.p = data;
209  break;
210  case osErrorResource:
211  event.status = osOK;
212  break;
213  case osErrorTimeout:
214  event.status = (osStatus)osEventTimeout;
215  break;
216  case osErrorParameter:
217  default:
218  event.status = osErrorParameter;
219  break;
220  }
221  event.def.message_id = _id;
222 
223  return event;
224  }
225 
226 private:
227  osMessageQueueId_t _id;
228  char _queue_mem[queue_sz * (sizeof(T *) + sizeof(mbed_rtos_storage_message_t))];
230 };
231 /** @}*/
232 /** @}*/
233 
234 } // namespace rtos
235 
236 #endif
237 
238 #endif // QUEUE_H
Message Queue Control Block.
Definition: rtx_os.h:247
bool full() const
Check if the queue is full.
Definition: Queue.h:106
Message Control Block.
Definition: rtx_os.h:237
~Queue()
Queue destructor.
Definition: Queue.h:84
The Queue class represents a collection of objects that are stored first by order of priority...
Definition: Queue.h:62
void * cb_mem
memory for control block
Definition: cmsis_os2.h:298
Attributes structure for message queue.
Definition: cmsis_os2.h:295
Prevents generation of copy constructor and copy assignment operator in derived classes.
Definition: NonCopyable.h:169
Event structure contains detailed information about an event.
Definition: cmsis_os.h:374
#define MBED_ASSERT(expr)
MBED_ASSERT Declare runtime assertions: results in runtime error if condition is false.
Definition: mbed_assert.h:65
Queue()
Create and initialize a message Queue of objects of the parameterized type T and maximum capacity spe...
Definition: Queue.h:69
Definition: TaskBase.h:25
bool empty() const
Check if the queue is empty.
Definition: Queue.h:95
uint32_t mq_size
size of provided memory for data storage
Definition: cmsis_os2.h:301
uint32_t count() const
Get number of queued messages in the queue.
Definition: Queue.h:117
void * mq_mem
memory for data storage
Definition: cmsis_os2.h:300
uint32_t cb_size
size of provided memory for control block
Definition: cmsis_os2.h:299
osStatus put(T *data, uint32_t millisec=0, uint8_t prio=0)
Inserts the given element to the end of the queue.
Definition: Queue.h:159
Important Information for this Arm website

This site uses cookies to store information on your computer. By continuing to use our site, you consent to our cookies. If you are not happy with the use of these cookies, please review our Cookie Policy to learn how they can be disabled. By disabling cookies, some features of the site will not work.