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 <stdint.h>
26 #include <string.h>
27 
28 #include "cmsis_os2.h"
29 #include "mbed_rtos_storage.h"
30 #include "platform/mbed_error.h"
31 #include "platform/NonCopyable.h"
32 #include "mbed_rtos1_types.h"
33 
34 namespace rtos {
35 /** \addtogroup rtos */
36 /** @{*/
37 /**
38  * \defgroup rtos_Queue Queue class
39  * @{
40  */
41 
42 /** The Queue class allow to control, send, receive, or wait for messages.
43  A message can be a integer or pointer value to a certain type T that is send
44  to a thread or interrupt service routine.
45  @tparam T data type of a single message element.
46  @tparam queue_sz maximum number of messages in queue.
47 
48  @note
49  Memory considerations: The queue control structures will be created on current thread's stack, both for the mbed OS
50  and underlying RTOS objects (static or dynamic RTOS memory pools are not being used).
51 */
52 template<typename T, uint32_t queue_sz>
53 class Queue : private mbed::NonCopyable<Queue<T, queue_sz> > {
54 public:
55  /** Create and initialize a message Queue.
56  *
57  * @note You cannot call this function from ISR context.
58  */
59  Queue() {
60  memset(&_obj_mem, 0, sizeof(_obj_mem));
61  osMessageQueueAttr_t attr = { 0 };
62  attr.mq_mem = _queue_mem;
63  attr.mq_size = sizeof(_queue_mem);
64  attr.cb_mem = &_obj_mem;
65  attr.cb_size = sizeof(_obj_mem);
66  _id = osMessageQueueNew(queue_sz, sizeof(T*), &attr);
67  MBED_ASSERT(_id);
68  }
69  /** Queue destructor
70  *
71  * @note You cannot call this function from ISR context.
72  */
73  ~Queue() {
74  osMessageQueueDelete(_id);
75  }
76 
77  /** Check if the queue is empty
78  *
79  * @return True if the queue is empty, false if not
80  *
81  * @note You may call this function from ISR context.
82  */
83  bool empty() const {
84  return osMessageQueueGetCount(_id) == 0;
85  }
86 
87  /** Check if the queue is full
88  *
89  * @return True if the queue is full, false if not
90  *
91  * @note You may call this function from ISR context.
92  */
93  bool full() const {
94  return osMessageQueueGetSpace(_id) == 0;
95  }
96 
97  /** Put a message in a Queue.
98  @param data message pointer.
99  @param millisec timeout value or 0 in case of no time-out. (default: 0)
100  @param prio priority value or 0 in case of default. (default: 0)
101  @return status code that indicates the execution status of the function:
102  @a osOK the message has been put into the queue.
103  @a osErrorTimeout the message could not be put into the queue in the given time.
104  @a osErrorResource not enough space in the queue.
105  @a osErrorParameter internal error or non-zero timeout specified in an ISR.
106 
107  @note You may call this function from ISR context if the millisec parameter is set to 0.
108  */
109  osStatus put(T* data, uint32_t millisec=0, uint8_t prio=0) {
110  return osMessageQueuePut(_id, &data, prio, millisec);
111  }
112 
113  /** Get a message or Wait for a message from a Queue. Messages are retrieved in a descending priority order or
114  first in first out when the priorities are the same.
115  @param millisec timeout value or 0 in case of no time-out. (default: osWaitForever).
116  @return event information that includes the message in event.value and the status code in event.status:
117  @a osEventMessage message received.
118  @a osOK no message is available in the queue and no timeout was specified.
119  @a osEventTimeout no message has arrived during the given timeout period.
120  @a osErrorParameter a parameter is invalid or outside of a permitted range.
121 
122  @note You may call this function from ISR context if the millisec parameter is set to 0.
123  */
124  osEvent get(uint32_t millisec=osWaitForever) {
125  osEvent event;
126  T *data = NULL;
127  osStatus_t res = osMessageQueueGet(_id, &data, NULL, millisec);
128 
129  switch (res) {
130  case osOK:
131  event.status = (osStatus)osEventMessage;
132  event.value.p = data;
133  break;
134  case osErrorResource:
135  event.status = osOK;
136  break;
137  case osErrorTimeout:
138  event.status = (osStatus)osEventTimeout;
139  break;
140  case osErrorParameter:
141  default:
142  event.status = osErrorParameter;
143  break;
144  }
145  event.def.message_id = _id;
146 
147  return event;
148  }
149 
150 private:
151  osMessageQueueId_t _id;
152  char _queue_mem[queue_sz * (sizeof(T*) + sizeof(mbed_rtos_storage_message_t))];
153  mbed_rtos_storage_msg_queue_t _obj_mem;
154 };
155 /** @}*/
156 /** @}*/
157 
158 }
159 #endif
160 
osStatus put(T *data, uint32_t millisec=0, uint8_t prio=0)
Definition: Queue.h:109
~Queue()
Definition: Queue.h:73
Definition: Queue.h:53
Queue()
Definition: Queue.h:59
Definition: NonCopyable.h:150
bool empty() const
Definition: Queue.h:83
Definition: cmsis_os.h:27
#define MBED_ASSERT(expr)
Definition: mbed_assert.h:63
bool full() const
Definition: Queue.h:93
Definition: Kernel.cpp:19
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.