2015-07-29 18:34:58 -04:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <mutex>
|
|
|
|
#include <atomic>
|
|
|
|
#include <deque>
|
|
|
|
#include <map>
|
|
|
|
#include <string>
|
2015-08-12 21:45:02 -04:00
|
|
|
#include <iostream>
|
2015-07-29 18:34:58 -04:00
|
|
|
|
2015-07-29 20:57:02 -04:00
|
|
|
#include "ThreadQueue.h"
|
2015-09-09 23:29:38 -04:00
|
|
|
#include "Timer.h"
|
2015-07-29 18:34:58 -04:00
|
|
|
|
|
|
|
struct map_string_less : public std::binary_function<std::string,std::string,bool>
|
|
|
|
{
|
|
|
|
bool operator()(const std::string& a,const std::string& b) const
|
|
|
|
{
|
|
|
|
return a.compare(b) < 0;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
class ReferenceCounter {
|
|
|
|
public:
|
|
|
|
mutable std::mutex m_mutex;
|
|
|
|
|
|
|
|
void setRefCount(int rc) {
|
|
|
|
refCount.store(rc);
|
|
|
|
}
|
|
|
|
|
|
|
|
void decRefCount() {
|
|
|
|
refCount.store(refCount.load()-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
int getRefCount() {
|
|
|
|
return refCount.load();
|
|
|
|
}
|
|
|
|
protected:
|
|
|
|
std::atomic_int refCount;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2015-08-11 21:19:55 -04:00
|
|
|
#define REBUFFER_GC_LIMIT 100
|
|
|
|
|
2015-07-29 18:34:58 -04:00
|
|
|
template<class BufferType = ReferenceCounter>
|
|
|
|
class ReBuffer {
|
|
|
|
|
|
|
|
public:
|
|
|
|
BufferType *getBuffer() {
|
|
|
|
BufferType* buf = NULL;
|
|
|
|
for (outputBuffersI = outputBuffers.begin(); outputBuffersI != outputBuffers.end(); outputBuffersI++) {
|
2015-08-11 21:19:55 -04:00
|
|
|
if (!buf && (*outputBuffersI)->getRefCount() <= 0) {
|
|
|
|
buf = (*outputBuffersI);
|
|
|
|
(*outputBuffersI)->setRefCount(0);
|
|
|
|
} else if ((*outputBuffersI)->getRefCount() <= 0) {
|
|
|
|
(*outputBuffersI)->decRefCount();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (buf) {
|
|
|
|
if (outputBuffers.back()->getRefCount() < -REBUFFER_GC_LIMIT) {
|
|
|
|
BufferType *ref = outputBuffers.back();
|
|
|
|
outputBuffers.pop_back();
|
|
|
|
delete ref;
|
2015-07-29 18:34:58 -04:00
|
|
|
}
|
2015-08-11 21:19:55 -04:00
|
|
|
return buf;
|
2015-07-29 18:34:58 -04:00
|
|
|
}
|
|
|
|
|
2015-08-12 21:45:02 -04:00
|
|
|
// if (outputBuffers.size() > 100) {
|
|
|
|
// std::cout << "Buffer over 100.." << std::endl;
|
|
|
|
// }
|
|
|
|
|
2015-07-29 18:34:58 -04:00
|
|
|
buf = new BufferType();
|
|
|
|
outputBuffers.push_back(buf);
|
|
|
|
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
|
|
|
void purge() {
|
|
|
|
while (!outputBuffers.empty()) {
|
|
|
|
BufferType *ref = outputBuffers.front();
|
|
|
|
outputBuffers.pop_front();
|
|
|
|
delete ref;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
private:
|
|
|
|
std::deque<BufferType*> outputBuffers;
|
|
|
|
typename std::deque<BufferType*>::iterator outputBuffersI;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
class IOThread {
|
|
|
|
public:
|
2015-07-29 20:57:02 -04:00
|
|
|
IOThread();
|
2015-07-29 22:52:54 -04:00
|
|
|
virtual ~IOThread();
|
2015-07-29 20:57:02 -04:00
|
|
|
|
|
|
|
static void *pthread_helper(void *context);
|
|
|
|
|
2015-07-29 18:34:58 -04:00
|
|
|
#ifdef __APPLE__
|
2015-07-29 20:57:02 -04:00
|
|
|
virtual void *threadMain();
|
2015-07-29 18:34:58 -04:00
|
|
|
#else
|
2015-07-29 20:57:02 -04:00
|
|
|
virtual void threadMain();
|
2015-07-29 18:34:58 -04:00
|
|
|
#endif
|
2015-07-29 20:57:02 -04:00
|
|
|
|
|
|
|
virtual void setup();
|
|
|
|
virtual void run();
|
|
|
|
virtual void terminate();
|
|
|
|
bool isTerminated();
|
|
|
|
virtual void onBindOutput(std::string name, ThreadQueueBase* threadQueue);
|
|
|
|
virtual void onBindInput(std::string name, ThreadQueueBase* threadQueue);
|
|
|
|
|
|
|
|
void setInputQueue(std::string qname, ThreadQueueBase *threadQueue);
|
|
|
|
void *getInputQueue(std::string qname);
|
|
|
|
void setOutputQueue(std::string qname, ThreadQueueBase *threadQueue);
|
|
|
|
void *getOutputQueue(std::string qname);
|
2015-07-29 18:34:58 -04:00
|
|
|
|
|
|
|
protected:
|
2015-07-29 20:57:02 -04:00
|
|
|
std::map<std::string, ThreadQueueBase *, map_string_less> input_queues;
|
|
|
|
std::map<std::string, ThreadQueueBase *, map_string_less> output_queues;
|
|
|
|
std::atomic_bool terminated;
|
2015-09-09 23:29:38 -04:00
|
|
|
Timer gTimer;
|
2015-07-29 18:34:58 -04:00
|
|
|
};
|