-
Notifications
You must be signed in to change notification settings - Fork 0
/
tasker.cpp
58 lines (49 loc) · 1.31 KB
/
tasker.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include "tasker.h"
std::unordered_map<size_t,Worker*> Tasker::clientIdToWorker;
Tasker::Tasker()
{
std::thread *t;
for(int i = 0; i < getWorkersCount(); i++)
{
Worker * worker = new Worker;
ParsedRequestFactory* factory = new CommonParsedRequestFactory();
worker->useParsedRequestFactory(factory);
Matcher* matcher = new Matcher_impl;
matcher->pushRule(std::shared_ptr<Rule>(new DefaultRule));
worker->useMatcher(matcher);
workers.push_back(worker);
t = new std::thread(&Worker::run, worker);
t->detach();
}
workerIterator = workers.begin();
}
void Tasker::pushTask(Task *task)
{
Worker* worker = getWorkerResponsableForTask(task);
worker->addTask(task);
}
Worker *Tasker::getWorkerResponsableForTask(Task* task)
{
auto worker = clientIdToWorker.find(task->getClientId());
if (worker == clientIdToWorker.end())
{
return getNextFreeWorker();
}
else
{
return (*worker).second;
}
}
Worker *Tasker::getNextFreeWorker()
{
if(workerIterator == workers.end())
{
workerIterator = workers.begin();
}
return (*(workerIterator++));
}
int Tasker::getWorkersCount()
{
return 1;
// return std::thread::hardware_concurrency()>2?std::thread::hardware_concurrency():2;
}