1 | /* |
1 | /* |
2 | iom.h -- generic I/O multiplexor |
2 | iom.h -- generic I/O multiplexor |
3 | Copyright (C) 2003 Marc Lehmann <pcg@goof.com> |
3 | Copyright (C) 2003, 2004 Marc Lehmann <pcg@goof.com> |
4 | |
4 | |
5 | This program is free software; you can redistribute it and/or modify |
5 | This program is free software; you can redistribute it and/or modify |
6 | it under the terms of the GNU General Public License as published by |
6 | it under the terms of the GNU General Public License as published by |
7 | the Free Software Foundation; either version 2 of the License, or |
7 | the Free Software Foundation; either version 2 of the License, or |
8 | (at your option) any later version. |
8 | (at your option) any later version. |
… | |
… | |
15 | You should have received a copy of the GNU General Public License |
15 | You should have received a copy of the GNU General Public License |
16 | along with this program; if not, write to the Free Software |
16 | along with this program; if not, write to the Free Software |
17 | Foundation, Inc. 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
17 | Foundation, Inc. 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
18 | */ |
18 | */ |
19 | |
19 | |
20 | #ifndef VPE_IOM_H__ |
20 | #ifndef IOM_H__ |
21 | #define VPE_IOM_H__ |
21 | #define IOM_H__ |
22 | |
22 | |
23 | #include <cassert> |
23 | // required: |
|
|
24 | // - a vector template like simplevec or stl's vector |
|
|
25 | // - defines for all watcher types required in your app |
|
|
26 | // edit iom_conf.h as appropriate. |
|
|
27 | #include "iom_conf.h" |
24 | |
28 | |
25 | #include "callback.h" |
29 | #include "callback.h" |
26 | #include "rxvtvec.h" |
|
|
27 | |
30 | |
28 | #ifndef IOM_IO |
31 | #ifndef IOM_IO |
29 | # define IOM_IO 1 |
32 | # define IOM_IO 0 |
30 | #endif |
33 | #endif |
31 | #ifndef IOM_TIME |
34 | #ifndef IOM_TIME |
32 | # define IOM_TIME 1 |
35 | # define IOM_TIME 0 |
33 | #endif |
36 | #endif |
34 | #ifndef IOM_CHECK |
37 | #ifndef IOM_CHECK |
35 | # define IOM_CHECK 1 |
38 | # define IOM_CHECK 0 |
36 | #endif |
39 | #endif |
37 | #ifndef IOM_IDLE |
40 | #ifndef IOM_IDLE |
38 | # define IOM_IDLE 0 |
41 | # define IOM_IDLE 0 |
39 | #endif |
42 | #endif |
40 | |
43 | |
41 | typedef double tstamp; |
44 | typedef double tstamp; |
42 | extern tstamp NOW; |
45 | extern tstamp NOW; |
43 | |
46 | |
|
|
47 | struct watcher; |
44 | #if IOM_IO |
48 | #if IOM_IO |
45 | struct io_watcher; |
49 | struct io_watcher; |
46 | #endif |
50 | #endif |
47 | #if IOM_TIME |
51 | #if IOM_TIME |
48 | struct time_watcher; |
52 | struct time_watcher; |
49 | #endif |
53 | #endif |
50 | #if IOM_CHECK |
54 | #if IOM_CHECK |
51 | struct check_watcher; |
55 | struct check_watcher; |
52 | #endif |
56 | #endif |
53 | #if IOM_IDLE |
57 | #if IOM_IDLE |
54 | struct idle_watcher; |
58 | struct idle_watcher; |
55 | #endif |
59 | #endif |
56 | |
60 | |
57 | template<class watcher> |
61 | template<class watcher> |
58 | struct io_manager_vec : protected simplevec<watcher *> { |
62 | struct io_manager_vec : vector<watcher *> { |
59 | friend class io_manager; |
63 | #if IOM_CHECK |
60 | protected: |
64 | bool activity; |
|
|
65 | #endif |
|
|
66 | |
61 | void erase_unordered (unsigned int pos) |
67 | void erase_unordered (unsigned int pos) |
62 | { |
68 | { |
63 | watcher *w = (*this)[size () - 1]; |
69 | watcher *w = (*this)[size () - 1]; |
64 | pop_back (); |
70 | pop_back (); |
65 | |
71 | |
… | |
… | |
82 | #if IOM_IDLE |
88 | #if IOM_IDLE |
83 | io_manager_vec<idle_watcher> iw; |
89 | io_manager_vec<idle_watcher> iw; |
84 | #endif |
90 | #endif |
85 | |
91 | |
86 | template<class watcher> |
92 | template<class watcher> |
87 | void reg (watcher *w, simplevec<watcher *> &queue); |
93 | void reg (watcher *w, io_manager_vec<watcher> &queue); |
88 | |
94 | |
89 | template<class watcher> |
95 | template<class watcher> |
90 | void unreg (watcher *w, simplevec<watcher *> &queue); |
96 | void unreg (watcher *w, io_manager_vec<watcher> &queue); |
91 | |
97 | |
92 | public: |
98 | public: |
93 | // register a watcher |
99 | // register a watcher |
94 | #if IOM_IO |
100 | #if IOM_IO |
95 | void reg (io_watcher *w); void unreg (io_watcher *w); |
101 | void reg (io_watcher *w); void unreg (io_watcher *w); |
… | |
… | |
113 | extern io_manager iom; // a singleton, together with it's construction/destruction problems. |
119 | extern io_manager iom; // a singleton, together with it's construction/destruction problems. |
114 | |
120 | |
115 | struct watcher { |
121 | struct watcher { |
116 | int active; /* 0 == inactive, else index into respective vector */ |
122 | int active; /* 0 == inactive, else index into respective vector */ |
117 | |
123 | |
118 | watcher() : active(0) { } |
124 | watcher () : active (0) { } |
119 | }; |
125 | }; |
120 | |
126 | |
121 | #if IOM_IO |
127 | #if IOM_IO |
122 | enum { EVENT_READ = 1, EVENT_WRITE = 2 }; |
128 | enum { EVENT_READ = 1, EVENT_WRITE = 2 }; |
123 | |
129 | |
… | |
… | |
131 | void start () { iom.reg (this); } |
137 | void start () { iom.reg (this); } |
132 | void start (int fd_, short events_) { set (fd_, events_); iom.reg (this); } |
138 | void start (int fd_, short events_) { set (fd_, events_); iom.reg (this); } |
133 | void stop () { iom.unreg (this); } |
139 | void stop () { iom.unreg (this); } |
134 | |
140 | |
135 | template<class O1, class O2> |
141 | template<class O1, class O2> |
136 | io_watcher (O1 *object, void (O2::*method)(io_watcher &, short)) |
142 | io_watcher (O1 *object, void (O2::*method) (io_watcher &, short)) |
137 | : callback2<void, io_watcher &, short>(object,method) |
143 | : callback2<void, io_watcher &, short> (object,method) |
138 | { } |
144 | { } |
139 | ~io_watcher () { stop (); } |
145 | ~io_watcher () { stop (); } |
140 | }; |
146 | }; |
141 | #endif |
147 | #endif |
142 | |
148 | |
… | |
… | |
145 | tstamp at; |
151 | tstamp at; |
146 | |
152 | |
147 | void trigger (); |
153 | void trigger (); |
148 | |
154 | |
149 | void set (tstamp when) { at = when; } |
155 | void set (tstamp when) { at = when; } |
150 | void operator ()() { trigger (); } |
156 | void operator () () { trigger (); } |
151 | void start () { iom.reg (this); } |
157 | void start () { iom.reg (this); } |
152 | void start (tstamp when) { set (when); iom.reg (this); } |
158 | void start (tstamp when) { set (when); iom.reg (this); } |
153 | void stop () { iom.unreg (this); } |
159 | void stop () { iom.unreg (this); } |
154 | |
160 | |
155 | template<class O1, class O2> |
161 | template<class O1, class O2> |
156 | time_watcher (O1 *object, void (O2::*method)(time_watcher &)) |
162 | time_watcher (O1 *object, void (O2::*method) (time_watcher &)) |
157 | : callback1<void, time_watcher &>(object,method) |
163 | : callback1<void, time_watcher &> (object,method), at (0) |
158 | { } |
164 | { } |
159 | ~time_watcher () { stop (); } |
165 | ~time_watcher () { stop (); } |
160 | }; |
166 | }; |
161 | #endif |
167 | #endif |
162 | |
168 | |
… | |
… | |
165 | struct check_watcher : watcher, callback1<void, check_watcher &> { |
171 | struct check_watcher : watcher, callback1<void, check_watcher &> { |
166 | void start () { iom.reg (this); } |
172 | void start () { iom.reg (this); } |
167 | void stop () { iom.unreg (this); } |
173 | void stop () { iom.unreg (this); } |
168 | |
174 | |
169 | template<class O1, class O2> |
175 | template<class O1, class O2> |
170 | check_watcher (O1 *object, void (O2::*method)(check_watcher &)) |
176 | check_watcher (O1 *object, void (O2::*method) (check_watcher &)) |
171 | : callback1<void, check_watcher &>(object,method) |
177 | : callback1<void, check_watcher &> (object,method) |
172 | { } |
178 | { } |
173 | ~check_watcher () { stop (); } |
179 | ~check_watcher () { stop (); } |
174 | }; |
180 | }; |
175 | #endif |
181 | #endif |
176 | |
182 | |
… | |
… | |
179 | struct idle_watcher : watcher, callback1<void, idle_watcher &> { |
185 | struct idle_watcher : watcher, callback1<void, idle_watcher &> { |
180 | void start () { iom.reg (this); } |
186 | void start () { iom.reg (this); } |
181 | void stop () { iom.unreg (this); } |
187 | void stop () { iom.unreg (this); } |
182 | |
188 | |
183 | template<class O1, class O2> |
189 | template<class O1, class O2> |
184 | idle_watcher (O1 *object, void (O2::*method)(idle_watcher &)) |
190 | idle_watcher (O1 *object, void (O2::*method) (idle_watcher &)) |
185 | : callback1<void, idle_watcher &>(object,method) |
191 | : callback1<void, idle_watcher &> (object,method) |
186 | { } |
192 | { } |
187 | ~idle_watcher () { stop (); } |
193 | ~idle_watcher () { stop (); } |
188 | }; |
194 | }; |
189 | #endif |
195 | #endif |
190 | |
196 | |