… | |
… | |
38 | # define IOM_CHECK 0 |
38 | # define IOM_CHECK 0 |
39 | #endif |
39 | #endif |
40 | #ifndef IOM_IDLE |
40 | #ifndef IOM_IDLE |
41 | # define IOM_IDLE 0 |
41 | # define IOM_IDLE 0 |
42 | #endif |
42 | #endif |
|
|
43 | #ifndef IOM_SIG |
|
|
44 | # define IOM_SIG 0 |
|
|
45 | #endif |
43 | |
46 | |
44 | typedef double tstamp; |
47 | typedef double tstamp; |
45 | extern tstamp NOW; |
48 | extern tstamp NOW; |
46 | |
49 | |
47 | struct watcher; |
50 | struct watcher; |
… | |
… | |
55 | struct check_watcher; |
58 | struct check_watcher; |
56 | #endif |
59 | #endif |
57 | #if IOM_IDLE |
60 | #if IOM_IDLE |
58 | struct idle_watcher; |
61 | struct idle_watcher; |
59 | #endif |
62 | #endif |
|
|
63 | #if IOM_SIG |
|
|
64 | struct sig_watcher; |
|
|
65 | #endif |
60 | |
66 | |
61 | template<class watcher> |
67 | template<class watcher> |
62 | struct io_manager_vec : protected vector<watcher *> { |
68 | struct io_manager_vec : vector<watcher *> { |
63 | friend class io_manager; |
69 | #if IOM_CHECK |
64 | protected: |
70 | bool activity; |
|
|
71 | #endif |
|
|
72 | |
65 | void erase_unordered (unsigned int pos) |
73 | void erase_unordered (unsigned int pos) |
66 | { |
74 | { |
67 | watcher *w = (*this)[size () - 1]; |
75 | watcher *w = (*this)[this->size () - 1]; |
68 | pop_back (); |
76 | this->pop_back (); |
69 | |
77 | |
70 | if (size ()) |
78 | if (this->size ()) |
71 | if ((*this)[pos] = w) |
79 | if ((*this)[pos] = w) |
72 | w->active = pos + 1; |
80 | w->active = pos + 1; |
73 | } |
81 | } |
74 | }; |
82 | }; |
75 | |
83 | |
… | |
… | |
84 | io_manager_vec<time_watcher> tw; |
92 | io_manager_vec<time_watcher> tw; |
85 | #endif |
93 | #endif |
86 | #if IOM_IDLE |
94 | #if IOM_IDLE |
87 | io_manager_vec<idle_watcher> iw; |
95 | io_manager_vec<idle_watcher> iw; |
88 | #endif |
96 | #endif |
|
|
97 | #if IOM_SIG |
|
|
98 | typedef io_manager_vec<sig_watcher> sig_vec; |
|
|
99 | vector<sig_vec *> sw; |
|
|
100 | static void sighandler (int signum); |
|
|
101 | #endif |
89 | |
102 | |
90 | template<class watcher> |
103 | template<class watcher> |
91 | void reg (watcher *w, io_manager_vec<watcher> &queue); |
104 | void reg (watcher *w, io_manager_vec<watcher> &queue); |
92 | |
105 | |
93 | template<class watcher> |
106 | template<class watcher> |
… | |
… | |
105 | void reg (check_watcher *w); void unreg (check_watcher *w); |
118 | void reg (check_watcher *w); void unreg (check_watcher *w); |
106 | #endif |
119 | #endif |
107 | #if IOM_IDLE |
120 | #if IOM_IDLE |
108 | void reg (idle_watcher *w); void unreg (idle_watcher *w); |
121 | void reg (idle_watcher *w); void unreg (idle_watcher *w); |
109 | #endif |
122 | #endif |
|
|
123 | #if IOM_SIG |
|
|
124 | void reg (sig_watcher *w); void unreg (sig_watcher *w); |
|
|
125 | #endif |
110 | |
126 | |
111 | void loop (); |
127 | void loop (); |
112 | |
128 | |
113 | io_manager (); |
129 | io_manager (); |
114 | ~io_manager (); |
130 | ~io_manager (); |
… | |
… | |
117 | extern io_manager iom; // a singleton, together with it's construction/destruction problems. |
133 | extern io_manager iom; // a singleton, together with it's construction/destruction problems. |
118 | |
134 | |
119 | struct watcher { |
135 | struct watcher { |
120 | int active; /* 0 == inactive, else index into respective vector */ |
136 | int active; /* 0 == inactive, else index into respective vector */ |
121 | |
137 | |
122 | watcher() : active(0) { } |
138 | watcher () : active (0) { } |
123 | }; |
139 | }; |
124 | |
140 | |
125 | #if IOM_IO |
141 | #if IOM_IO |
126 | enum { EVENT_READ = 1, EVENT_WRITE = 2 }; |
142 | enum { EVENT_READ = 1, EVENT_WRITE = 2 }; |
127 | |
143 | |
… | |
… | |
135 | void start () { iom.reg (this); } |
151 | void start () { iom.reg (this); } |
136 | void start (int fd_, short events_) { set (fd_, events_); iom.reg (this); } |
152 | void start (int fd_, short events_) { set (fd_, events_); iom.reg (this); } |
137 | void stop () { iom.unreg (this); } |
153 | void stop () { iom.unreg (this); } |
138 | |
154 | |
139 | template<class O1, class O2> |
155 | template<class O1, class O2> |
140 | io_watcher (O1 *object, void (O2::*method)(io_watcher &, short)) |
156 | io_watcher (O1 *object, void (O2::*method) (io_watcher &, short)) |
141 | : callback2<void, io_watcher &, short>(object,method) |
157 | : callback2<void, io_watcher &, short> (object,method) |
142 | { } |
158 | { } |
143 | ~io_watcher () { stop (); } |
159 | ~io_watcher () { stop (); } |
144 | }; |
160 | }; |
145 | #endif |
161 | #endif |
146 | |
162 | |
… | |
… | |
149 | tstamp at; |
165 | tstamp at; |
150 | |
166 | |
151 | void trigger (); |
167 | void trigger (); |
152 | |
168 | |
153 | void set (tstamp when) { at = when; } |
169 | void set (tstamp when) { at = when; } |
154 | void operator ()() { trigger (); } |
170 | void operator () () { trigger (); } |
155 | void start () { iom.reg (this); } |
171 | void start () { iom.reg (this); } |
156 | void start (tstamp when) { set (when); iom.reg (this); } |
172 | void start (tstamp when) { set (when); iom.reg (this); } |
157 | void stop () { iom.unreg (this); } |
173 | void stop () { iom.unreg (this); } |
158 | |
174 | |
159 | template<class O1, class O2> |
175 | template<class O1, class O2> |
160 | time_watcher (O1 *object, void (O2::*method)(time_watcher &)) |
176 | time_watcher (O1 *object, void (O2::*method) (time_watcher &)) |
161 | : callback1<void, time_watcher &>(object,method) |
177 | : callback1<void, time_watcher &> (object,method), at (0) |
162 | { } |
178 | { } |
163 | ~time_watcher () { stop (); } |
179 | ~time_watcher () { stop (); } |
164 | }; |
180 | }; |
165 | #endif |
181 | #endif |
166 | |
182 | |
… | |
… | |
169 | struct check_watcher : watcher, callback1<void, check_watcher &> { |
185 | struct check_watcher : watcher, callback1<void, check_watcher &> { |
170 | void start () { iom.reg (this); } |
186 | void start () { iom.reg (this); } |
171 | void stop () { iom.unreg (this); } |
187 | void stop () { iom.unreg (this); } |
172 | |
188 | |
173 | template<class O1, class O2> |
189 | template<class O1, class O2> |
174 | check_watcher (O1 *object, void (O2::*method)(check_watcher &)) |
190 | check_watcher (O1 *object, void (O2::*method) (check_watcher &)) |
175 | : callback1<void, check_watcher &>(object,method) |
191 | : callback1<void, check_watcher &> (object,method) |
176 | { } |
192 | { } |
177 | ~check_watcher () { stop (); } |
193 | ~check_watcher () { stop (); } |
178 | }; |
194 | }; |
179 | #endif |
195 | #endif |
180 | |
196 | |
… | |
… | |
183 | struct idle_watcher : watcher, callback1<void, idle_watcher &> { |
199 | struct idle_watcher : watcher, callback1<void, idle_watcher &> { |
184 | void start () { iom.reg (this); } |
200 | void start () { iom.reg (this); } |
185 | void stop () { iom.unreg (this); } |
201 | void stop () { iom.unreg (this); } |
186 | |
202 | |
187 | template<class O1, class O2> |
203 | template<class O1, class O2> |
188 | idle_watcher (O1 *object, void (O2::*method)(idle_watcher &)) |
204 | idle_watcher (O1 *object, void (O2::*method) (idle_watcher &)) |
189 | : callback1<void, idle_watcher &>(object,method) |
205 | : callback1<void, idle_watcher &> (object,method) |
190 | { } |
206 | { } |
191 | ~idle_watcher () { stop (); } |
207 | ~idle_watcher () { stop (); } |
192 | }; |
208 | }; |
193 | #endif |
209 | #endif |
194 | |
210 | |
195 | #endif |
211 | #if IOM_SIG |
|
|
212 | struct sig_watcher : watcher, callback1<void, sig_watcher &> { |
|
|
213 | int signum; |
196 | |
214 | |
|
|
215 | void start (int signum); |
|
|
216 | void stop () { iom.unreg (this); } |
|
|
217 | |
|
|
218 | template<class O1, class O2> |
|
|
219 | sig_watcher (O1 *object, void (O2::*method) (sig_watcher &)) |
|
|
220 | : callback1<void, sig_watcher &> (object,method), signum (-1) |
|
|
221 | { } |
|
|
222 | ~sig_watcher () { stop (); } |
|
|
223 | }; |
|
|
224 | #endif |
|
|
225 | |
|
|
226 | #endif |
|
|
227 | |