1 | /* |
1 | /* |
2 | iom.h -- generic I/O multiplexer |
2 | iom.h -- generic I/O multiplexer |
3 | Copyright (C) 2003, 2004 Marc Lehmann <gvpe@schmorp.de> |
3 | Copyright (C) 2003-2006 Marc Lehmann <gvpe@schmorp.de> |
4 | |
4 | |
5 | This file is part of GVPE. |
5 | This file is part of GVPE. |
6 | |
6 | |
7 | GVPE is free software; you can redistribute it and/or modify |
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 |
8 | it under the terms of the GNU General Public License as published by |
… | |
… | |
26 | // - a vector template like simplevec or stl's vector |
26 | // - a vector template like simplevec or stl's vector |
27 | // - defines for all watcher types required in your app |
27 | // - defines for all watcher types required in your app |
28 | // edit iom_conf.h as appropriate. |
28 | // edit iom_conf.h as appropriate. |
29 | #include "iom_conf.h" |
29 | #include "iom_conf.h" |
30 | |
30 | |
|
|
31 | #if IOM_CHILD |
|
|
32 | # undef IOM_SIG |
|
|
33 | # define IOM_SIG 1 |
|
|
34 | #endif |
|
|
35 | |
31 | #include "callback.h" |
36 | #include "callback.h" |
32 | |
|
|
33 | #ifndef IOM_IO |
|
|
34 | # define IOM_IO 0 |
|
|
35 | #endif |
|
|
36 | #ifndef IOM_TIME |
|
|
37 | # define IOM_TIME 0 |
|
|
38 | #endif |
|
|
39 | #ifndef IOM_CHECK |
|
|
40 | # define IOM_CHECK 0 |
|
|
41 | #endif |
|
|
42 | #ifndef IOM_IDLE |
|
|
43 | # define IOM_IDLE 0 |
|
|
44 | #endif |
|
|
45 | #ifndef IOM_SIG |
|
|
46 | # define IOM_SIG 0 |
|
|
47 | #endif |
|
|
48 | |
37 | |
49 | typedef double tstamp; |
38 | typedef double tstamp; |
50 | extern tstamp NOW; |
39 | extern tstamp NOW; |
51 | |
40 | |
52 | // TSTAMP_MAX must still fit into a positive struct timeval |
41 | // TSTAMP_MAX must still fit into a positive struct timeval |
… | |
… | |
65 | #if IOM_IDLE |
54 | #if IOM_IDLE |
66 | struct idle_watcher; |
55 | struct idle_watcher; |
67 | #endif |
56 | #endif |
68 | #if IOM_SIG |
57 | #if IOM_SIG |
69 | struct sig_watcher; |
58 | struct sig_watcher; |
|
|
59 | #endif |
|
|
60 | #if IOM_CHILD |
|
|
61 | struct child_watcher; |
70 | #endif |
62 | #endif |
71 | |
63 | |
72 | template<class watcher> |
64 | template<class watcher> |
73 | struct io_manager_vec : vector<watcher *> { |
65 | struct io_manager_vec : vector<watcher *> { |
74 | void erase_unordered (unsigned int pos) |
66 | void erase_unordered (unsigned int pos) |
… | |
… | |
113 | static void reg (idle_watcher &w); static void unreg (idle_watcher &w); |
105 | static void reg (idle_watcher &w); static void unreg (idle_watcher &w); |
114 | #endif |
106 | #endif |
115 | #if IOM_SIG |
107 | #if IOM_SIG |
116 | static void reg (sig_watcher &w); static void unreg (sig_watcher &w); |
108 | static void reg (sig_watcher &w); static void unreg (sig_watcher &w); |
117 | #endif |
109 | #endif |
|
|
110 | #if IOM_CHILD |
|
|
111 | static void reg (child_watcher &w); static void unreg (child_watcher &w); |
|
|
112 | #endif |
118 | |
113 | |
119 | static void loop (); |
114 | static void loop (); |
120 | }; |
115 | }; |
121 | |
116 | |
122 | struct watcher { |
117 | struct watcher { |
123 | int active; /* 0 == inactive, else index into respective vector */ |
118 | int active; /* 0 == inactive, else index into respective vector */ |
124 | |
119 | |
|
|
120 | bool is_active () { return active; } |
|
|
121 | |
125 | watcher () : active (0) { } |
122 | watcher () : active (0) { } |
126 | }; |
123 | }; |
127 | |
124 | |
128 | #if IOM_IO |
125 | #if IOM_IO |
129 | enum { EVENT_UNDEF = -1, EVENT_NONE = 0, EVENT_READ = 1, EVENT_WRITE = 2 }; |
126 | enum { EVENT_UNDEF = -1, EVENT_NONE = 0, EVENT_READ = 1, EVENT_WRITE = 2 }; |
130 | |
127 | |
131 | struct io_watcher : watcher, callback2<void, io_watcher &, short> { |
128 | struct io_watcher : watcher, callback<void (io_watcher &, short)> { |
132 | int fd; |
129 | int fd; |
133 | short events; |
130 | short events; |
134 | |
131 | |
135 | void set (int fd_, short events_) { fd = fd_; events = events_; } |
132 | void set (int fd_, short events_) { fd = fd_; events = events_; } |
136 | |
133 | |
… | |
… | |
139 | void start (int fd_, short events_) { set (fd_, events_); io_manager::reg (*this); } |
136 | void start (int fd_, short events_) { set (fd_, events_); io_manager::reg (*this); } |
140 | void stop () { io_manager::unreg (*this); } |
137 | void stop () { io_manager::unreg (*this); } |
141 | |
138 | |
142 | template<class O1, class O2> |
139 | template<class O1, class O2> |
143 | io_watcher (O1 *object, void (O2::*method) (io_watcher &, short)) |
140 | io_watcher (O1 *object, void (O2::*method) (io_watcher &, short)) |
144 | : callback2<void, io_watcher &, short> (object,method) |
141 | : callback<void (io_watcher &, short)> (object, method) |
145 | { } |
142 | { } |
146 | ~io_watcher () { stop (); } |
143 | ~io_watcher () { stop (); } |
147 | }; |
144 | }; |
148 | #endif |
145 | #endif |
149 | |
146 | |
150 | #if IOM_TIME |
147 | #if IOM_TIME |
151 | struct time_watcher : watcher, callback1<void, time_watcher &> { |
148 | struct time_watcher : watcher, callback<void (time_watcher &)> { |
152 | tstamp at; |
149 | tstamp at; |
153 | |
150 | |
154 | void trigger (); |
151 | void trigger (); |
155 | |
152 | |
156 | void set (tstamp when) { at = when; } |
153 | void set (tstamp when) { at = when; } |
… | |
… | |
159 | void start (tstamp when) { set (when); io_manager::reg (*this); } |
156 | void start (tstamp when) { set (when); io_manager::reg (*this); } |
160 | void stop () { io_manager::unreg (*this); } |
157 | void stop () { io_manager::unreg (*this); } |
161 | |
158 | |
162 | template<class O1, class O2> |
159 | template<class O1, class O2> |
163 | time_watcher (O1 *object, void (O2::*method) (time_watcher &)) |
160 | time_watcher (O1 *object, void (O2::*method) (time_watcher &)) |
164 | : callback1<void, time_watcher &> (object,method), at (0) |
161 | : callback<void (time_watcher &)> (object, method), at (0) |
165 | { } |
162 | { } |
166 | ~time_watcher () { stop (); } |
163 | ~time_watcher () { stop (); } |
167 | }; |
164 | }; |
168 | #endif |
165 | #endif |
169 | |
166 | |
170 | #if IOM_CHECK |
167 | #if IOM_CHECK |
171 | // run before checking for new events |
168 | // run before checking for new events |
172 | struct check_watcher : watcher, callback1<void, check_watcher &> { |
169 | struct check_watcher : watcher, callback<void (check_watcher &)> { |
173 | void start () { io_manager::reg (*this); } |
170 | void start () { io_manager::reg (*this); } |
174 | void stop () { io_manager::unreg (*this); } |
171 | void stop () { io_manager::unreg (*this); } |
175 | |
172 | |
176 | template<class O1, class O2> |
173 | template<class O1, class O2> |
177 | check_watcher (O1 *object, void (O2::*method) (check_watcher &)) |
174 | check_watcher (O1 *object, void (O2::*method) (check_watcher &)) |
178 | : callback1<void, check_watcher &> (object,method) |
175 | : callback<void (check_watcher &)> (object, method) |
179 | { } |
176 | { } |
180 | ~check_watcher () { stop (); } |
177 | ~check_watcher () { stop (); } |
181 | }; |
178 | }; |
182 | #endif |
179 | #endif |
183 | |
180 | |
184 | #if IOM_IDLE |
181 | #if IOM_IDLE |
185 | // run after checking for any i/o, but before waiting |
182 | // run after checking for any i/o, but before waiting |
186 | struct idle_watcher : watcher, callback1<void, idle_watcher &> { |
183 | struct idle_watcher : watcher, callback<void (idle_watcher &)> { |
187 | void start () { io_manager::reg (*this); } |
184 | void start () { io_manager::reg (*this); } |
188 | void stop () { io_manager::unreg (*this); } |
185 | void stop () { io_manager::unreg (*this); } |
189 | |
186 | |
190 | template<class O1, class O2> |
187 | template<class O1, class O2> |
191 | idle_watcher (O1 *object, void (O2::*method) (idle_watcher &)) |
188 | idle_watcher (O1 *object, void (O2::*method) (idle_watcher &)) |
192 | : callback1<void, idle_watcher &> (object,method) |
189 | : callback<void (idle_watcher &)> (object, method) |
193 | { } |
190 | { } |
194 | ~idle_watcher () { stop (); } |
191 | ~idle_watcher () { stop (); } |
195 | }; |
192 | }; |
196 | #endif |
193 | #endif |
197 | |
194 | |
198 | #if IOM_SIG |
195 | #if IOM_SIG |
199 | struct sig_watcher : watcher, callback1<void, sig_watcher &> { |
196 | struct sig_watcher : watcher, callback<void (sig_watcher &)> { |
200 | int signum; |
197 | int signum; |
201 | |
198 | |
202 | void start (int signum); |
199 | void start (int signum); |
203 | void stop () { io_manager::unreg (*this); } |
200 | void stop () { io_manager::unreg (*this); } |
204 | |
201 | |
205 | template<class O1, class O2> |
202 | template<class O1, class O2> |
206 | sig_watcher (O1 *object, void (O2::*method) (sig_watcher &)) |
203 | sig_watcher (O1 *object, void (O2::*method) (sig_watcher &)) |
207 | : callback1<void, sig_watcher &> (object,method), signum (-1) |
204 | : callback<void(sig_watcher &)> (object, method), signum (0) |
208 | { } |
205 | { } |
209 | ~sig_watcher () { stop (); } |
206 | ~sig_watcher () { stop (); } |
210 | }; |
207 | }; |
211 | #endif |
208 | #endif |
212 | |
209 | |
213 | #endif |
210 | #if IOM_CHILD |
|
|
211 | struct child_watcher : watcher, callback<void (child_watcher &, int)> { |
|
|
212 | int /*pid_t*/ pid; |
214 | |
213 | |
|
|
214 | void start (int pid) { this->pid = pid; io_manager::reg (*this); } |
|
|
215 | void stop () { io_manager::unreg (*this); } |
|
|
216 | |
|
|
217 | template<class O1, class O2> |
|
|
218 | child_watcher (O1 *object, void (O2::*method) (child_watcher &, int status)) |
|
|
219 | : callback<void (child_watcher &, int)> (object, method), pid (0) |
|
|
220 | { } |
|
|
221 | ~child_watcher () { stop (); } |
|
|
222 | }; |
|
|
223 | #endif |
|
|
224 | |
|
|
225 | #endif |
|
|
226 | |