-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathThread.h
111 lines (82 loc) · 2.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
/*
* Thread.h
*
* Created on: Dec 20, 2014
* Author: root
*/
/* 1 Thread及其子类不单独创建、运行、停止,统一在ThreadMonitor或者其子类中管理。 except signalthread
* 2 Thread及其子类可以提供startLoop(线程开始时回调)接口,也可以使用其管理类中的startLoop接口。
* 3 Thread及其子类应该提供stopLoop接口,统一由其管理类stop时调用。
*/
#ifndef GWEN_THREAD_H_
#define GWEN_THREAD_H_
#include <event2/event.h>
#include <event2/event_compat.h>
#include "BlockingQueue.h"
#include <set>
class ThreadMonitor;
class Buffer;
typedef int (*START_LOOP_FUNC)(void *arg);
class Thread
{
public:
Thread(ThreadMonitor *monitor = NULL);
~Thread(void);
static void* threadCallback(void *arg);
int threadIndex(int index = -1);
int start(void);
int wait(void);
int signal(int sig);
int runStartLoop(void);
protected:
ThreadMonitor *monitor_;
START_LOOP_FUNC start_loop_;
void *loop_ptr_;
pthread_t tid_;
int thread_index_;
};
class EventBaseThread : public Thread
{
public:
EventBaseThread(ThreadMonitor *monitor);
~EventBaseThread(void);
event_base *eventBase(void);
int startEventBaseLoop(void);
int stopEventBaseLoop(void);
private:
static void eventCallback(int fd, short which, void *arg);
protected:
struct event_base *base_;
struct event *event_; //监听管理的事件机
int receive_fd_; //管理的接收端
int send_fd_; //管道的发送端
};
class SignalEventBaseThread: public Thread
{
typedef std::set<event *> SignalEventSet;
public:
SignalEventBaseThread(void);
virtual ~SignalEventBaseThread(void);
event_base *eventBase(void);
virtual int signalCallBack(int sig);
void registeSignal(int sig);
int startSignalLoop(void);
private:
static void eventCallback(int fd, short which, void *arg);
static int startSignalLoop(void *arg);
private:
struct event_base *base_;
SignalEventSet sig_set_;
};
class ProcessThread : public Thread
{
public:
ProcessThread(ThreadMonitor *monitor);
~ProcessThread(void);
int processPop(Buffer* &buffer);
int processPush(Buffer* buffer);
int stopProcessLoop(void);
protected:
BlockingQueue process_queue_;
};
#endif /* GWEN_THREAD_H_ */