… | |
… | |
20 | #ifndef VPE_IOM_H__ |
20 | #ifndef VPE_IOM_H__ |
21 | #define VPE_IOM_H__ |
21 | #define VPE_IOM_H__ |
22 | |
22 | |
23 | #include <cassert> |
23 | #include <cassert> |
24 | |
24 | |
|
|
25 | #include "callback.h" |
25 | #include "rxvtvec.h" |
26 | #include "rxvtvec.h" |
26 | #include "callback.h" |
|
|
27 | |
27 | |
|
|
28 | #ifndef IOM_IO |
28 | #define IOM_IO 1 |
29 | # define IOM_IO 1 |
|
|
30 | #endif |
|
|
31 | #ifndef IOM_TIME |
29 | #define IOM_TIME 1 |
32 | # define IOM_TIME 1 |
|
|
33 | #endif |
|
|
34 | #ifndef IOM_CHECK |
30 | #define IOM_CHECK 1 |
35 | # define IOM_CHECK 1 |
|
|
36 | #endif |
|
|
37 | #ifndef IOM_IDLE |
31 | #define IOM_IDLE 0 |
38 | # define IOM_IDLE 0 |
|
|
39 | #endif |
|
|
40 | |
|
|
41 | typedef double tstamp; |
|
|
42 | extern tstamp NOW; |
32 | |
43 | |
33 | #if IOM_IO |
44 | #if IOM_IO |
34 | typedef double tstamp; |
|
|
35 | extern tstamp NOW; |
|
|
36 | |
|
|
37 | struct io_watcher; |
45 | struct io_watcher; |
38 | #endif |
46 | #endif |
39 | #if IOM_TIME |
47 | #if IOM_TIME |
40 | struct time_watcher; |
48 | struct time_watcher; |
41 | #endif |
49 | #endif |
… | |
… | |
44 | #endif |
52 | #endif |
45 | #if IOM_IDLE |
53 | #if IOM_IDLE |
46 | struct idle_watcher; |
54 | struct idle_watcher; |
47 | #endif |
55 | #endif |
48 | |
56 | |
|
|
57 | template<class watcher> |
|
|
58 | struct io_manager_vec : protected simplevec<watcher *> { |
|
|
59 | friend class io_manager; |
|
|
60 | protected: |
|
|
61 | void erase_unordered (unsigned int pos) |
|
|
62 | { |
|
|
63 | watcher *w = (*this)[size () - 1]; |
|
|
64 | pop_back (); |
|
|
65 | |
|
|
66 | if (size ()) |
|
|
67 | if ((*this)[pos] = w) |
|
|
68 | w->active = pos + 1; |
|
|
69 | } |
|
|
70 | }; |
|
|
71 | |
49 | class io_manager { |
72 | class io_manager { |
50 | #if IOM_IO |
73 | #if IOM_IO |
51 | simplevec<io_watcher *> iow; |
74 | io_manager_vec<io_watcher> iow; |
52 | #endif |
75 | #endif |
53 | #if IOM_CHECK |
76 | #if IOM_CHECK |
54 | simplevec<check_watcher *> cw; |
77 | io_manager_vec<check_watcher> cw; |
55 | #endif |
78 | #endif |
56 | #if IOM_TIME |
79 | #if IOM_TIME |
57 | simplevec<time_watcher *> tw; |
80 | io_manager_vec<time_watcher> tw; |
58 | #endif |
81 | #endif |
59 | #if IOM_IDLE |
82 | #if IOM_IDLE |
60 | simplevec<idle_watcher *> iw; |
83 | io_manager_vec<idle_watcher> iw; |
61 | #endif |
84 | #endif |
62 | |
85 | |
63 | template<class watcher> |
86 | template<class watcher> |
64 | void reg (watcher *w, simplevec<watcher *> &queue); |
87 | void reg (watcher *w, simplevec<watcher *> &queue); |
65 | |
88 | |
… | |
… | |
87 | ~io_manager (); |
110 | ~io_manager (); |
88 | }; |
111 | }; |
89 | |
112 | |
90 | extern io_manager iom; // a singleton, together with it's construction/destruction problems. |
113 | extern io_manager iom; // a singleton, together with it's construction/destruction problems. |
91 | |
114 | |
|
|
115 | struct watcher { |
|
|
116 | int active; /* 0 == inactive, else index into respective vector */ |
|
|
117 | |
|
|
118 | watcher() : active(0) { } |
|
|
119 | }; |
|
|
120 | |
92 | #if IOM_IO |
121 | #if IOM_IO |
93 | enum { EVENT_READ = 1, EVENT_WRITE = 2 }; |
122 | enum { EVENT_READ = 1, EVENT_WRITE = 2 }; |
94 | |
123 | |
95 | struct io_watcher : callback2<void, io_watcher &, short> { |
124 | struct io_watcher : watcher, callback2<void, io_watcher &, short> { |
96 | int fd; |
125 | int fd; |
97 | short events; |
126 | short events; |
98 | |
|
|
99 | template<class O1, class O2> |
|
|
100 | io_watcher (O1 *object, void (O2::*method)(io_watcher &, short)) |
|
|
101 | : callback2<void, io_watcher &, short>(object,method) |
|
|
102 | { } |
|
|
103 | |
|
|
104 | ~io_watcher (); |
|
|
105 | |
127 | |
106 | void set (int fd_, short events_) { fd = fd_; events = events_; } |
128 | void set (int fd_, short events_) { fd = fd_; events = events_; } |
107 | |
129 | |
108 | void set (short events_) { set (fd, events_); } |
130 | void set (short events_) { set (fd, events_); } |
109 | void start () { iom.reg (this); } |
131 | void start () { iom.reg (this); } |
110 | void start (int fd_, short events_) { set (fd_, events_); iom.reg (this); } |
132 | void start (int fd_, short events_) { set (fd_, events_); iom.reg (this); } |
111 | void stop () { iom.unreg (this); } |
133 | void stop () { iom.unreg (this); } |
|
|
134 | |
|
|
135 | template<class O1, class O2> |
|
|
136 | io_watcher (O1 *object, void (O2::*method)(io_watcher &, short)) |
|
|
137 | : callback2<void, io_watcher &, short>(object,method) |
|
|
138 | { } |
|
|
139 | ~io_watcher () { stop (); } |
112 | }; |
140 | }; |
113 | #endif |
141 | #endif |
114 | |
142 | |
115 | #if IOM_TIME |
143 | #if IOM_TIME |
116 | enum { TSTAMP_CANCEL = -1 }; |
|
|
117 | |
|
|
118 | struct time_watcher : callback1<void, time_watcher &> { |
144 | struct time_watcher : watcher, callback1<void, time_watcher &> { |
119 | tstamp at; |
145 | tstamp at; |
120 | |
|
|
121 | template<class O1, class O2> |
|
|
122 | time_watcher (O1 *object, void (O2::*method)(time_watcher &)) |
|
|
123 | : callback1<void, time_watcher &>(object,method) |
|
|
124 | { } |
|
|
125 | |
|
|
126 | ~time_watcher (); |
|
|
127 | |
146 | |
128 | void trigger (); |
147 | void trigger (); |
129 | |
148 | |
130 | void set (tstamp when) { at = when; } |
149 | void set (tstamp when) { at = when; } |
131 | void operator ()() { trigger (); } |
150 | void operator ()() { trigger (); } |
132 | void start () { iom.reg (this); } |
151 | void start () { iom.reg (this); } |
133 | void start (tstamp when) { set (when); iom.reg (this); } |
152 | void start (tstamp when) { set (when); iom.reg (this); } |
134 | void stop () { iom.unreg (this); } |
153 | void stop () { iom.unreg (this); } |
135 | |
154 | |
136 | void reset (tstamp when = TSTAMP_CANCEL) |
155 | template<class O1, class O2> |
137 | { |
156 | time_watcher (O1 *object, void (O2::*method)(time_watcher &)) |
138 | stop (); |
157 | : callback1<void, time_watcher &>(object,method) |
139 | at = when; |
|
|
140 | } |
158 | { } |
|
|
159 | ~time_watcher () { stop (); } |
141 | }; |
160 | }; |
142 | #endif |
161 | #endif |
143 | |
162 | |
144 | #if IOM_CHECK |
163 | #if IOM_CHECK |
145 | // run before checking for new events |
164 | // run before checking for new events |
146 | struct check_watcher : callback1<void, check_watcher &> { |
165 | struct check_watcher : watcher, callback1<void, check_watcher &> { |
|
|
166 | void start () { iom.reg (this); } |
|
|
167 | void stop () { iom.unreg (this); } |
|
|
168 | |
147 | template<class O1, class O2> |
169 | template<class O1, class O2> |
148 | check_watcher (O1 *object, void (O2::*method)(check_watcher &)) |
170 | check_watcher (O1 *object, void (O2::*method)(check_watcher &)) |
149 | : callback1<void, check_watcher &>(object,method) |
171 | : callback1<void, check_watcher &>(object,method) |
150 | { } |
172 | { } |
151 | |
|
|
152 | ~check_watcher (); |
173 | ~check_watcher () { stop (); } |
153 | |
|
|
154 | void start () { iom.reg (this); } |
|
|
155 | void stop () { iom.unreg (this); } |
|
|
156 | }; |
174 | }; |
157 | #endif |
175 | #endif |
158 | |
176 | |
159 | #if IOM_IDLE |
177 | #if IOM_IDLE |
160 | // run after checking for any i/o, but before waiting |
178 | // run after checking for any i/o, but before waiting |
161 | struct idle_watcher : callback1<void, idle_watcher &> { |
179 | struct idle_watcher : watcher, callback1<void, idle_watcher &> { |
|
|
180 | void start () { iom.reg (this); } |
|
|
181 | void stop () { iom.unreg (this); } |
|
|
182 | |
162 | template<class O1, class O2> |
183 | template<class O1, class O2> |
163 | idle_watcher (O1 *object, void (O2::*method)(idle_watcher &)) |
184 | idle_watcher (O1 *object, void (O2::*method)(idle_watcher &)) |
164 | : callback1<void, idle_watcher &>(object,method) |
185 | : callback1<void, idle_watcher &>(object,method) |
165 | { } |
186 | { } |
166 | |
|
|
167 | ~idle_watcher (); |
187 | ~idle_watcher () { stop (); } |
168 | |
|
|
169 | void start () { iom.reg (this); } |
|
|
170 | void stop () { iom.unreg (this); } |
|
|
171 | }; |
188 | }; |
172 | #endif |
189 | #endif |
173 | |
190 | |
174 | #endif |
191 | #endif |
175 | |
192 | |