… | |
… | |
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 |
|
|
46 | #ifndef IOM_ACCURACY |
|
|
47 | # define IOM_ACCURACY 0.001 // start timers at most this much earlier (can be 0) |
|
|
48 | #endif |
43 | |
49 | |
44 | typedef double tstamp; |
50 | typedef double tstamp; |
45 | extern tstamp NOW; |
51 | extern tstamp NOW; |
46 | |
52 | |
47 | struct watcher; |
53 | struct watcher; |
… | |
… | |
55 | struct check_watcher; |
61 | struct check_watcher; |
56 | #endif |
62 | #endif |
57 | #if IOM_IDLE |
63 | #if IOM_IDLE |
58 | struct idle_watcher; |
64 | struct idle_watcher; |
59 | #endif |
65 | #endif |
|
|
66 | #if IOM_SIG |
|
|
67 | struct sig_watcher; |
|
|
68 | #endif |
60 | |
69 | |
61 | template<class watcher> |
70 | template<class watcher> |
62 | struct io_manager_vec : protected vector<watcher *> { |
71 | struct io_manager_vec : vector<watcher *> { |
63 | friend class io_manager; |
|
|
64 | protected: |
|
|
65 | #if IOM_CHECK |
|
|
66 | bool activity; |
|
|
67 | #endif |
|
|
68 | |
|
|
69 | void erase_unordered (unsigned int pos) |
72 | void erase_unordered (unsigned int pos) |
70 | { |
73 | { |
71 | watcher *w = (*this)[size () - 1]; |
74 | watcher *w = (*this)[this->size () - 1]; |
72 | pop_back (); |
75 | this->pop_back (); |
73 | |
76 | |
74 | if (size ()) |
77 | if (!this->empty ()) |
75 | if ((*this)[pos] = w) |
78 | if (((*this)[pos] = w)) // '=' is correct! |
76 | w->active = pos + 1; |
79 | w->active = pos + 1; |
77 | } |
80 | } |
78 | }; |
81 | }; |
79 | |
82 | |
|
|
83 | // only used as a namespace, and for initialisation purposes |
80 | class io_manager { |
84 | class io_manager { |
81 | #if IOM_IO |
|
|
82 | io_manager_vec<io_watcher> iow; |
|
|
83 | #endif |
|
|
84 | #if IOM_CHECK |
|
|
85 | io_manager_vec<check_watcher> cw; |
|
|
86 | #endif |
|
|
87 | #if IOM_TIME |
|
|
88 | io_manager_vec<time_watcher> tw; |
|
|
89 | #endif |
|
|
90 | #if IOM_IDLE |
|
|
91 | io_manager_vec<idle_watcher> iw; |
|
|
92 | #endif |
|
|
93 | |
|
|
94 | template<class watcher> |
85 | template<class watcher> |
95 | void reg (watcher *w, io_manager_vec<watcher> &queue); |
86 | static void reg (watcher &w, io_manager_vec<watcher> &queue); |
96 | |
87 | |
97 | template<class watcher> |
88 | template<class watcher> |
98 | void unreg (watcher *w, io_manager_vec<watcher> &queue); |
89 | static void unreg (watcher &w, io_manager_vec<watcher> &queue); |
99 | |
90 | |
100 | public: |
91 | public: |
101 | // register a watcher |
92 | // register a watcher |
102 | #if IOM_IO |
93 | #if IOM_IO |
103 | void reg (io_watcher *w); void unreg (io_watcher *w); |
94 | static void reg (io_watcher &w); static void unreg (io_watcher &w); |
104 | #endif |
95 | #endif |
105 | #if IOM_TIME |
96 | #if IOM_TIME |
106 | void reg (time_watcher *w); void unreg (time_watcher *w); |
97 | static void reg (time_watcher &w); static void unreg (time_watcher &w); |
107 | #endif |
98 | #endif |
108 | #if IOM_CHECK |
99 | #if IOM_CHECK |
109 | void reg (check_watcher *w); void unreg (check_watcher *w); |
100 | static void reg (check_watcher &w); static void unreg (check_watcher &w); |
110 | #endif |
101 | #endif |
111 | #if IOM_IDLE |
102 | #if IOM_IDLE |
112 | void reg (idle_watcher *w); void unreg (idle_watcher *w); |
103 | static void reg (idle_watcher &w); static void unreg (idle_watcher &w); |
|
|
104 | #endif |
|
|
105 | #if IOM_SIG |
|
|
106 | static void reg (sig_watcher &w); static void unreg (sig_watcher &w); |
113 | #endif |
107 | #endif |
114 | |
108 | |
115 | void loop (); |
109 | static void loop (); |
116 | |
|
|
117 | io_manager (); |
|
|
118 | ~io_manager (); |
|
|
119 | }; |
110 | }; |
120 | |
|
|
121 | extern io_manager iom; // a singleton, together with it's construction/destruction problems. |
|
|
122 | |
111 | |
123 | struct watcher { |
112 | struct watcher { |
124 | int active; /* 0 == inactive, else index into respective vector */ |
113 | int active; /* 0 == inactive, else index into respective vector */ |
125 | |
114 | |
126 | watcher() : active(0) { } |
115 | watcher () : active (0) { } |
127 | }; |
116 | }; |
128 | |
117 | |
129 | #if IOM_IO |
118 | #if IOM_IO |
130 | enum { EVENT_READ = 1, EVENT_WRITE = 2 }; |
119 | enum { EVENT_READ = 1, EVENT_WRITE = 2 }; |
131 | |
120 | |
… | |
… | |
134 | short events; |
123 | short events; |
135 | |
124 | |
136 | void set (int fd_, short events_) { fd = fd_; events = events_; } |
125 | void set (int fd_, short events_) { fd = fd_; events = events_; } |
137 | |
126 | |
138 | void set (short events_) { set (fd, events_); } |
127 | void set (short events_) { set (fd, events_); } |
139 | void start () { iom.reg (this); } |
128 | void start () { io_manager::reg (*this); } |
140 | void start (int fd_, short events_) { set (fd_, events_); iom.reg (this); } |
129 | void start (int fd_, short events_) { set (fd_, events_); io_manager::reg (*this); } |
141 | void stop () { iom.unreg (this); } |
130 | void stop () { io_manager::unreg (*this); } |
142 | |
131 | |
143 | template<class O1, class O2> |
132 | template<class O1, class O2> |
144 | io_watcher (O1 *object, void (O2::*method)(io_watcher &, short)) |
133 | io_watcher (O1 *object, void (O2::*method) (io_watcher &, short)) |
145 | : callback2<void, io_watcher &, short>(object,method) |
134 | : callback2<void, io_watcher &, short> (object,method) |
146 | { } |
135 | { } |
147 | ~io_watcher () { stop (); } |
136 | ~io_watcher () { stop (); } |
148 | }; |
137 | }; |
149 | #endif |
138 | #endif |
150 | |
139 | |
… | |
… | |
153 | tstamp at; |
142 | tstamp at; |
154 | |
143 | |
155 | void trigger (); |
144 | void trigger (); |
156 | |
145 | |
157 | void set (tstamp when) { at = when; } |
146 | void set (tstamp when) { at = when; } |
158 | void operator ()() { trigger (); } |
147 | void operator () () { trigger (); } |
159 | void start () { iom.reg (this); } |
148 | void start () { io_manager::reg (*this); } |
160 | void start (tstamp when) { set (when); iom.reg (this); } |
149 | void start (tstamp when) { set (when); io_manager::reg (*this); } |
161 | void stop () { iom.unreg (this); } |
150 | void stop () { io_manager::unreg (*this); } |
162 | |
151 | |
163 | template<class O1, class O2> |
152 | template<class O1, class O2> |
164 | time_watcher (O1 *object, void (O2::*method)(time_watcher &)) |
153 | time_watcher (O1 *object, void (O2::*method) (time_watcher &)) |
165 | : callback1<void, time_watcher &>(object,method), at(0) |
154 | : callback1<void, time_watcher &> (object,method), at (0) |
166 | { } |
155 | { } |
167 | ~time_watcher () { stop (); } |
156 | ~time_watcher () { stop (); } |
168 | }; |
157 | }; |
169 | #endif |
158 | #endif |
170 | |
159 | |
171 | #if IOM_CHECK |
160 | #if IOM_CHECK |
172 | // run before checking for new events |
161 | // run before checking for new events |
173 | struct check_watcher : watcher, callback1<void, check_watcher &> { |
162 | struct check_watcher : watcher, callback1<void, check_watcher &> { |
174 | void start () { iom.reg (this); } |
163 | void start () { io_manager::reg (*this); } |
175 | void stop () { iom.unreg (this); } |
164 | void stop () { io_manager::unreg (*this); } |
176 | |
165 | |
177 | template<class O1, class O2> |
166 | template<class O1, class O2> |
178 | check_watcher (O1 *object, void (O2::*method)(check_watcher &)) |
167 | check_watcher (O1 *object, void (O2::*method) (check_watcher &)) |
179 | : callback1<void, check_watcher &>(object,method) |
168 | : callback1<void, check_watcher &> (object,method) |
180 | { } |
169 | { } |
181 | ~check_watcher () { stop (); } |
170 | ~check_watcher () { stop (); } |
182 | }; |
171 | }; |
183 | #endif |
172 | #endif |
184 | |
173 | |
185 | #if IOM_IDLE |
174 | #if IOM_IDLE |
186 | // run after checking for any i/o, but before waiting |
175 | // run after checking for any i/o, but before waiting |
187 | struct idle_watcher : watcher, callback1<void, idle_watcher &> { |
176 | struct idle_watcher : watcher, callback1<void, idle_watcher &> { |
188 | void start () { iom.reg (this); } |
177 | void start () { io_manager::reg (*this); } |
189 | void stop () { iom.unreg (this); } |
178 | void stop () { io_manager::unreg (*this); } |
190 | |
179 | |
191 | template<class O1, class O2> |
180 | template<class O1, class O2> |
192 | idle_watcher (O1 *object, void (O2::*method)(idle_watcher &)) |
181 | idle_watcher (O1 *object, void (O2::*method) (idle_watcher &)) |
193 | : callback1<void, idle_watcher &>(object,method) |
182 | : callback1<void, idle_watcher &> (object,method) |
194 | { } |
183 | { } |
195 | ~idle_watcher () { stop (); } |
184 | ~idle_watcher () { stop (); } |
196 | }; |
185 | }; |
197 | #endif |
186 | #endif |
198 | |
187 | |
199 | #endif |
188 | #if IOM_SIG |
|
|
189 | struct sig_watcher : watcher, callback1<void, sig_watcher &> { |
|
|
190 | int signum; |
200 | |
191 | |
|
|
192 | void start (int signum); |
|
|
193 | void stop () { io_manager::unreg (*this); } |
|
|
194 | |
|
|
195 | template<class O1, class O2> |
|
|
196 | sig_watcher (O1 *object, void (O2::*method) (sig_watcher &)) |
|
|
197 | : callback1<void, sig_watcher &> (object,method), signum (-1) |
|
|
198 | { } |
|
|
199 | ~sig_watcher () { stop (); } |
|
|
200 | }; |
|
|
201 | #endif |
|
|
202 | |
|
|
203 | #endif |
|
|
204 | |