-
Notifications
You must be signed in to change notification settings - Fork 0
/
taskQueue.cc
258 lines (219 loc) · 6.13 KB
/
taskQueue.cc
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
#include "taskQueue.h"
TaskQueueManager::TaskQueueManager()
{
pthread_mutex_init(&mutex_,NULL);
}
TaskQueueManager::~TaskQueueManager()
{
pthread_mutex_lock(&mutex_);
for (size_t i = 0; i < datas_.size(); i++)
{
DataObj *tmp = NULL;
tmp = datas_.front();
datas_.pop();
if (NULL != tmp)
{
delete tmp;
tmp = NULL;
}
}
}
// push data to queue
void TaskQueueManager::push(DataObj *t)
{
pthread_mutex_lock(&mutex_);
datas_.push(t);
pthread_mutex_unlock(&mutex_);
}
// pop DataObj from queue
DataObj* TaskQueueManager::pop()
{
pthread_mutex_lock(&mutex_);
DataObj *tmp = NULL;
if (!datas_.empty())
{
tmp = datas_.front();
datas_.pop();
}
pthread_mutex_unlock(&mutex_);
return tmp;
}
// DataProcessManager function
DataProcessManager::DataProcessManager(int thread_num, TaskQueueManager* itk):
workThreadNum_(thread_num),
Itk_(itk)
{}
DataProcessManager::~DataProcessManager()
{
}
// start process,(create thread and work)
void DataProcessManager::start()
{
pthread_t thrManger;
pthread_create(&thrManger, NULL, managerThreadFunc, (void*)this);
}
// mangerThreadFunc: create/destory/check/update workThread
void * DataProcessManager::managerThreadFunc(void *arg)
{
DataProcessManager *processer = (DataProcessManager*)arg;
// get work threadNum
size_t workThreadNum = processer->workThreadNum_;
// workThread map, key:pthreadId, Value:thread OBJ
std::map<pthread_t,WorkPthread*>KpidVPthreadMap;
int err = 0;
// init workThread
err = processer->initWorkThreadMap(KpidVPthreadMap, workThreadNum);
if(err)
{
cout << "initWorkThreadMap failed" << endl;
cout << strerror(errno);
//return;
}
// do loop, 1.get workThreadNum, now the workThreadNum not change
// 2. update workThreadMap with new workThreadNum
// 3. check workThreadStatus, if dead, try create again
while(true)
{
// int workThreadNum = arg->workThreadNum;
int err = 0;
err = processer->checkProcessThreadStatus(KpidVPthreadMap);
if(!err)
{
// no thread kill
}
}
return 0;
}
int DataProcessManager::initWorkThreadMap(std::map<pthread_t,WorkPthread*>&KpidVPthreadMap, size_t thread_num)
{
for (size_t i = 0; i < thread_num; i++)
{
int err = 0;
err = addWorkPthreadToMap(KpidVPthreadMap);
if(err)
{
cout << "addWorkPthreadToMap failed" << endl;
return -2;
}
}
return 0;
}
int DataProcessManager::addWorkPthreadToMap(std::map<pthread_t,WorkPthread*>&KpidVPthreadMap)
{
try
{
WorkPthread *processerObj = new WorkPthread;
// work thread is running
processerObj->running_ = 1;
// get data interface
processerObj->taskQueueMgr_ = Itk_;
int err = 0;
err = createProcessThread(processerObj);
if(err)
{
cout << "addWorkPthreadToMap::createProcessThread failed" << endl;
return -2;
}
KpidVPthreadMap[processerObj->pthreadId_] = processerObj;
return 0;
}
catch(...)
{
cout << "addWorkPthreadToMap failed" << endl;
}
return 0;
}
// cheack all thread status
int DataProcessManager::checkProcessThreadStatus(std::map<pthread_t,WorkPthread*>&KpidVPthreadMap)
{
std::map<pthread_t, WorkPthread*>::iterator mapItr;
for (mapItr = KpidVPthreadMap.begin(); mapItr != KpidVPthreadMap.end(); ++mapItr)
{
pthread_t id;
int err = 0;
id = (*mapItr).first;
err = judgePthreadStatus(id);
if (err)
{
createProcessThread((*mapItr).second);
KpidVPthreadMap[(*mapItr).second->pthreadId_] = (*mapItr).second;
KpidVPthreadMap.erase(mapItr);
}
}
return 0;
}
// create Process Thread
int DataProcessManager::createProcessThread(WorkPthread *StrPthread)
{
int err = 0;
err = pthread_create(&(StrPthread->pthreadId_), NULL, processThreadFunc, (void*)StrPthread);
if (err)
{
cout << "createProcessThread failed" << endl;
return -2;
}
usleep(1000);
return 0;
}
// judge all thread status
int DataProcessManager::judgePthreadStatus(pthread_t tid)
{
int pthread_kill_err = 0;
pthread_kill_err = pthread_kill(tid, 0);
if (ESRCH == pthread_kill_err)
{
cout << "judgePthreadStatu: thread already kill or exit" << endl;
return -2;
}
else if(pthread_kill_err == EINVAL)
{
cout << "judgePthreadSttu: send signal illegal" << endl;
return -2;
}
return 0;
}
// data workThread function
void *DataProcessManager::processThreadFunc(void *arg)
{
WorkPthread *pthread = (WorkPthread*)arg;
TaskQueueManager *taskQueueMgr = pthread->taskQueueMgr_;
// To Do
// do some initial work, e.g: yuv handle
sleep(1);
DataObj *obj;
try
{
while(pthread->running_)
{
// get data
// analysis
// call callback function to processer data after analysis
obj = taskQueueMgr->pop();
if (NULL == obj)
{
cout << "the obj is null" << endl;
usleep(100);
continue;
}
// TODO process
// TODO After process
obj->callBack(obj->data_);
if (obj)
{
// release obj member ,then release obj
// TO DO relase obj's member first
delete obj;
obj = NULL;
}
}
}
catch(...)
{
if(obj)
{
delete obj;
obj = NULL;
}
}
return NULL;
}