2 iom.h -- generic I/O multiplexer
3 Copyright (C) 2003-2006 Marc Lehmann <gvpe@schmorp.de>
5 This file is part of GVPE.
7 GVPE is free software; you can redistribute it and/or modify
8 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.
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.
17 You should have received a copy of the GNU General Public License
18 along with gvpe; if not, write to the Free Software
19 Foundation, Inc. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
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.
38 typedef double tstamp;
41 // TSTAMP_MAX must still fit into a positive struct timeval
42 #define TSTAMP_MAX (double)(1UL<<31)
44 //#define IOM_LIBEVENT "event.h" *NOT* a supported feature
46 # include <sys/time.h>
47 # include IOM_LIBEVENT
52 # undef IOM_IDLE // NYI
53 # undef IOM_SIG // NYI
54 # undef IOM_CHILD // NYI
77 template<class watcher>
78 struct io_manager_vec : vector<watcher *> {
79 void erase_unordered (unsigned int pos)
81 watcher *w = (*this)[this->size () - 1];
85 if (((*this)[pos] = w)) // '=' is correct!
90 // only used as a namespace, and for initialisation purposes
92 template<class watcher>
93 static void reg (watcher &w, io_manager_vec<watcher> &queue);
95 template<class watcher>
96 static void unreg (watcher &w, io_manager_vec<watcher> &queue);
101 static tstamp now ();
104 static void set_now ();
107 // register a watcher
110 static void reg (io_watcher &w); static void unreg (io_watcher &w);
113 static void reg (time_watcher &w); static void unreg (time_watcher &w);
116 static void reg (sig_watcher &w); static void unreg (sig_watcher &w);
119 static void reg (check_watcher &w); static void unreg (check_watcher &w);
123 static void reg (idle_watcher &w); static void unreg (idle_watcher &w);
126 static void reg (child_watcher &w); static void unreg (child_watcher &w);
133 int active; /* 0 == inactive, else index into respective vector */
135 bool is_active () { return active; }
137 watcher () : active (0) { }
142 enum { EVENT_UNDEF = -1, EVENT_NONE = 0, EVENT_READ = EV_READ, EVENT_WRITE = EV_WRITE };
144 void iom_io_c_callback (int fd, short events, void *data);
146 struct io_watcher : watcher, callback<void (io_watcher &, short)> {
151 void set (int fd_, short events_);
152 void set (short events_) { set (fd, events_); }
153 void start () { if (!active) event_add (&ev, 0); active = 1; }
154 void start (int fd_, short events_) { set (fd_, events_); start (); }
155 void stop () { if (active) event_del (&ev); active = 0; }
157 template<class O, class M>
158 io_watcher (O object, M method)
159 : callback<void (io_watcher &, short)> (object, method)
161 ~io_watcher () { stop (); }
164 enum { EVENT_UNDEF = -1, EVENT_NONE = 0, EVENT_READ = 1, EVENT_WRITE = 2 };
166 struct io_watcher : watcher, callback<void (io_watcher &, short)> {
170 void set (int fd_, short events_) { fd = fd_; events = events_; }
172 void set (short events_) { set (fd, events_); }
173 void start () { io_manager::reg (*this); }
174 void start (int fd_, short events_) { set (fd_, events_); start (); }
175 void stop () { io_manager::unreg (*this); }
177 template<class O, class M>
178 io_watcher (O object, M method)
179 : callback<void (io_watcher &, short)> (object, method)
181 ~io_watcher () { stop (); }
188 void iom_time_c_callback (int fd, short events, void *data);
190 struct time_watcher : watcher, callback<void (time_watcher &)> {
196 void set (tstamp when)
202 void operator () () { trigger (); }
204 void start (tstamp when) { at = when; start (); }
205 void stop () { if (active) evtimer_del (&ev); active = 0; }
207 template<class O, class M>
208 time_watcher (O object, M method)
209 : callback<void (time_watcher &)> (object, method), at (0)
211 ~time_watcher () { stop (); }
214 struct time_watcher : watcher, callback<void (time_watcher &)> {
219 void set (tstamp when) { at = when; }
220 void operator () () { trigger (); }
221 void start () { io_manager::reg (*this); }
222 void start (tstamp when) { set (when); start (); }
223 void stop () { io_manager::unreg (*this); }
225 template<class O, class M>
226 time_watcher (O object, M method)
227 : callback<void (time_watcher &)> (object, method), at (0)
229 ~time_watcher () { stop (); }
235 // run before checking for new events
236 struct check_watcher : watcher, callback<void (check_watcher &)> {
237 void start () { io_manager::reg (*this); }
238 void stop () { io_manager::unreg (*this); }
240 template<class O, class M>
241 check_watcher (O object, M method)
242 : callback<void (check_watcher &)> (object, method)
244 ~check_watcher () { stop (); }
249 // run after checking for any i/o, but before waiting
250 struct idle_watcher : watcher, callback<void (idle_watcher &)> {
251 void start () { io_manager::reg (*this); }
252 void stop () { io_manager::unreg (*this); }
254 template<class O, class M>
255 idle_watcher (O object, M method)
256 : callback<void (idle_watcher &)> (object, method)
258 ~idle_watcher () { stop (); }
263 struct sig_watcher : watcher, callback<void (sig_watcher &)> {
266 void start (int signum);
267 void stop () { io_manager::unreg (*this); }
269 template<class O, class M>
270 sig_watcher (O object, M method)
271 : callback<void(sig_watcher &)> (object, method), signum (0)
273 ~sig_watcher () { stop (); }
278 struct child_watcher : watcher, callback<void (child_watcher &, int)> {
281 void start (int pid) { this->pid = pid; io_manager::reg (*this); }
282 void stop () { io_manager::unreg (*this); }
284 template<class O, class M>
285 child_watcher (O object, M method)
286 : callback<void (child_watcher &, int)> (object, method), pid (0)
288 ~child_watcher () { stop (); }