-
Notifications
You must be signed in to change notification settings - Fork 1
/
thread.h
425 lines (374 loc) · 7.21 KB
/
thread.h
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
/*
Copyright (c) 2012-2014 The SSDB Authors. All rights reserved.
Use of this source code is governed by a BSD-style license that can be
found in the LICENSE file.
*/
#ifndef UTIL_THREAD_H_
#define UTIL_THREAD_H_
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <pthread.h>
#include <queue>
#include <vector>
class Mutex{
private:
pthread_mutex_t mutex;
public:
Mutex(){
pthread_mutex_init(&mutex, NULL);
}
~Mutex(){
pthread_mutex_destroy(&mutex);
}
void lock(){
pthread_mutex_lock(&mutex);
}
void unlock(){
pthread_mutex_unlock(&mutex);
}
};
class Locking{
private:
Mutex *mutex;
// No copying allowed
Locking(const Locking&);
void operator=(const Locking&);
public:
Locking(Mutex *mutex){
this->mutex = mutex;
this->mutex->lock();
}
~Locking(){
this->mutex->unlock();
}
};
/*
class Semaphore {
private:
pthread_cond_t cond;
pthread_mutex_t mutex;
public:
Semaphore(Mutex* mu){
pthread_cond_init(&cond, NULL);
pthread_mutex_init(&mutex, NULL);
}
~CondVar(){
pthread_cond_destroy(&cond);
pthread_mutex_destroy(&mutex);
}
void wait();
void signal();
};
*/
// Thread safe queue
template <class T>
class Queue{
private:
pthread_cond_t cond;
pthread_mutex_t mutex;
std::queue<T> items;
public:
Queue();
~Queue();
bool empty();
int size();
int push(const T item);
// TODO: with timeout
int pop(T *data);
};
// Selectable queue, multi writers, single reader
template <class T>
class SelectableQueue{
private:
int fds[2];
pthread_mutex_t mutex;
std::queue<T> items;
public:
SelectableQueue();
~SelectableQueue();
int fd(){
return fds[0];
}
// multi writer
int push(const T item);
// single reader
int pop(T *data);
};
template<class W, class JOB>
class WorkerPool{
public:
class Worker{
public:
Worker(){};
Worker(const std::string &name);
virtual ~Worker(){}
int id;
virtual void init(){}
virtual void destroy(){}
virtual int proc(JOB *job) = 0;
private:
protected:
std::string name;
};
private:
std::string name;
Queue<JOB> jobs;
SelectableQueue<JOB> results;
int num_workers;
std::vector<pthread_t> tids;
bool started;
struct run_arg{
int id;
WorkerPool *tp;
};
static void* _run_worker(void *arg);
public:
WorkerPool(const char *name="");
~WorkerPool();
int fd(){
return results.fd();
}
int start(int num_workers);
int stop();
int push(JOB job);
int pop(JOB *job);
};
template <class T>
Queue<T>::Queue(){
pthread_cond_init(&cond, NULL);
pthread_mutex_init(&mutex, NULL);
}
template <class T>
Queue<T>::~Queue(){
pthread_cond_destroy(&cond);
pthread_mutex_destroy(&mutex);
}
template <class T>
bool Queue<T>::empty(){
bool ret = false;
if(pthread_mutex_lock(&mutex) != 0){
return -1;
}
ret = items.empty();
pthread_mutex_unlock(&mutex);
return ret;
}
template <class T>
int Queue<T>::size(){
int ret = -1;
if(pthread_mutex_lock(&mutex) != 0){
return -1;
}
ret = items.size();
pthread_mutex_unlock(&mutex);
return ret;
}
template <class T>
int Queue<T>::push(const T item){
if(pthread_mutex_lock(&mutex) != 0){
return -1;
}
{
items.push(item);
}
pthread_mutex_unlock(&mutex);
pthread_cond_signal(&cond);
return 1;
}
template <class T>
int Queue<T>::pop(T *data){
if(pthread_mutex_lock(&mutex) != 0){
return -1;
}
{
// 必须放在循环中, 因为 pthread_cond_wait 可能抢不到锁而被其它处理了
while(items.empty()){
//fprintf(stderr, "%d wait\n", pthread_self());
if(pthread_cond_wait(&cond, &mutex) != 0){
//fprintf(stderr, "%s %d -1!\n", __FILE__, __LINE__);
return -1;
}
//fprintf(stderr, "%d wait 2\n", pthread_self());
}
*data = items.front();
//fprintf(stderr, "%d job: %d\n", pthread_self(), (int)*data);
items.pop();
}
if(pthread_mutex_unlock(&mutex) != 0){
//fprintf(stderr, "error!\n");
return -1;
}
//fprintf(stderr, "%d wait end 2, job: %d\n", pthread_self(), (int)*data);
return 1;
}
template <class T>
SelectableQueue<T>::SelectableQueue(){
if(pipe(fds) == -1){
exit(0);
}
pthread_mutex_init(&mutex, NULL);
}
template <class T>
SelectableQueue<T>::~SelectableQueue(){
pthread_mutex_destroy(&mutex);
close(fds[0]);
close(fds[1]);
}
template <class T>
int SelectableQueue<T>::push(const T item){
if(pthread_mutex_lock(&mutex) != 0){
return -1;
}
{
items.push(item);
}
if(::write(fds[1], "1", 1) == -1){
exit(0);
}
pthread_mutex_unlock(&mutex);
return 1;
}
template <class T>
int SelectableQueue<T>::pop(T *data){
int n, ret = 1;
char buf[1];
while(1){
n = ::read(fds[0], buf, 1);
if(n < 0){
if(errno == EINTR){
continue;
}else{
return -1;
}
}else if(n == 0){
ret = -1;
}else{
if(pthread_mutex_lock(&mutex) != 0){
return -1;
}
{
if(items.empty()){
fprintf(stderr, "%s %d error!\n", __FILE__, __LINE__);
pthread_mutex_unlock(&mutex);
return -1;
}
*data = items.front();
items.pop();
}
pthread_mutex_unlock(&mutex);
}
break;
}
return ret;
}
template<class W, class JOB>
WorkerPool<W, JOB>::WorkerPool(const char *name){
this->name = name;
this->started = false;
}
template<class W, class JOB>
WorkerPool<W, JOB>::~WorkerPool(){
if(started){
stop();
}
}
template<class W, class JOB>
int WorkerPool<W, JOB>::push(JOB job){
return this->jobs.push(job);
}
template<class W, class JOB>
int WorkerPool<W, JOB>::pop(JOB *job){
return this->results.pop(job);
}
template<class W, class JOB>
void* WorkerPool<W, JOB>::_run_worker(void *arg){
struct run_arg *p = (struct run_arg*)arg;
int id = p->id;
WorkerPool *tp = p->tp;
delete p;
W w(tp->name);
Worker *worker = (Worker *)&w;
worker->id = id;
worker->init();
while(1){
JOB job;
if(tp->jobs.pop(&job) == -1){
fprintf(stderr, "jobs.pop error\n");
::exit(0);
break;
}
worker->proc(&job);
if(tp->results.push(job) == -1){
fprintf(stderr, "results.push error\n");
::exit(0);
break;
}
}
worker->destroy();
return (void *)NULL;
}
template<class W, class JOB>
int WorkerPool<W, JOB>::start(int num_workers){
this->num_workers = num_workers;
if(started){
return 0;
}
int err;
pthread_t tid;
for(int i=0; i<num_workers; i++){
struct run_arg *arg = new run_arg();
arg->id = i;
arg->tp = this;
err = pthread_create(&tid, NULL, &WorkerPool::_run_worker, arg);
if(err != 0){
fprintf(stderr, "can't create thread: %s\n", strerror(err));
}else{
tids.push_back(tid);
}
}
started = true;
return 0;
}
template<class W, class JOB>
int WorkerPool<W, JOB>::stop(){
// TODO: notify works quit and wait
for(int i=0; i<tids.size(); i++){
#ifdef OS_ANDROID
#else
pthread_cancel(tids[i]);
#endif
}
return 0;
}
#if 0
class MyWorker : public WorkerPool<MyWorker, int>::Worker{
public:
int proc(int *job){
*job = (id + 1) * 100000 + *job;
return 0;
}
};
int main(){
int num_jobs = 1000;
WorkerPool<MyWorker, int> tp(10);
tp.start();
for(int i=0; i<num_jobs; i++){
//usleep(200 * 1000);
//printf("job: %d\n", i);
tp.push_job(i);
}
printf("add end\n");
for(int i=0; i<num_jobs; i++){
int job;
tp.pop_result(&job);
printf("result: %d, %d\n", i, job);
}
printf("end\n");
//tp.stop();
return 0;
}
#endif
#endif