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