Mistake on this page?
Report an issue in GitHub or email us
Queue.h
1 /* mbed Microcontroller Library
2  * Copyright (c) 2006-2012 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 "cmsis_os2.h"
26 #include "mbed_rtos1_types.h"
27 #include "mbed_rtos_storage.h"
28 #include "platform/mbed_error.h"
29 #include "platform/NonCopyable.h"
30 
31 namespace rtos {
32 /** \addtogroup rtos */
33 /** @{*/
34 /**
35  * \defgroup rtos_Queue Queue class
36  * @{
37  */
38 
39 /** The Queue class represents a collection of objects that are stored first by
40  * order of priority, and then in first-in, first-out (FIFO) order.
41  *
42  * You can use a queue when you need to store data and then access it in the same
43  * order that it has been stored. The order in which you retrieve the data is in
44  * order of descending priority. If multiple elements have the same priority,
45  * they are retrieved in FIFO order.
46  *
47  * The object type stored in the queue can be an integer, pointer or a generic
48  * type given by the template parameter T.
49  *
50  * @tparam T Specifies the type of elements stored in the queue.
51  * @tparam queue_sz Maximum number of messages that you can store in the queue.
52  *
53  * @note Memory considerations: The queue control structures are created on the
54  * current thread's stack, both for the Mbed OS and underlying RTOS
55  * objects (static or dynamic RTOS memory pools are not being used).
56  *
57  */
58 template<typename T, uint32_t queue_sz>
59 class Queue : private mbed::NonCopyable<Queue<T, queue_sz> > {
60 public:
61  /** Create and initialize a message Queue of objects of the parameterized
62  * type `T` and maximum capacity specified by `queue_sz`.
63  *
64  * @note You cannot call this function from ISR context.
65  */
67  {
68  osMessageQueueAttr_t attr = { 0 };
69  attr.mq_mem = _queue_mem;
70  attr.mq_size = sizeof(_queue_mem);
71  attr.cb_mem = &_obj_mem;
72  attr.cb_size = sizeof(_obj_mem);
73  _id = osMessageQueueNew(queue_sz, sizeof(T *), &attr);
74  MBED_ASSERT(_id);
75  }
76 
77  /** Queue destructor
78  *
79  * @note You cannot call this function from ISR context.
80  */
82  {
83  osMessageQueueDelete(_id);
84  }
85 
86  /** Check if the queue is empty.
87  *
88  * @return True if the queue is empty, false if not
89  *
90  * @note You may call this function from ISR context.
91  */
92  bool empty() const
93  {
94  return osMessageQueueGetCount(_id) == 0;
95  }
96 
97  /** Check if the queue is full.
98  *
99  * @return True if the queue is full, false if not
100  *
101  * @note You may call this function from ISR context.
102  */
103  bool full() const
104  {
105  return osMessageQueueGetSpace(_id) == 0;
106  }
107 
108  /** Inserts the given element to the end of the queue.
109  *
110  * This function puts the message pointed to by `data` into the queue. The
111  * parameter `prio` is used to sort the message according to their priority
112  * (higher numbers indicate higher priority) on insertion.
113  *
114  * The timeout indicated by the parameter `millisec` specifies how long the
115  * function blocks waiting for the message to be inserted into the
116  * queue.
117  *
118  * The parameter `millisec` can have the following values:
119  * - When the timeout is 0 (the default), the function returns instantly.
120  * - When the timeout is osWaitForever, the function waits for an
121  * infinite time.
122  * - For all other values, the function waits for the given number of
123  * milliseconds.
124  *
125  * @param data Pointer to the element to insert into the queue.
126  * @param millisec Timeout for the operation to be executed, or 0 in case
127  * of no timeout. (default: 0)
128  * @param prio Priority of the operation or 0 in case of default.
129  * (default: 0)
130  *
131  * @return Status code that indicates the execution status of the function:
132  * @a osOK The message has been successfully inserted
133  * into the queue.
134  * @a osErrorTimeout The message could not be inserted into the
135  * queue in the given time.
136  * @a osErrorResource The message could not be inserted because
137  * the queue is full.
138  * @a osErrorParameter Internal error or nonzero timeout specified
139  * in an ISR.
140  *
141  * @note You may call this function from ISR context if the millisec
142  * parameter is set to 0.
143  *
144  */
145  osStatus put(T *data, uint32_t millisec = 0, uint8_t prio = 0)
146  {
147  return osMessageQueuePut(_id, &data, prio, millisec);
148  }
149 
150  /** Get a message or wait for a message from the queue.
151  *
152  * This function retrieves a message from the queue. The message is stored
153  * in the value field of the returned `osEvent` object.
154  *
155  * The timeout specified by the parameter `millisec` specifies how long the
156  * function waits to retrieve the message from the queue.
157  *
158  * The timeout parameter can have the following values:
159  * - When the timeout is 0, the function returns instantly.
160  * - When the timeout is osWaitForever (default), the function waits
161  * infinite time until the message is retrieved.
162  * - When the timeout is any other value, the function waits for the
163  * specified time before returning a timeout error.
164  *
165  * Messages are retrieved in descending priority order. If two messages
166  * share the same priority level, they are retrieved in first-in, first-out
167  * (FIFO) order.
168  *
169  * @param millisec Timeout value.
170  * (default: osWaitForever).
171  *
172  * @return Event information that includes the message in event. Message
173  * value and the status code in event.status:
174  * @a osEventMessage Message successfully received.
175  * @a osOK No message is available in the queue, and no
176  * timeout was specified.
177  * @a osEventTimeout No message was received before a timeout
178  * event occurred.
179  * @a osErrorParameter A parameter is invalid or outside of a
180  * permitted range.
181  *
182  * @note You may call this function from ISR context if the millisec
183  * parameter is set to 0.
184  */
185  osEvent get(uint32_t millisec = osWaitForever)
186  {
187  osEvent event;
188  T *data = NULL;
189  osStatus_t res = osMessageQueueGet(_id, &data, NULL, millisec);
190 
191  switch (res) {
192  case osOK:
193  event.status = (osStatus)osEventMessage;
194  event.value.p = data;
195  break;
196  case osErrorResource:
197  event.status = osOK;
198  break;
199  case osErrorTimeout:
200  event.status = (osStatus)osEventTimeout;
201  break;
202  case osErrorParameter:
203  default:
204  event.status = osErrorParameter;
205  break;
206  }
207  event.def.message_id = _id;
208 
209  return event;
210  }
211 
212 private:
213  osMessageQueueId_t _id;
214  char _queue_mem[queue_sz * (sizeof(T *) + sizeof(mbed_rtos_storage_message_t))];
215  mbed_rtos_storage_msg_queue_t _obj_mem;
216 };
217 /** @}*/
218 /** @}*/
219 
220 } // namespace rtos
221 
222 #endif // QUEUE_H
bool full() const
Check if the queue is full.
Definition: Queue.h:103
~Queue()
Queue destructor.
Definition: Queue.h:81
The Queue class represents a collection of objects that are stored first by order of priority...
Definition: Queue.h:59
Prevents generation of copy constructor and copy assignment operator in derived classes.
Definition: NonCopyable.h:168
#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:66
bool empty() const
Check if the queue is empty.
Definition: Queue.h:92
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:145
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.