TEMPLATE MAGIC FOR GREAT JUSTICE
[invirt/third/libt4.git] / rpc / pollmgr.h
1 #ifndef pollmgr_h
2 #define pollmgr_h 
3
4 #include <sys/select.h>
5 #include <vector>
6 #include <thread>
7
8 #ifdef __linux__
9 #include <sys/epoll.h>
10 #endif
11
12 #define MAX_POLL_FDS 128
13
14 typedef enum {
15         CB_NONE = 0x0,
16         CB_RDONLY = 0x1,
17         CB_WRONLY = 0x10,
18         CB_RDWR = 0x11,
19         CB_MASK = ~0x11,
20 } poll_flag;
21
22 class aio_mgr {
23         public:
24                 virtual void watch_fd(int fd, poll_flag flag) = 0;
25                 virtual bool unwatch_fd(int fd, poll_flag flag) = 0;
26                 virtual bool is_watched(int fd, poll_flag flag) = 0;
27                 virtual void wait_ready(std::vector<int> *readable, std::vector<int> *writable) = 0;
28                 virtual ~aio_mgr() {}
29 };
30
31 class aio_callback {
32         public:
33                 virtual void read_cb(int fd) = 0;
34                 virtual void write_cb(int fd) = 0;
35                 virtual ~aio_callback() {}
36 };
37
38 class PollMgr {
39         public:
40                 PollMgr();
41                 ~PollMgr();
42
43                 static PollMgr *Instance();
44                 static PollMgr *CreateInst();
45
46                 void add_callback(int fd, poll_flag flag, aio_callback *ch);
47                 void del_callback(int fd, poll_flag flag);
48                 bool has_callback(int fd, poll_flag flag, aio_callback *ch);
49                 void block_remove_fd(int fd);
50                 void wait_loop();
51
52
53                 static PollMgr *instance;
54                 static int useful;
55                 static int useless;
56
57         private:
58         std::mutex m_;
59         std::condition_variable changedone_c_;
60         std::thread th_;
61
62                 aio_callback *callbacks_[MAX_POLL_FDS];
63                 aio_mgr *aio_;
64                 bool pending_change_;
65
66 };
67
68 class SelectAIO : public aio_mgr {
69         public :
70
71                 SelectAIO();
72                 ~SelectAIO();
73                 void watch_fd(int fd, poll_flag flag);
74                 bool unwatch_fd(int fd, poll_flag flag);
75                 bool is_watched(int fd, poll_flag flag);
76                 void wait_ready(std::vector<int> *readable, std::vector<int> *writable);
77
78         private:
79
80                 fd_set rfds_;
81                 fd_set wfds_;
82                 int highfds_;
83                 int pipefd_[2];
84
85         std::mutex m_;
86
87 };
88
89 #ifdef __linux__ 
90 class EPollAIO : public aio_mgr {
91         public:
92                 EPollAIO();
93                 ~EPollAIO();
94                 void watch_fd(int fd, poll_flag flag);
95                 bool unwatch_fd(int fd, poll_flag flag);
96                 bool is_watched(int fd, poll_flag flag);
97                 void wait_ready(std::vector<int> *readable, std::vector<int> *writable);
98
99         private:
100                 int pollfd_;
101                 struct epoll_event ready_[MAX_POLL_FDS];
102                 int fdstatus_[MAX_POLL_FDS];
103
104 };
105 #endif /* __linux */
106
107 #endif /* pollmgr_h */
108