Lee Shen / FTHR_USB_serial_qSPI
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Queue.h Source File

Queue.h

00001 /* mbed Microcontroller Library
00002  * Copyright (c) 2006-2012 ARM Limited
00003  *
00004  * Permission is hereby granted, free of charge, to any person obtaining a copy
00005  * of this software and associated documentation files (the "Software"), to deal
00006  * in the Software without restriction, including without limitation the rights
00007  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00008  * copies of the Software, and to permit persons to whom the Software is
00009  * furnished to do so, subject to the following conditions:
00010  *
00011  * The above copyright notice and this permission notice shall be included in
00012  * all copies or substantial portions of the Software.
00013  *
00014  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00015  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00016  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00017  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00018  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00019  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
00020  * SOFTWARE.
00021  */
00022 #ifndef QUEUE_H
00023 #define QUEUE_H
00024 
00025 #include <stdint.h>
00026 #include <string.h>
00027 
00028 #include "cmsis_os2.h"
00029 #include "mbed_rtos_storage.h"
00030 #include "platform/mbed_error.h"
00031 #include "platform/NonCopyable.h"
00032 #include "mbed_rtos1_types.h"
00033 
00034 namespace rtos {
00035 /** \addtogroup rtos */
00036 /** @{*/
00037 
00038 /** The Queue class allow to control, send, receive, or wait for messages.
00039  A message can be a integer or pointer value  to a certain type T that is send
00040  to a thread or interrupt service routine.
00041   @tparam  T         data type of a single message element.
00042   @tparam  queue_sz  maximum number of messages in queue.
00043 
00044  @note
00045  Memory considerations: The queue control structures will be created on current thread's stack, both for the mbed OS
00046  and underlying RTOS objects (static or dynamic RTOS memory pools are not being used).
00047 */
00048 template<typename T, uint32_t queue_sz>
00049 class Queue : private mbed::NonCopyable<Queue<T, queue_sz> > {
00050 public:
00051     /** Create and initialize a message Queue. */
00052     Queue() {
00053         memset(&_obj_mem, 0, sizeof(_obj_mem));
00054         memset(&_attr, 0, sizeof(_attr));
00055         _attr.mq_mem = _queue_mem;
00056         _attr.mq_size = sizeof(_queue_mem);
00057         _attr.cb_mem = &_obj_mem;
00058         _attr.cb_size = sizeof(_obj_mem);
00059         _id = osMessageQueueNew(queue_sz, sizeof(T*), &_attr);
00060         MBED_ASSERT(_id);
00061     }
00062 
00063     ~Queue() {
00064         osMessageQueueDelete(_id);
00065     }
00066 
00067     /** Put a message in a Queue.
00068       @param   data      message pointer.
00069       @param   millisec  timeout value or 0 in case of no time-out. (default: 0)
00070       @param   prio      priority value or 0 in case of default. (default: 0)
00071       @return  status code that indicates the execution status of the function:
00072                @a osOK the message has been put into the queue.
00073                @a osErrorTimeout the message could not be put into the queue in the given time.
00074                @a osErrorResource not enough space in the queue.
00075                @a osErrorParameter internal error or non-zero timeout specified in an ISR.
00076     */
00077     osStatus put(T* data, uint32_t millisec=0, uint8_t prio=0) {
00078         return osMessageQueuePut(_id, &data, prio, millisec);
00079     }
00080 
00081     /** Get a message or Wait for a message from a Queue. Messages are retrieved in a descending priority order or
00082         first in first out when the priorities are the same.
00083       @param   millisec  timeout value or 0 in case of no time-out. (default: osWaitForever).
00084       @return  event information that includes the message in event.value and the status code in event.status:
00085                @a osEventMessage message received.
00086                @a osOK no message is available in the queue and no timeout was specified.
00087                @a osEventTimeout no message has arrived during the given timeout period.
00088                @a osErrorParameter a parameter is invalid or outside of a permitted range.
00089     */
00090     osEvent get(uint32_t millisec=osWaitForever) {
00091         osEvent event;
00092         T *data = NULL;
00093         osStatus_t res = osMessageQueueGet(_id, &data, NULL, millisec);
00094 
00095         switch (res) {
00096             case osOK:
00097                 event.status = (osStatus)osEventMessage;
00098                 event.value.p = data;
00099                 break;
00100             case osErrorResource:
00101                 event.status = osOK;
00102                 break;
00103             case osErrorTimeout:
00104                 event.status = (osStatus)osEventTimeout;
00105                 break;
00106             case osErrorParameter:
00107             default:
00108                 event.status = osErrorParameter;
00109                 break;
00110         }
00111         event.def.message_id = _id;
00112 
00113         return event;
00114     }
00115 
00116 private:
00117     osMessageQueueId_t            _id;
00118     osMessageQueueAttr_t          _attr;
00119     char                          _queue_mem[queue_sz * (sizeof(T*) + sizeof(mbed_rtos_storage_message_t))];
00120     mbed_rtos_storage_msg_queue_t _obj_mem;
00121 };
00122 
00123 }
00124 #endif
00125 
00126 /** @}*/