-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtaskFactory.cpp
More file actions
71 lines (55 loc) · 1.32 KB
/
taskFactory.cpp
File metadata and controls
71 lines (55 loc) · 1.32 KB
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
59
60
61
62
63
64
65
66
67
68
69
70
/*
* File: taskFactory.cpp
* Author: georgez
*
* Created on 13 October 2014, 16:05
*/
#include <queue>
#include <memory>
#include "taskFactory.h"
#include "task_impl.h"
taskFactory::taskFactory(task* (*mt)() ,int m) {
_makeTask=mt;
_max_cache=std::max(m,4);
}
taskFactory::~taskFactory() {
try{
while(_task_cache.size() > 0){
delete _task_cache.front();
_task_cache.pop();
}
}catch(...){
// Catch All
std::cout << "Exception Caught ~taskFactory" << std::endl;
}
}
task* taskFactory::getTask(std::shared_ptr<session> ps){
task* pTask=nullptr;
// Use mutex to protect queue
{
std::unique_lock<std::mutex> lock(this->_queue_mutex);
if(_task_cache.size() > 0){
pTask=_task_cache.front();
_task_cache.pop();
}
}
if(pTask==nullptr){
pTask=_makeTask();
}
pTask->_pti->clean(ps);
return pTask;
}
void taskFactory::recycleTask(task* tp){
bool d=true;
// use mutex to lock queue
{
std::unique_lock<std::mutex> lock(this->_queue_mutex);
if(_task_cache.size() < (uint)_max_cache){
_task_cache.push(tp);
d=false;
}
}
if(d){
delete tp;
}
}