Official mbed Real Time Operating System based on the RTX implementation of the CMSIS-RTOS API open standard.

Dependents:   denki-yohou_b TestY201 Network-RTOS NTPClient_HelloWorld ... more

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_os.h"
00029 #include "platform/mbed_error.h"
00030 
00031 namespace rtos {
00032 /** \addtogroup rtos */
00033 /** @{*/
00034 
00035 /** The Queue class allow to control, send, receive, or wait for messages.
00036  A message can be a integer or pointer value  to a certain type T that is send
00037  to a thread or interrupt service routine.
00038   @tparam  T         data type of a single message element.
00039   @tparam  queue_sz  maximum number of messages in queue.
00040 */
00041 template<typename T, uint32_t queue_sz>
00042 class Queue {
00043 public:
00044     /** Create and initialise a message Queue. */
00045     Queue() {
00046     #ifdef CMSIS_OS_RTX
00047         memset(_queue_q, 0, sizeof(_queue_q));
00048         _queue_def.pool = _queue_q;
00049         _queue_def.queue_sz = queue_sz;
00050     #endif
00051         _queue_id = osMessageCreate(&_queue_def, NULL);
00052         if (_queue_id == NULL) {
00053             error("Error initialising the queue object\n");
00054         }
00055     }
00056 
00057     /** Put a message in a Queue.
00058       @param   data      message pointer.
00059       @param   millisec  timeout value or 0 in case of no time-out. (default: 0)
00060       @return  status code that indicates the execution status of the function.
00061     */
00062     osStatus put(T* data, uint32_t millisec=0) {
00063         return osMessagePut(_queue_id, (uint32_t)data, millisec);
00064     }
00065 
00066     /** Get a message or Wait for a message from a Queue.
00067       @param   millisec  timeout value or 0 in case of no time-out. (default: osWaitForever).
00068       @return  event information that includes the message and the status code.
00069     */
00070     osEvent get(uint32_t millisec=osWaitForever) {
00071         return osMessageGet(_queue_id, millisec);
00072     }
00073 
00074 private:
00075     osMessageQId    _queue_id;
00076     osMessageQDef_t _queue_def;
00077 #ifdef CMSIS_OS_RTX
00078     uint32_t        _queue_q[4+(queue_sz)];
00079 #endif
00080 };
00081 
00082 }
00083 #endif
00084 
00085 /** @}*/