2015-03-17 14:30:53 +00:00
|
|
|
#include <Threader.h>
|
|
|
|
|
2015-03-18 14:28:39 +00:00
|
|
|
std::vector<char*> Threader::threadPool;
|
2015-03-19 14:34:35 +00:00
|
|
|
std::vector<std::condition_variable*> cvPool;
|
|
|
|
std::vector<bool> Threader::readyPool;
|
|
|
|
int Threader::threadId = 0;
|
2015-03-17 14:30:53 +00:00
|
|
|
|
2015-03-18 14:28:39 +00:00
|
|
|
int Threader::getFreeDataSlotId() {
|
2015-03-19 14:34:35 +00:00
|
|
|
int tps = threadPool.size();
|
|
|
|
for(int i = 0; i != (gThreads > tps ? tps : gThreads); ++i) {
|
2015-03-18 14:28:39 +00:00
|
|
|
if(threadPool[i] == NULL) return i;
|
2015-03-17 14:30:53 +00:00
|
|
|
}
|
2015-03-18 14:28:39 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int Threader::getFreeThreadId() {
|
|
|
|
int res;
|
2015-03-19 14:34:35 +00:00
|
|
|
while((res = getFreeDataSlotId()) < 0) Sleep(50);
|
2015-03-18 14:28:39 +00:00
|
|
|
return res;
|
2015-03-17 14:30:53 +00:00
|
|
|
}
|
|
|
|
|
2015-03-19 14:34:35 +00:00
|
|
|
void Threader::fireThread(char *res, void *func(int,std::condition_variable*)) {
|
|
|
|
if(threadId < gThreads) {
|
|
|
|
threadPool.push_back(res);
|
|
|
|
std::condition_variable cv;
|
|
|
|
cvPool.push_back(&cv);
|
|
|
|
std::thread workerThread(func, threadId++, &cv);
|
|
|
|
workerThread.detach();
|
|
|
|
readyPool.push_back(true);
|
|
|
|
cv.notify_one();
|
|
|
|
} else {
|
|
|
|
int id = getFreeThreadId();
|
|
|
|
threadPool[id] = res;
|
|
|
|
readyPool[id] = true;
|
|
|
|
cvPool[id]->notify_one();
|
|
|
|
}
|
2015-03-18 14:28:39 +00:00
|
|
|
}
|
2015-03-17 14:30:53 +00:00
|
|
|
|
2015-03-19 14:34:35 +00:00
|
|
|
void Threader::cleanUp() {
|
|
|
|
threadId = 0;
|
|
|
|
threadPool.clear();
|
|
|
|
cvPool.clear();
|
2015-03-17 14:30:53 +00:00
|
|
|
}
|