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