USB-LED-Fader/firmware/message_queue.c

96 lines
2.5 KiB
C
Raw Normal View History

2006-09-26 18:18:27 +00:00
/**
* \file message_queue.c
* \brief A message queue used to exchange messages between two concurrent threads.
* \author Thomas Stegemann
* \version $Id: message_queue.c,v 1.1 2006/09/26 18:18:27 rschaten Exp $
*
* License: See documentation.
*/
#include <stdint.h>
#include "message_queue.h"
#include "config_message_queue_impl.h"
/** Structure of the global data of the queue */
typedef struct S_messageQueue_GlobalData {
messageQueue_QueuedMessage queue[messageQueue_Size]; /**< the queue itself */
messageQueue_SizeType begin; /**< the current start of the queue */
messageQueue_SizeType end; /**< the current end of the queue */
} messageQueue_GlobalData;
/** Global data of the queue */
static volatile messageQueue_GlobalData m_data;
/**
* Get the next entry fron the queue.
* \param value Number of the current entry.
* \return 0 if the value is larger than the queue, otherwise the next entry.
*/
static inline messageQueue_SizeType messageQueue_next(messageQueue_SizeType value) {
value++;
if(value >= messageQueue_Size) {
value= 0;
}
return value;
}
/**
* Initialize the queue.
*/
void messageQueue_init(void) {
m_data.begin= 0;
m_data.end= 0;
}
/**
* Clean up the queue. Currently this does nothing.
*/
void messageQueue_cleanup(void)
{}
/**
* Test if the queue is empty.
* \return True if it is empty, otherwise false.
*/
Boolean messageQueue_isEmpty(void) {
return boolean(m_data.begin == m_data.end);
}
/**
* Test if the queue is full. If it is full, new entries will overwrite the
* first entries.
* \return True if it is full, otherwise false.
*/
Boolean messageQueue_isFull(void) {
return boolean(messageQueue_next(m_data.end) == m_data.begin);
}
/**
* Read a message from the queue.
* \param pMessage Pointer to a message variable that should be set to the
* message.
* \return True if an entry could be read, otherwise false.
*/
Boolean messageQueue_read(messageQueue_QueuedMessage* pMessage) {
Boolean success= !messageQueue_isEmpty();
if(success) {
*pMessage= m_data.queue[m_data.begin];
m_data.begin= messageQueue_next(m_data.begin);
}
return success;
}
/**
* Write a message to the queue.
* \param message The message to append.
* \return True if the message could be appended, otherwise false.
*/
Boolean messageQueue_write(messageQueue_QueuedMessage message) {
Boolean success= !messageQueue_isFull();
if(success) {
m_data.queue[m_data.end]= message;
m_data.end= messageQueue_next(m_data.end);
}
return success;
}