This commit is contained in:
Erik Frojdh 2020-02-03 11:04:49 +01:00
parent 9f79f132b7
commit a3c686d271
4 changed files with 89 additions and 103 deletions

View File

@ -17,7 +17,7 @@
#include <pthread.h>
#include "analogDetector.h"
#include "circularFifo.h"
#include "CircularFifo.h"
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>

View File

@ -7,16 +7,23 @@
* modified by the sls detetor group
* */
#include <iostream>
#include <semaphore.h>
#include <vector>
#include <iostream>
/** Circular Fifo (a.k.a. Circular Buffer)
* Thread safe for one reader, and one writer */
template<typename Element>
class CircularFifo {
public:
template <typename Element> class CircularFifo {
private:
std::vector<Element *> array;
unsigned int tail; // input index
unsigned int head; // output index
unsigned int Capacity;
mutable sem_t data_mutex;
mutable sem_t free_mutex;
unsigned int increment(unsigned int idx_) const;
public:
CircularFifo(unsigned int Size) : tail(0), head(0) {
Capacity = Size + 1;
array.resize(Capacity);
@ -36,34 +43,20 @@ public:
int getDataValue() const;
int getFreeValue() const;
private:
std::vector <Element*> array;
unsigned int tail; // input index
unsigned int head; // output index
unsigned int Capacity;
mutable sem_t data_mutex;
mutable sem_t free_mutex;
unsigned int increment(unsigned int idx_) const;
};
template<typename Element>
int CircularFifo<Element>::getDataValue() const
{
template <typename Element> int CircularFifo<Element>::getDataValue() const {
int value;
sem_getvalue(&data_mutex, &value);
return value;
}
template<typename Element>
int CircularFifo<Element>::getFreeValue() const
{
template <typename Element> int CircularFifo<Element>::getFreeValue() const {
int value;
sem_getvalue(&free_mutex, &value);
return value;
}
/** Producer only: Adds item to the circular queue.
* If queue is full at 'push' operation no update/overwrite
* will happen, it is up to the caller to handle this case
@ -72,8 +65,7 @@ int CircularFifo<Element>::getFreeValue() const
* \param no_block if true, return immediately if fifo is full
* \return whether operation was successful or not */
template <typename Element>
bool CircularFifo<Element>::push(Element*& item_, bool no_block)
{
bool CircularFifo<Element>::push(Element *&item_, bool no_block) {
// check for fifo full
if (no_block && isFull())
return false;
@ -93,8 +85,7 @@ bool CircularFifo<Element>::push(Element*& item_, bool no_block)
* \param no_block if true, return immediately if fifo is full
* \return whether operation was successful or not */
template <typename Element>
bool CircularFifo<Element>::pop(Element*& item_, bool no_block)
{
bool CircularFifo<Element>::pop(Element *&item_, bool no_block) {
// check for fifo empty
if (no_block && isEmpty())
return false;
@ -111,9 +102,7 @@ bool CircularFifo<Element>::pop(Element*& item_, bool no_block)
* as the Procuder adds more items.
*
* \return true if circular buffer is empty */
template<typename Element>
bool CircularFifo<Element>::isEmpty() const
{
template <typename Element> bool CircularFifo<Element>::isEmpty() const {
return (getDataValue() == 0);
}
@ -122,9 +111,7 @@ bool CircularFifo<Element>::isEmpty() const
* as the Consumer catches up.
*
* \return true if circular buffer is full. */
template<typename Element>
bool CircularFifo<Element>::isFull() const
{
template <typename Element> bool CircularFifo<Element>::isFull() const {
return (getFreeValue() == 0);
}
@ -134,8 +121,7 @@ bool CircularFifo<Element>::isFull() const
* \param idx_ the index to the incremented/wrapped
* \return new value for the index */
template <typename Element>
unsigned int CircularFifo<Element>::increment(unsigned int idx_) const
{
unsigned int CircularFifo<Element>::increment(unsigned int idx_) const {
// increment or wrap
// =================
// index++;
@ -146,4 +132,3 @@ unsigned int CircularFifo<Element>::increment(unsigned int idx_) const
idx_ = (idx_ + 1) % Capacity;
return idx_;
}

View File

@ -12,7 +12,7 @@
#include "sls_detector_defs.h"
#include "logger.h"
#include "circularFifo.h"
#include "CircularFifo.h"
class Fifo : private virtual slsDetectorDefs {

View File

@ -1,4 +1,5 @@
target_sources(tests PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/test-GeneralData.cpp
${CMAKE_CURRENT_SOURCE_DIR}/test-FileNames.cpp
${CMAKE_CURRENT_SOURCE_DIR}/test-CircularFifo.cpp
)