non blocking queue

Fork of mbed-rtos by mbed official

Committer:
emilmont
Date:
Fri Nov 23 09:57:31 2012 +0000
Revision:
6:350b53afb889
Child:
8:88a1a9c26ae3
Merge RTOS C++ API and RTX under the same library; Update RTX to version 4.60; Add proper Thread destructor;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
emilmont 6:350b53afb889 1 /* Copyright (c) 2012 mbed.org */
emilmont 6:350b53afb889 2 #ifndef QUEUE_H
emilmont 6:350b53afb889 3 #define QUEUE_H
emilmont 6:350b53afb889 4
emilmont 6:350b53afb889 5 #include <stdint.h>
emilmont 6:350b53afb889 6 #include <string.h>
emilmont 6:350b53afb889 7
emilmont 6:350b53afb889 8 #include "cmsis_os.h"
emilmont 6:350b53afb889 9 #include "error.h"
emilmont 6:350b53afb889 10
emilmont 6:350b53afb889 11 namespace rtos {
emilmont 6:350b53afb889 12
emilmont 6:350b53afb889 13 /*! The Queue class allow to control, send, receive, or wait for messages.
emilmont 6:350b53afb889 14 A message can be a integer or pointer value to a certain type T that is send
emilmont 6:350b53afb889 15 to a thread or interrupt service routine.
emilmont 6:350b53afb889 16 \tparam T data type of a single message element.
emilmont 6:350b53afb889 17 \tparam queue_sz maximum number of messages in queue.
emilmont 6:350b53afb889 18 */
emilmont 6:350b53afb889 19 template<typename T, uint32_t queue_sz>
emilmont 6:350b53afb889 20 class Queue {
emilmont 6:350b53afb889 21 public:
emilmont 6:350b53afb889 22 /*! Create and initialise a message Queue. */
emilmont 6:350b53afb889 23 Queue() {
emilmont 6:350b53afb889 24 #ifdef CMSIS_OS_RTX
emilmont 6:350b53afb889 25 memset(_queue_q, 0, sizeof(_queue_q));
emilmont 6:350b53afb889 26 _queue_def.pool = _queue_q;
emilmont 6:350b53afb889 27 _queue_def.queue_sz = queue_sz;
emilmont 6:350b53afb889 28 #endif
emilmont 6:350b53afb889 29 _queue_id = osMessageCreate(&_queue_def, NULL);
emilmont 6:350b53afb889 30 if (_queue_id == NULL) {
emilmont 6:350b53afb889 31 error("Error initialising the queue object\n");
emilmont 6:350b53afb889 32 }
emilmont 6:350b53afb889 33 }
emilmont 6:350b53afb889 34
emilmont 6:350b53afb889 35 /*! Put a message in a Queue.
emilmont 6:350b53afb889 36 \param data message pointer.
emilmont 6:350b53afb889 37 \param millisec timeout value or 0 in case of no time-out. (default: 0)
emilmont 6:350b53afb889 38 \return status code that indicates the execution status of the function.
emilmont 6:350b53afb889 39 */
emilmont 6:350b53afb889 40 osStatus put(T* data, uint32_t millisec=0) {
emilmont 6:350b53afb889 41 return osMessagePut(_queue_id, (uint32_t)data, millisec);
emilmont 6:350b53afb889 42 }
emilmont 6:350b53afb889 43
emilmont 6:350b53afb889 44 /*! Get a message or Wait for a message from a Queue.
emilmont 6:350b53afb889 45 \param millisec timeout value or 0 in case of no time-out. (default: osWaitForever).
emilmont 6:350b53afb889 46 \return event information that includes the message and the status code.
emilmont 6:350b53afb889 47 */
emilmont 6:350b53afb889 48 osEvent get(uint32_t millisec=osWaitForever) {
emilmont 6:350b53afb889 49 return osMessageGet(_queue_id, millisec);
emilmont 6:350b53afb889 50 }
emilmont 6:350b53afb889 51
emilmont 6:350b53afb889 52 private:
emilmont 6:350b53afb889 53 osMessageQId _queue_id;
emilmont 6:350b53afb889 54 osMessageQDef_t _queue_def;
emilmont 6:350b53afb889 55 #ifdef CMSIS_OS_RTX
emilmont 6:350b53afb889 56 uint32_t _queue_q[4+(queue_sz)];
emilmont 6:350b53afb889 57 #endif
emilmont 6:350b53afb889 58 };
emilmont 6:350b53afb889 59
emilmont 6:350b53afb889 60 }
emilmont 6:350b53afb889 61 #endif