kopia lustrzana https://github.com/OpenRTX/OpenRTX
RingBuffer class: implemented eraseElement() function, rewritten the pop() function in a cleaner way, fixed a bug in the pop() function causing the not_full condition variable to be never rised.
rodzic
29ad0830f0
commit
ff7a28ff59
|
@ -108,25 +108,27 @@ public:
|
||||||
{
|
{
|
||||||
pthread_mutex_lock(&mutex);
|
pthread_mutex_lock(&mutex);
|
||||||
|
|
||||||
if(numElements == 0)
|
if((numElements == 0) && (blocking == false))
|
||||||
{
|
|
||||||
if(blocking)
|
|
||||||
{
|
{
|
||||||
|
// No elements present and non-blocking call: unlock mutex and return
|
||||||
|
pthread_mutex_unlock(&mutex);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// The call is blocking: wait until there is something into the queue
|
||||||
while(numElements == 0)
|
while(numElements == 0)
|
||||||
{
|
{
|
||||||
pthread_cond_wait(¬_empty, &mutex);
|
pthread_cond_wait(¬_empty, &mutex);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
pthread_mutex_unlock(&mutex);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
// At least one element present pop one.
|
||||||
elem = data[readPos];
|
elem = data[readPos];
|
||||||
readPos = (readPos + 1) % N;
|
readPos = (readPos + 1) % N;
|
||||||
|
|
||||||
|
// Signal that the queue is no more full
|
||||||
|
if(numElements >= N) pthread_cond_signal(¬_full);
|
||||||
numElements -= 1;
|
numElements -= 1;
|
||||||
|
|
||||||
pthread_mutex_unlock(&mutex);
|
pthread_mutex_unlock(&mutex);
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
|
@ -152,6 +154,27 @@ public:
|
||||||
return numElements >= N;
|
return numElements >= N;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Discard one element from the buffer's tail, creating a new empty slot.
|
||||||
|
* In case the buffer is full calling this function unlocks the eventual
|
||||||
|
* threads waiting to push data.
|
||||||
|
*/
|
||||||
|
void eraseElement()
|
||||||
|
{
|
||||||
|
// Nothing to erase
|
||||||
|
if(numElements == 0) return;
|
||||||
|
|
||||||
|
pthread_mutex_lock(&mutex);
|
||||||
|
|
||||||
|
// Chomp away one element just by advancing the read pointer.
|
||||||
|
readPos = (readPos + 1) % N;
|
||||||
|
|
||||||
|
if(numElements >= N) pthread_cond_signal(¬_full);
|
||||||
|
numElements -= 1;
|
||||||
|
|
||||||
|
pthread_mutex_unlock(&mutex);
|
||||||
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
||||||
size_t readPos; ///< Read pointer.
|
size_t readPos; ///< Read pointer.
|
||||||
|
|
Ładowanie…
Reference in New Issue