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