Skip to content
Snippets Groups Projects
Commit 9d8403eb authored by Federico's avatar Federico
Browse files

Reorder Queue methods

parent b7dcc404
Branches
No related tags found
2 merge requests!40Update to Miosix 2.7,!17Draft: Improved miosix build system and fixed cmake scripts
...@@ -88,13 +88,6 @@ public: ...@@ -88,13 +88,6 @@ public:
*/ */
void waitUntilNotFull(); void waitUntilNotFull();
/**
* Get an element from the queue. If the queue is empty, then sleep until
* an element becomes available.
* \param elem an element from the queue
*/
void get(T& elem);
/** /**
* Put an element to the queue. If the queue is full, then sleep until a * Put an element to the queue. If the queue is full, then sleep until a
* place becomes available. * place becomes available.
...@@ -103,46 +96,53 @@ public: ...@@ -103,46 +96,53 @@ public:
void put(const T& elem); void put(const T& elem);
/** /**
* Get an element from the queue, only if the queue is not empty.<br> * Put an element to the queue, only if th queue is not full.<br>
* Can ONLY be used inside an IRQ, or when interrupts are disabled. * Can ONLY be used inside an IRQ, or when interrupts are disabled.
* \param elem an element from the queue. The element is valid only if the * \param elem element to add. The element has been added only if the
* return value is true * return value is true
* \return true if the queue was not empty * \return true if the queue was not full.
*/ */
bool IRQget(T& elem); bool IRQput(const T& elem);
/** /**
* Get an element from the queue, only if the queue is not empty.<br> * Put an element to the queue, only if th queue is not full.<br>
* Can ONLY be used inside an IRQ, or when interrupts are disabled. * Can ONLY be used inside an IRQ, or when interrupts are disabled.
* \param elem an element from the queue. The element is valid only if the * \param elem element to add. The element has been added only if the
* return value is true * return value is true
* \param hppw is not modified if no thread is woken or if the woken thread * \param hppw is not modified if no thread is woken or if the woken thread
* has a lower or equal priority than the currently running thread, else is * has a lower or equal priority than the currently running thread, else is
* set to true * set to true
* \return true if the queue was not empty * \return true if the queue was not full.
*/ */
bool IRQget(T& elem, bool& hppw); bool IRQput(const T& elem, bool& hppw);
/** /**
* Put an element to the queue, only if th queue is not full.<br> * Get an element from the queue. If the queue is empty, then sleep until
* an element becomes available.
* \param elem an element from the queue
*/
void get(T& elem);
/**
* Get an element from the queue, only if the queue is not empty.<br>
* Can ONLY be used inside an IRQ, or when interrupts are disabled. * Can ONLY be used inside an IRQ, or when interrupts are disabled.
* \param elem element to add. The element has been added only if the * \param elem an element from the queue. The element is valid only if the
* return value is true * return value is true
* \return true if the queue was not full. * \return true if the queue was not empty
*/ */
bool IRQput(const T& elem); bool IRQget(T& elem);
/** /**
* Put an element to the queue, only if th queue is not full.<br> * Get an element from the queue, only if the queue is not empty.<br>
* Can ONLY be used inside an IRQ, or when interrupts are disabled. * Can ONLY be used inside an IRQ, or when interrupts are disabled.
* \param elem element to add. The element has been added only if the * \param elem an element from the queue. The element is valid only if the
* return value is true * return value is true
* \param hppw is not modified if no thread is woken or if the woken thread * \param hppw is not modified if no thread is woken or if the woken thread
* has a lower or equal priority than the currently running thread, else is * has a lower or equal priority than the currently running thread, else is
* set to true * set to true
* \return true if the queue was not full. * \return true if the queue was not empty
*/ */
bool IRQput(const T& elem, bool& hppw); bool IRQget(T& elem, bool& hppw);
/** /**
* Clear all items in the queue.<br> * Clear all items in the queue.<br>
...@@ -215,35 +215,59 @@ void Queue<T,len>::waitUntilNotFull() ...@@ -215,35 +215,59 @@ void Queue<T,len>::waitUntilNotFull()
} }
template <typename T, unsigned int len> template <typename T, unsigned int len>
void Queue<T,len>::get(T& elem) void Queue<T,len>::put(const T& elem)
{ {
FastInterruptDisableLock dLock; FastInterruptDisableLock dLock;
IRQwakeWaitingThread(); IRQwakeWaitingThread();
while(isEmpty()) while(isFull())
{ {
waiting=Thread::IRQgetCurrentThread(); waiting=Thread::IRQgetCurrentThread();
Thread::IRQenableIrqAndWait(dLock); Thread::IRQenableIrqAndWait(dLock);
IRQwakeWaitingThread(); IRQwakeWaitingThread();
} }
numElem--; numElem++;
elem=buffer[getPos]; buffer[putPos]=elem;
if(++getPos==len) getPos=0; if(++putPos==len) putPos=0;
} }
template <typename T, unsigned int len> template <typename T, unsigned int len>
void Queue<T,len>::put(const T& elem) bool Queue<T,len>::IRQput(const T& elem)
{
IRQwakeWaitingThread();
if(isFull()) return false;
numElem++;
buffer[putPos]=elem;
if(++putPos==len) putPos=0;
return true;
}
template <typename T, unsigned int len>
bool Queue<T,len>::IRQput(const T& elem, bool& hppw)
{
if(waiting && (Thread::IRQgetCurrentThread()->IRQgetPriority() <
waiting->IRQgetPriority())) hppw=true;
IRQwakeWaitingThread();
if(isFull()) return false;
numElem++;
buffer[putPos]=elem;
if(++putPos==len) putPos=0;
return true;
}
template <typename T, unsigned int len>
void Queue<T,len>::get(T& elem)
{ {
FastInterruptDisableLock dLock; FastInterruptDisableLock dLock;
IRQwakeWaitingThread(); IRQwakeWaitingThread();
while(isFull()) while(isEmpty())
{ {
waiting=Thread::IRQgetCurrentThread(); waiting=Thread::IRQgetCurrentThread();
Thread::IRQenableIrqAndWait(dLock); Thread::IRQenableIrqAndWait(dLock);
IRQwakeWaitingThread(); IRQwakeWaitingThread();
} }
numElem++; numElem--;
buffer[putPos]=elem; elem=buffer[getPos];
if(++putPos==len) putPos=0; if(++getPos==len) getPos=0;
} }
template <typename T, unsigned int len> template <typename T, unsigned int len>
...@@ -270,30 +294,6 @@ bool Queue<T,len>::IRQget(T& elem, bool& hppw) ...@@ -270,30 +294,6 @@ bool Queue<T,len>::IRQget(T& elem, bool& hppw)
return true; return true;
} }
template <typename T, unsigned int len>
bool Queue<T,len>::IRQput(const T& elem)
{
IRQwakeWaitingThread();
if(isFull()) return false;
numElem++;
buffer[putPos]=elem;
if(++putPos==len) putPos=0;
return true;
}
template <typename T, unsigned int len>
bool Queue<T,len>::IRQput(const T& elem, bool& hppw)
{
if(waiting && (Thread::IRQgetCurrentThread()->IRQgetPriority() <
waiting->IRQgetPriority())) hppw=true;
IRQwakeWaitingThread();
if(isFull()) return false;
numElem++;
buffer[putPos]=elem;
if(++putPos==len) putPos=0;
return true;
}
//This partial specialization is meant to to produce compiler errors in case an //This partial specialization is meant to to produce compiler errors in case an
//attempt is made to instantiate a Queue with zero size, as it is forbidden //attempt is made to instantiate a Queue with zero size, as it is forbidden
template<typename T> class Queue<T,0> {}; template<typename T> class Queue<T,0> {};
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment