mirror of
https://github.com/slsdetectorgroup/slsDetectorPackage.git
synced 2025-04-22 03:40:04 +02:00
trying to get atomic circular fifo
git-svn-id: file:///afs/psi.ch/project/sls_det_software/svn/slsDetectorSoftware@663 951219d9-93cf-4727-9268-0efd64621fa3
This commit is contained in:
parent
f908226713
commit
23bbefd4a2
@ -154,6 +154,7 @@ typedef struct {
|
|||||||
int ymax; /**< is the roi ymax (in channel number)*/
|
int ymax; /**< is the roi ymax (in channel number)*/
|
||||||
} ROI ;
|
} ROI ;
|
||||||
|
|
||||||
|
|
||||||
/* /\* */
|
/* /\* */
|
||||||
/* @short structure for a generic integer array */
|
/* @short structure for a generic integer array */
|
||||||
/* *\/ */
|
/* *\/ */
|
||||||
|
Binary file not shown.
@ -10,6 +10,8 @@
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
@ -131,3 +131,111 @@ unsigned int CircularFifo<Element>::increment(unsigned int idx_) const
|
|||||||
}
|
}
|
||||||
|
|
||||||
#endif /* CIRCULARFIFO_H_ */
|
#endif /* CIRCULARFIFO_H_ */
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
#ifndef CIRCULARFIFO_H_
|
||||||
|
#define CIRCULARFIFO_H_
|
||||||
|
|
||||||
|
#include "sls_detector_defs.h"
|
||||||
|
|
||||||
|
#include "/usr/include/alsa/atomic.h"
|
||||||
|
#include <vector>
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
template<typename Element>
|
||||||
|
class CircularFifo {
|
||||||
|
public:
|
||||||
|
|
||||||
|
CircularFifo(unsigned int Size) : tail(0), head(0){
|
||||||
|
Capacity = Size + 1;
|
||||||
|
array.resize(Capacity);
|
||||||
|
}
|
||||||
|
virtual ~CircularFifo() {}
|
||||||
|
|
||||||
|
bool push(Element*& item_);
|
||||||
|
bool pop(Element*& item_);
|
||||||
|
|
||||||
|
bool wasEmpty() const;
|
||||||
|
bool wasFull() const;
|
||||||
|
bool isLockFree() const;
|
||||||
|
|
||||||
|
private:
|
||||||
|
vector <Element*> array;
|
||||||
|
unsigned int Capacity;
|
||||||
|
|
||||||
|
std::atomic<unsigned int> tail; // input index
|
||||||
|
std::atomic<unsigned int> head; // output index
|
||||||
|
|
||||||
|
unsigned int increment(unsigned int idx_) const;
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
template<typename Element>
|
||||||
|
bool CircularFifo<Element>::push(Element*& item_)
|
||||||
|
{
|
||||||
|
auto currentTail = tail.load();
|
||||||
|
auto nextTail = increment(currentTail);
|
||||||
|
if(nextTail != head.load())
|
||||||
|
{
|
||||||
|
array[currentTail] = item_;
|
||||||
|
tail.store(nextTail);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// queue was full
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
template<typename Element>
|
||||||
|
bool CircularFifo<Element>::pop(Element*& item_)
|
||||||
|
{
|
||||||
|
const auto currentHead = head.load();
|
||||||
|
if(currentHead == tail.load())
|
||||||
|
return false; // empty queue
|
||||||
|
|
||||||
|
item_ = array[currentHead];
|
||||||
|
head.store(increment(currentHead));
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
template<typename Element>
|
||||||
|
bool CircularFifo<Element>::wasEmpty() const
|
||||||
|
{
|
||||||
|
return (head.load() == tail.load());
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
template<typename Element>
|
||||||
|
bool CircularFifo<Element>::wasFull() const
|
||||||
|
{
|
||||||
|
const auto nextTail = increment(tail.load());
|
||||||
|
return (nextTail == head.load());
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename Element>
|
||||||
|
bool CircularFifo<Element>::isLockFree() const
|
||||||
|
{
|
||||||
|
return (tail.is_lock_free() && head.is_lock_free());
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
template<typename Element>
|
||||||
|
unsigned int CircularFifo<Element>::increment(unsigned int idx_) const
|
||||||
|
{
|
||||||
|
// increment or wrap
|
||||||
|
// =================
|
||||||
|
// index++;
|
||||||
|
// if(index == array.lenght) -> index = 0;
|
||||||
|
//
|
||||||
|
//or as written below:
|
||||||
|
// index = (index+1) % array.length
|
||||||
|
return (idx_ + 1) % Capacity;
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif */
|
||||||
|
Loading…
x
Reference in New Issue
Block a user