-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpollmgr.h
107 lines (82 loc) · 2.07 KB
/
pollmgr.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
#ifndef pollmgr_h
#define pollmgr_h
#include <sys/select.h>
#include <vector>
#ifdef __linux__
#include <sys/epoll.h>
#endif
#define MAX_POLL_FDS 128
typedef enum {
CB_NONE = 0x0,
CB_RDONLY = 0x1,
CB_WRONLY = 0x10,
CB_RDWR = 0x11,
CB_MASK = ~0x11,
} poll_flag;
class aio_mgr {
public:
virtual void watch_fd(int fd, poll_flag flag) = 0;
virtual bool unwatch_fd(int fd, poll_flag flag) = 0;
virtual bool is_watched(int fd, poll_flag flag) = 0;
virtual void wait_ready(std::vector<int> *readable, std::vector<int> *writable) = 0;
virtual ~aio_mgr() {}
};
class aio_callback {
public:
virtual void read_cb(int fd) = 0;
virtual void write_cb(int fd) = 0;
virtual ~aio_callback() {}
};
class PollMgr {
public:
PollMgr();
~PollMgr();
static PollMgr *Instance();
static PollMgr *CreateInst();
void add_callback(int fd, poll_flag flag, aio_callback *ch);
void del_callback(int fd, poll_flag flag);
bool has_callback(int fd, poll_flag flag, aio_callback *ch);
void block_remove_fd(int fd);
void wait_loop();
static PollMgr *instance;
static int useful;
static int useless;
private:
pthread_mutex_t m_;
pthread_cond_t changedone_c_;
pthread_t th_;
aio_callback *callbacks_[MAX_POLL_FDS];
aio_mgr *aio_;
bool pending_change_;
};
class SelectAIO : public aio_mgr {
public :
SelectAIO();
~SelectAIO();
void watch_fd(int fd, poll_flag flag);
bool unwatch_fd(int fd, poll_flag flag);
bool is_watched(int fd, poll_flag flag);
void wait_ready(std::vector<int> *readable, std::vector<int> *writable);
private:
fd_set rfds_;
fd_set wfds_;
int highfds_;
int pipefd_[2];
pthread_mutex_t m_;
};
#ifdef __linux__
class EPollAIO : public aio_mgr {
public:
EPollAIO();
~EPollAIO();
void watch_fd(int fd, poll_flag flag);
bool unwatch_fd(int fd, poll_flag flag);
bool is_watched(int fd, poll_flag flag);
void wait_ready(std::vector<int> *readable, std::vector<int> *writable);
private:
int pollfd_;
struct epoll_event ready_[MAX_POLL_FDS];
int fdstatus_[MAX_POLL_FDS];
};
#endif /* __linux */
#endif /* pollmgr_h */