… | |
… | |
19 | #ifndef VPE_IOM_H__ |
19 | #ifndef VPE_IOM_H__ |
20 | #define VPE_IOM_H__ |
20 | #define VPE_IOM_H__ |
21 | |
21 | |
22 | #include <vector> |
22 | #include <vector> |
23 | |
23 | |
|
|
24 | #include <cassert> |
|
|
25 | |
24 | #include <sys/poll.h> |
26 | #include <sys/poll.h> |
25 | |
27 | |
|
|
28 | #include "callback.h" |
26 | #include "slog.h" |
29 | #include "slog.h" |
27 | |
30 | |
28 | typedef double tstamp; |
31 | typedef double tstamp; |
29 | |
32 | |
30 | extern tstamp NOW; |
33 | extern tstamp NOW; |
31 | |
34 | |
32 | template<class R, class A> class callback; |
|
|
33 | struct io_watcher; |
35 | struct io_watcher; |
34 | struct time_watcher; |
36 | struct time_watcher; |
35 | |
37 | |
36 | class io_manager { |
38 | class io_manager { |
37 | vector<pollfd> pfs; |
39 | vector<pollfd> pfs; |
38 | vector<const io_watcher *> iow; |
40 | vector<io_watcher *> iow; |
39 | vector<time_watcher *> tw; // actually a heap |
41 | vector<time_watcher *> tw; // actually a heap |
40 | |
42 | |
41 | void idle_cb (tstamp &ts); time_watcher *idle; |
43 | void idle_cb (time_watcher &w); time_watcher *idle; |
42 | public: |
44 | public: |
43 | |
45 | |
44 | void reschedule_time_watchers (); |
46 | void reschedule_time_watchers (); |
45 | |
47 | |
46 | // register a watcher |
48 | // register a watcher |
47 | void reg (int fd, short events, io_watcher *w); |
49 | void reg (io_watcher *w); |
48 | void unreg (io_watcher *w); |
50 | void unreg (io_watcher *w); |
49 | void reg (time_watcher *w); |
51 | void reg (time_watcher *w); |
50 | void unreg (time_watcher *w); |
52 | void unreg (time_watcher *w); |
51 | |
53 | |
52 | void loop (); |
54 | void loop (); |
53 | |
55 | |
54 | io_manager (); |
56 | io_manager (); |
55 | ~io_manager (); |
57 | ~io_manager (); |
56 | }; |
58 | }; |
57 | |
59 | |
58 | extern io_manager iom; |
60 | extern io_manager iom; // a singleton, together with it's construction/destruction problems. |
59 | |
61 | |
60 | template<class R, class A> |
62 | struct io_watcher : callback2<void, io_watcher &, short> { |
61 | class callback { |
63 | pollfd *p; |
62 | struct object { }; |
|
|
63 | |
64 | |
64 | void *obj; |
65 | template<class O1, class O2> |
65 | R (object::*meth)(A arg); |
66 | io_watcher (O1 *object, void (O2::*method)(io_watcher &, short)) |
|
|
67 | : callback2<void, io_watcher &, short>(object,method) |
|
|
68 | { } |
66 | |
69 | |
67 | // a proxy is a kind of recipe on how to call a specific class method |
70 | ~io_watcher (); |
68 | struct proxy_base { |
|
|
69 | virtual R call (void *obj, R (object::*meth)(A), A arg) = 0; |
|
|
70 | }; |
|
|
71 | template<class O1, class O2> |
|
|
72 | struct proxy : proxy_base { |
|
|
73 | virtual R call (void *obj, R (object::*meth)(A), A arg) |
|
|
74 | { |
|
|
75 | ((reinterpret_cast<O1 *>(obj)) ->* (reinterpret_cast<R (O2::*)(A)>(meth))) |
|
|
76 | (arg); |
|
|
77 | } |
|
|
78 | }; |
|
|
79 | |
71 | |
80 | proxy_base *prxy; |
72 | void set(int fd, short events) |
81 | |
|
|
82 | public: |
|
|
83 | template<class O1, class O2> |
|
|
84 | callback (O1 *object, R (O2::*method)(A)) |
|
|
85 | { |
73 | { |
86 | static proxy<O1,O2> p; |
74 | assert (p); |
87 | obj = reinterpret_cast<void *>(object); |
75 | p->fd = fd; |
88 | meth = reinterpret_cast<R (object::*)(A)>(method); |
76 | p->events = events; |
89 | prxy = &p; |
|
|
90 | } |
77 | } |
91 | |
78 | |
92 | R call(A arg) const |
79 | void set(short events) |
93 | { |
80 | { |
94 | return prxy->call (obj, meth, arg); |
81 | assert (p); |
95 | } |
82 | p->events = events; |
96 | |
|
|
97 | R operator ()(A arg) const |
|
|
98 | { |
|
|
99 | return call (arg); |
|
|
100 | } |
|
|
101 | }; |
|
|
102 | |
|
|
103 | struct io_watcher : callback<void, short> { |
|
|
104 | template<class O1, class O2> |
|
|
105 | io_watcher (O1 *object, void (O2::*method)(short revents)) |
|
|
106 | : callback<void, short>(object,method) |
|
|
107 | { } |
|
|
108 | |
|
|
109 | ~io_watcher () |
|
|
110 | { |
|
|
111 | iom.unreg (this); |
|
|
112 | } |
83 | } |
113 | |
84 | |
114 | void start (int fd, short events) |
85 | void start (int fd, short events) |
115 | { |
86 | { |
116 | iom.reg (fd, events, this); |
87 | iom.reg (this); // make sure pfd is set |
|
|
88 | |
|
|
89 | p->fd = fd; |
|
|
90 | p->events = events; |
117 | } |
91 | } |
118 | |
92 | |
119 | void stop () |
93 | void stop () |
120 | { |
94 | { |
121 | iom.unreg (this); |
95 | iom.unreg (this); |
122 | } |
96 | } |
123 | }; |
97 | }; |
124 | |
98 | |
125 | #define TSTAMP_CANCEL -1. |
99 | #define TSTAMP_CANCEL -1. |
126 | |
100 | |
127 | struct time_watcher : callback<void, tstamp &> { |
101 | struct time_watcher : callback1<void, time_watcher &> { |
128 | bool registered; // already registered? |
102 | bool registered; // already registered? |
129 | tstamp at; |
103 | tstamp at; |
130 | |
104 | |
131 | template<class O1, class O2> |
105 | template<class O1, class O2> |
132 | time_watcher (O1 *object, void (O2::*method)(tstamp &)) |
106 | time_watcher (O1 *object, void (O2::*method)(time_watcher &)) |
133 | : callback<void, tstamp &>(object,method) |
107 | : callback1<void, time_watcher &>(object,method) |
134 | , registered(false) |
108 | , registered(false) |
135 | { } |
109 | { } |
136 | |
110 | |
137 | ~time_watcher () |
111 | ~time_watcher (); |
138 | { |
|
|
139 | iom.unreg (this); |
|
|
140 | } |
|
|
141 | |
112 | |
142 | void set (tstamp when); |
113 | void set (tstamp when); |
143 | void trigger (); |
114 | void trigger (); |
144 | |
115 | |
145 | void operator ()() |
116 | void operator ()() |
… | |
… | |
159 | } |
130 | } |
160 | |
131 | |
161 | void reset (tstamp when = TSTAMP_CANCEL) |
132 | void reset (tstamp when = TSTAMP_CANCEL) |
162 | { |
133 | { |
163 | stop (); |
134 | stop (); |
|
|
135 | |
164 | at = when; |
136 | at = when; |
165 | } |
137 | } |
166 | }; |
138 | }; |
167 | |
139 | |
168 | #endif |
140 | #endif |