1 |
pcg |
1.1 |
/* |
2 |
root |
1.20 |
iom.h -- generic I/O multiplexer |
3 |
root |
1.26 |
Copyright (C) 2003-2006 Marc Lehmann <gvpe@schmorp.de> |
4 |
pcg |
1.1 |
|
5 |
root |
1.23 |
This file is part of GVPE. |
6 |
|
|
|
7 |
|
|
GVPE is free software; you can redistribute it and/or modify |
8 |
pcg |
1.1 |
it under the terms of the GNU General Public License as published by |
9 |
|
|
the Free Software Foundation; either version 2 of the License, or |
10 |
|
|
(at your option) any later version. |
11 |
|
|
|
12 |
|
|
This program is distributed in the hope that it will be useful, |
13 |
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 |
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15 |
|
|
GNU General Public License for more details. |
16 |
|
|
|
17 |
|
|
You should have received a copy of the GNU General Public License |
18 |
root |
1.23 |
along with gvpe; if not, write to the Free Software |
19 |
root |
1.22 |
Foundation, Inc. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
20 |
pcg |
1.1 |
*/ |
21 |
|
|
|
22 |
pcg |
1.7 |
#ifndef IOM_H__ |
23 |
|
|
#define IOM_H__ |
24 |
pcg |
1.1 |
|
25 |
pcg |
1.7 |
// required: |
26 |
|
|
// - a vector template like simplevec or stl's vector |
27 |
|
|
// - defines for all watcher types required in your app |
28 |
|
|
// edit iom_conf.h as appropriate. |
29 |
|
|
#include "iom_conf.h" |
30 |
pcg |
1.1 |
|
31 |
root |
1.25 |
#if IOM_CHILD |
32 |
|
|
# undef IOM_SIG |
33 |
|
|
# define IOM_SIG 1 |
34 |
|
|
#endif |
35 |
|
|
|
36 |
pcg |
1.6 |
#include "callback.h" |
37 |
pcg |
1.1 |
|
38 |
pcg |
1.6 |
typedef double tstamp; |
39 |
|
|
extern tstamp NOW; |
40 |
pcg |
1.2 |
|
41 |
root |
1.23 |
// TSTAMP_MAX must still fit into a positive struct timeval |
42 |
|
|
#define TSTAMP_MAX (double)(1UL<<31) |
43 |
|
|
|
44 |
root |
1.30 |
//#define IOM_LIBEVENT "event.h" *NOT* a supported feature |
45 |
|
|
#ifdef IOM_LIBEVENT |
46 |
|
|
# include <sys/time.h> |
47 |
|
|
# include IOM_LIBEVENT |
48 |
|
|
# undef IOM_IO |
49 |
|
|
# define IOM_IO 1 |
50 |
root |
1.31 |
# undef IOM_TIME |
51 |
|
|
# define IOM_TIME 1 |
52 |
|
|
# undef IOM_IDLE // NYI |
53 |
|
|
# undef IOM_SIG // NYI |
54 |
|
|
# undef IOM_CHILD // NYI |
55 |
root |
1.30 |
#endif |
56 |
|
|
|
57 |
pcg |
1.7 |
struct watcher; |
58 |
pcg |
1.2 |
#if IOM_IO |
59 |
pcg |
1.7 |
struct io_watcher; |
60 |
pcg |
1.2 |
#endif |
61 |
|
|
#if IOM_TIME |
62 |
pcg |
1.7 |
struct time_watcher; |
63 |
pcg |
1.2 |
#endif |
64 |
|
|
#if IOM_CHECK |
65 |
pcg |
1.7 |
struct check_watcher; |
66 |
pcg |
1.2 |
#endif |
67 |
pcg |
1.5 |
#if IOM_IDLE |
68 |
pcg |
1.7 |
struct idle_watcher; |
69 |
pcg |
1.5 |
#endif |
70 |
pcg |
1.12 |
#if IOM_SIG |
71 |
|
|
struct sig_watcher; |
72 |
|
|
#endif |
73 |
root |
1.25 |
#if IOM_CHILD |
74 |
|
|
struct child_watcher; |
75 |
|
|
#endif |
76 |
pcg |
1.1 |
|
77 |
pcg |
1.6 |
template<class watcher> |
78 |
pcg |
1.10 |
struct io_manager_vec : vector<watcher *> { |
79 |
pcg |
1.6 |
void erase_unordered (unsigned int pos) |
80 |
|
|
{ |
81 |
root |
1.13 |
watcher *w = (*this)[this->size () - 1]; |
82 |
|
|
this->pop_back (); |
83 |
pcg |
1.6 |
|
84 |
root |
1.20 |
if (!this->empty ()) |
85 |
|
|
if (((*this)[pos] = w)) // '=' is correct! |
86 |
pcg |
1.6 |
w->active = pos + 1; |
87 |
|
|
} |
88 |
|
|
}; |
89 |
|
|
|
90 |
root |
1.15 |
// only used as a namespace, and for initialisation purposes |
91 |
pcg |
1.2 |
class io_manager { |
92 |
|
|
template<class watcher> |
93 |
root |
1.15 |
static void reg (watcher &w, io_manager_vec<watcher> &queue); |
94 |
pcg |
1.1 |
|
95 |
pcg |
1.2 |
template<class watcher> |
96 |
root |
1.15 |
static void unreg (watcher &w, io_manager_vec<watcher> &queue); |
97 |
pcg |
1.1 |
|
98 |
|
|
public: |
99 |
root |
1.21 |
#if IOM_TIME |
100 |
|
|
// fetch time only |
101 |
|
|
static tstamp now (); |
102 |
|
|
|
103 |
|
|
// set NOW |
104 |
|
|
static void set_now (); |
105 |
|
|
#endif |
106 |
|
|
|
107 |
pcg |
1.1 |
// register a watcher |
108 |
root |
1.30 |
#ifndef IOM_LIBEVENT |
109 |
pcg |
1.2 |
#if IOM_IO |
110 |
root |
1.15 |
static void reg (io_watcher &w); static void unreg (io_watcher &w); |
111 |
pcg |
1.2 |
#endif |
112 |
|
|
#if IOM_TIME |
113 |
root |
1.15 |
static void reg (time_watcher &w); static void unreg (time_watcher &w); |
114 |
pcg |
1.2 |
#endif |
115 |
root |
1.30 |
#if IOM_SIG |
116 |
|
|
static void reg (sig_watcher &w); static void unreg (sig_watcher &w); |
117 |
|
|
#endif |
118 |
pcg |
1.2 |
#if IOM_CHECK |
119 |
root |
1.15 |
static void reg (check_watcher &w); static void unreg (check_watcher &w); |
120 |
pcg |
1.2 |
#endif |
121 |
root |
1.30 |
#endif |
122 |
pcg |
1.5 |
#if IOM_IDLE |
123 |
root |
1.15 |
static void reg (idle_watcher &w); static void unreg (idle_watcher &w); |
124 |
pcg |
1.5 |
#endif |
125 |
root |
1.25 |
#if IOM_CHILD |
126 |
|
|
static void reg (child_watcher &w); static void unreg (child_watcher &w); |
127 |
|
|
#endif |
128 |
pcg |
1.1 |
|
129 |
root |
1.15 |
static void loop (); |
130 |
pcg |
1.1 |
}; |
131 |
|
|
|
132 |
pcg |
1.6 |
struct watcher { |
133 |
|
|
int active; /* 0 == inactive, else index into respective vector */ |
134 |
|
|
|
135 |
root |
1.27 |
bool is_active () { return active; } |
136 |
|
|
|
137 |
pcg |
1.11 |
watcher () : active (0) { } |
138 |
pcg |
1.6 |
}; |
139 |
|
|
|
140 |
pcg |
1.2 |
#if IOM_IO |
141 |
root |
1.30 |
#ifdef IOM_LIBEVENT |
142 |
|
|
enum { EVENT_UNDEF = -1, EVENT_NONE = 0, EVENT_READ = EV_READ, EVENT_WRITE = EV_WRITE }; |
143 |
|
|
|
144 |
|
|
void iom_io_c_callback (int fd, short events, void *data); |
145 |
|
|
|
146 |
|
|
struct io_watcher : watcher, callback<void (io_watcher &, short)> { |
147 |
|
|
struct event ev; |
148 |
|
|
int fd; |
149 |
root |
1.31 |
short events; |
150 |
root |
1.30 |
|
151 |
root |
1.31 |
void set (int fd_, short events_); |
152 |
root |
1.30 |
void set (short events_) { set (fd, events_); } |
153 |
root |
1.31 |
void start () { if (!active) event_add (&ev, 0); active = 1; } |
154 |
root |
1.30 |
void start (int fd_, short events_) { set (fd_, events_); start (); } |
155 |
|
|
void stop () { if (active) event_del (&ev); active = 0; } |
156 |
|
|
|
157 |
|
|
template<class O, class M> |
158 |
|
|
io_watcher (O object, M method) |
159 |
|
|
: callback<void (io_watcher &, short)> (object, method) |
160 |
|
|
{ } |
161 |
|
|
~io_watcher () { stop (); } |
162 |
|
|
}; |
163 |
|
|
#else |
164 |
root |
1.24 |
enum { EVENT_UNDEF = -1, EVENT_NONE = 0, EVENT_READ = 1, EVENT_WRITE = 2 }; |
165 |
pcg |
1.1 |
|
166 |
root |
1.28 |
struct io_watcher : watcher, callback<void (io_watcher &, short)> { |
167 |
pcg |
1.1 |
int fd; |
168 |
|
|
short events; |
169 |
|
|
|
170 |
pcg |
1.5 |
void set (int fd_, short events_) { fd = fd_; events = events_; } |
171 |
pcg |
1.1 |
|
172 |
pcg |
1.5 |
void set (short events_) { set (fd, events_); } |
173 |
root |
1.15 |
void start () { io_manager::reg (*this); } |
174 |
root |
1.30 |
void start (int fd_, short events_) { set (fd_, events_); start (); } |
175 |
root |
1.15 |
void stop () { io_manager::unreg (*this); } |
176 |
pcg |
1.6 |
|
177 |
root |
1.29 |
template<class O, class M> |
178 |
|
|
io_watcher (O object, M method) |
179 |
root |
1.28 |
: callback<void (io_watcher &, short)> (object, method) |
180 |
pcg |
1.6 |
{ } |
181 |
|
|
~io_watcher () { stop (); } |
182 |
pcg |
1.1 |
}; |
183 |
pcg |
1.2 |
#endif |
184 |
root |
1.30 |
#endif |
185 |
pcg |
1.1 |
|
186 |
pcg |
1.2 |
#if IOM_TIME |
187 |
root |
1.30 |
#ifdef IOM_LIBEVENT |
188 |
|
|
void iom_time_c_callback (int fd, short events, void *data); |
189 |
|
|
|
190 |
|
|
struct time_watcher : watcher, callback<void (time_watcher &)> { |
191 |
|
|
struct event ev; |
192 |
|
|
tstamp at; |
193 |
|
|
|
194 |
|
|
void trigger (); |
195 |
|
|
|
196 |
root |
1.31 |
void set (tstamp when) |
197 |
root |
1.30 |
{ |
198 |
root |
1.31 |
at = when; |
199 |
|
|
if (active) |
200 |
|
|
start (); |
201 |
root |
1.30 |
} |
202 |
root |
1.31 |
void operator () () { trigger (); } |
203 |
|
|
void start (); |
204 |
|
|
void start (tstamp when) { at = when; start (); } |
205 |
root |
1.30 |
void stop () { if (active) evtimer_del (&ev); active = 0; } |
206 |
|
|
|
207 |
|
|
template<class O, class M> |
208 |
|
|
time_watcher (O object, M method) |
209 |
|
|
: callback<void (time_watcher &)> (object, method), at (0) |
210 |
root |
1.31 |
{ } |
211 |
root |
1.30 |
~time_watcher () { stop (); } |
212 |
|
|
}; |
213 |
|
|
#else |
214 |
root |
1.28 |
struct time_watcher : watcher, callback<void (time_watcher &)> { |
215 |
pcg |
1.1 |
tstamp at; |
216 |
|
|
|
217 |
|
|
void trigger (); |
218 |
|
|
|
219 |
|
|
void set (tstamp when) { at = when; } |
220 |
pcg |
1.11 |
void operator () () { trigger (); } |
221 |
root |
1.15 |
void start () { io_manager::reg (*this); } |
222 |
root |
1.30 |
void start (tstamp when) { set (when); start (); } |
223 |
root |
1.15 |
void stop () { io_manager::unreg (*this); } |
224 |
pcg |
1.1 |
|
225 |
root |
1.29 |
template<class O, class M> |
226 |
|
|
time_watcher (O object, M method) |
227 |
root |
1.28 |
: callback<void (time_watcher &)> (object, method), at (0) |
228 |
pcg |
1.6 |
{ } |
229 |
|
|
~time_watcher () { stop (); } |
230 |
pcg |
1.1 |
}; |
231 |
pcg |
1.2 |
#endif |
232 |
root |
1.30 |
#endif |
233 |
pcg |
1.2 |
|
234 |
|
|
#if IOM_CHECK |
235 |
|
|
// run before checking for new events |
236 |
root |
1.28 |
struct check_watcher : watcher, callback<void (check_watcher &)> { |
237 |
root |
1.15 |
void start () { io_manager::reg (*this); } |
238 |
|
|
void stop () { io_manager::unreg (*this); } |
239 |
pcg |
1.6 |
|
240 |
root |
1.29 |
template<class O, class M> |
241 |
|
|
check_watcher (O object, M method) |
242 |
root |
1.28 |
: callback<void (check_watcher &)> (object, method) |
243 |
pcg |
1.6 |
{ } |
244 |
|
|
~check_watcher () { stop (); } |
245 |
pcg |
1.5 |
}; |
246 |
|
|
#endif |
247 |
|
|
|
248 |
|
|
#if IOM_IDLE |
249 |
|
|
// run after checking for any i/o, but before waiting |
250 |
root |
1.28 |
struct idle_watcher : watcher, callback<void (idle_watcher &)> { |
251 |
root |
1.15 |
void start () { io_manager::reg (*this); } |
252 |
|
|
void stop () { io_manager::unreg (*this); } |
253 |
pcg |
1.6 |
|
254 |
root |
1.29 |
template<class O, class M> |
255 |
|
|
idle_watcher (O object, M method) |
256 |
root |
1.28 |
: callback<void (idle_watcher &)> (object, method) |
257 |
pcg |
1.5 |
{ } |
258 |
pcg |
1.6 |
~idle_watcher () { stop (); } |
259 |
pcg |
1.2 |
}; |
260 |
|
|
#endif |
261 |
pcg |
1.1 |
|
262 |
pcg |
1.12 |
#if IOM_SIG |
263 |
root |
1.28 |
struct sig_watcher : watcher, callback<void (sig_watcher &)> { |
264 |
pcg |
1.12 |
int signum; |
265 |
|
|
|
266 |
|
|
void start (int signum); |
267 |
root |
1.15 |
void stop () { io_manager::unreg (*this); } |
268 |
pcg |
1.12 |
|
269 |
root |
1.29 |
template<class O, class M> |
270 |
|
|
sig_watcher (O object, M method) |
271 |
root |
1.28 |
: callback<void(sig_watcher &)> (object, method), signum (0) |
272 |
pcg |
1.12 |
{ } |
273 |
|
|
~sig_watcher () { stop (); } |
274 |
|
|
}; |
275 |
|
|
#endif |
276 |
|
|
|
277 |
root |
1.25 |
#if IOM_CHILD |
278 |
root |
1.28 |
struct child_watcher : watcher, callback<void (child_watcher &, int)> { |
279 |
root |
1.25 |
int /*pid_t*/ pid; |
280 |
|
|
|
281 |
|
|
void start (int pid) { this->pid = pid; io_manager::reg (*this); } |
282 |
|
|
void stop () { io_manager::unreg (*this); } |
283 |
|
|
|
284 |
root |
1.29 |
template<class O, class M> |
285 |
|
|
child_watcher (O object, M method) |
286 |
root |
1.28 |
: callback<void (child_watcher &, int)> (object, method), pid (0) |
287 |
root |
1.25 |
{ } |
288 |
|
|
~child_watcher () { stop (); } |
289 |
|
|
}; |
290 |
|
|
#endif |
291 |
|
|
|
292 |
pcg |
1.1 |
#endif |
293 |
|
|
|