1 | /* |
1 | /* |
2 | iom.C -- I/O multiplexor |
2 | iom.C -- generic I/O multiplexor |
|
|
3 | Copyright (C) 2003 Marc Lehmann <pcg@goof.com> |
3 | |
4 | |
4 | 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 |
5 | 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 |
6 | the Free Software Foundation; either version 2 of the License, or |
7 | the Free Software Foundation; either version 2 of the License, or |
7 | (at your option) any later version. |
8 | (at your option) any later version. |
… | |
… | |
21 | #include <sys/time.h> |
22 | #include <sys/time.h> |
22 | |
23 | |
23 | #include <algorithm> |
24 | #include <algorithm> |
24 | #include <functional> |
25 | #include <functional> |
25 | |
26 | |
|
|
27 | #include "gettext.h" |
|
|
28 | |
26 | #include "slog.h" |
29 | #include "slog.h" |
27 | #include "iom.h" |
30 | #include "iom.h" |
28 | |
31 | |
|
|
32 | tstamp NOW; |
|
|
33 | bool iom_valid; |
|
|
34 | io_manager iom; |
|
|
35 | |
29 | inline bool lowest_first (const time_watcher *a, const time_watcher *b) |
36 | inline bool earliest_first (const time_watcher *a, const time_watcher *b) |
30 | { |
37 | { |
31 | return a->at > b->at; |
38 | return a->at > b->at; |
32 | } |
39 | } |
33 | |
40 | |
34 | tstamp NOW; |
|
|
35 | |
|
|
36 | io_manager iom; |
|
|
37 | |
|
|
38 | void time_watcher::set (tstamp when) |
41 | void time_watcher::set (tstamp when) |
39 | { |
42 | { |
40 | iom.unreg (this); |
|
|
41 | at = when; |
43 | at = when; |
42 | iom.reg (this); |
44 | |
|
|
45 | if (registered) |
|
|
46 | iom.reschedule_time_watchers (); |
43 | } |
47 | } |
44 | |
48 | |
45 | void time_watcher::trigger () |
49 | void time_watcher::trigger () |
46 | { |
50 | { |
|
|
51 | call (*this); |
|
|
52 | |
|
|
53 | if (registered) |
|
|
54 | iom.reschedule_time_watchers (); |
|
|
55 | else |
|
|
56 | iom.reg (this); |
|
|
57 | } |
|
|
58 | |
|
|
59 | time_watcher::~time_watcher () |
|
|
60 | { |
|
|
61 | if (iom_valid) |
47 | iom.unreg (this); |
62 | iom.unreg (this); |
48 | call (at); |
63 | } |
|
|
64 | |
|
|
65 | void io_watcher::set(int fd_, short events_) |
|
|
66 | { |
|
|
67 | fd = fd_; |
|
|
68 | events = events_; |
|
|
69 | |
|
|
70 | if (registered) |
|
|
71 | { |
|
|
72 | iom.unreg (this); |
49 | iom.reg (this); |
73 | iom.reg (this); |
|
|
74 | } |
50 | } |
75 | } |
51 | |
76 | |
52 | void time_watcher::start () |
77 | io_watcher::~io_watcher () |
53 | { |
78 | { |
|
|
79 | if (iom_valid) |
54 | iom.unreg (this); |
80 | iom.unreg (this); |
55 | iom.reg (this); |
|
|
56 | } |
81 | } |
57 | |
82 | |
58 | void io_manager::reg (int fd, short events, io_watcher *w) |
83 | void io_manager::reg (io_watcher *w) |
59 | { |
84 | { |
|
|
85 | if (!w->registered) |
|
|
86 | { |
|
|
87 | w->registered = true; |
|
|
88 | |
60 | pollfd pfd; |
89 | pollfd pfd; |
61 | |
90 | |
62 | pfd.fd = fd; |
91 | pfd.fd = w->fd; |
63 | pfd.events = events; |
92 | pfd.events = w->events; |
64 | |
93 | |
65 | pfs.push_back (pfd); |
94 | pfs.push_back (pfd); |
66 | iow.push_back (w); |
95 | iow.push_back (w); |
|
|
96 | } |
67 | } |
97 | } |
68 | |
98 | |
69 | void io_manager::unreg (io_watcher *w) |
99 | void io_manager::unreg (io_watcher *w) |
70 | { |
100 | { |
|
|
101 | if (w->registered) |
|
|
102 | { |
|
|
103 | w->registered = false; |
|
|
104 | |
71 | unsigned int sz = iow.size (); |
105 | unsigned int sz = iow.size (); |
72 | unsigned int i = find (iow.begin (), iow.end (), w) - iow.begin (); |
106 | unsigned int i = find (iow.begin (), iow.end (), w) - iow.begin (); |
73 | |
107 | |
74 | if (i != sz) |
108 | assert (i != sz); |
75 | { |
109 | |
76 | if (sz == 1) |
110 | if (sz == 1) |
77 | { |
111 | { |
78 | pfs.clear (); |
112 | pfs.clear (); |
79 | iow.clear (); |
113 | iow.clear (); |
80 | } |
114 | } |
… | |
… | |
89 | pfs[i] = pfs[sz - 1]; pfs.pop_back (); |
123 | pfs[i] = pfs[sz - 1]; pfs.pop_back (); |
90 | } |
124 | } |
91 | } |
125 | } |
92 | } |
126 | } |
93 | |
127 | |
|
|
128 | void io_manager::reschedule_time_watchers () |
|
|
129 | { |
|
|
130 | make_heap (tw.begin (), tw.end (), earliest_first); |
|
|
131 | } |
|
|
132 | |
94 | void io_manager::reg (time_watcher *w) |
133 | void io_manager::reg (time_watcher *w) |
95 | { |
134 | { |
|
|
135 | if (!w->registered) |
|
|
136 | { |
|
|
137 | w->registered = true; |
|
|
138 | |
96 | tw.push_back (w); |
139 | tw.push_back (w); |
97 | push_heap (tw.begin (), tw.end (), lowest_first); |
140 | push_heap (tw.begin (), tw.end (), earliest_first); |
|
|
141 | } |
98 | } |
142 | } |
99 | |
143 | |
100 | void io_manager::unreg (time_watcher *w) |
144 | void io_manager::unreg (time_watcher *w) |
101 | { |
145 | { |
|
|
146 | if (w->registered) |
|
|
147 | { |
|
|
148 | w->registered = false; |
|
|
149 | |
102 | unsigned int sz = tw.size (); |
150 | unsigned int sz = tw.size (); |
103 | unsigned int i = find (tw.begin (), tw.end (), w) - tw.begin (); |
151 | unsigned int i = find (tw.begin (), tw.end (), w) - tw.begin (); |
104 | |
152 | |
105 | if (i != sz) |
153 | assert (i != sz); |
106 | { |
154 | |
107 | if (i != sz - 1) |
155 | if (i != sz - 1) |
108 | tw[i] = tw[sz - 1]; |
156 | tw[i] = tw[sz - 1]; |
109 | |
157 | |
110 | tw.pop_back (); |
158 | tw.pop_back (); |
111 | make_heap (tw.begin (), tw.end (), lowest_first); |
159 | reschedule_time_watchers (); |
112 | } |
160 | } |
113 | } |
161 | } |
114 | |
162 | |
115 | inline void set_now (void) |
163 | inline void set_now (void) |
116 | { |
164 | { |
… | |
… | |
127 | |
175 | |
128 | for (;;) |
176 | for (;;) |
129 | { |
177 | { |
130 | while (tw[0]->at <= NOW) |
178 | while (tw[0]->at <= NOW) |
131 | { |
179 | { |
|
|
180 | // remove the first watcher |
|
|
181 | time_watcher *w = tw[0]; |
|
|
182 | |
132 | pop_heap (tw.begin (), tw.end (), lowest_first); |
183 | pop_heap (tw.begin (), tw.end (), earliest_first); |
133 | time_watcher *w = *(tw.end () - 1); |
|
|
134 | |
|
|
135 | if (w->at >= 0) |
|
|
136 | { |
|
|
137 | w->call (w->at); |
|
|
138 | push_heap (tw.begin (), tw.end (), lowest_first); |
|
|
139 | } |
|
|
140 | else |
|
|
141 | tw.pop_back (); |
184 | tw.pop_back (); |
|
|
185 | |
|
|
186 | w->registered = false; |
|
|
187 | |
|
|
188 | // call it |
|
|
189 | w->call (*w); |
|
|
190 | |
|
|
191 | // re-add it if necessary |
|
|
192 | if (w->at >= 0 && !w->registered) |
|
|
193 | reg (w); |
142 | } |
194 | } |
143 | |
195 | |
144 | int timeout = (int) ((tw[0]->at - NOW) * 1000); |
196 | int timeout = (int) ((tw[0]->at - NOW) * 1000); |
145 | |
197 | |
146 | int fds = poll (&pfs[0], pfs.size (), timeout); |
198 | int fds = poll (&pfs[0], pfs.size (), timeout); |
147 | |
199 | |
148 | set_now (); |
200 | set_now (); |
149 | |
201 | |
150 | for (unsigned int i = iow.size (); fds > 0 && i--; ) |
202 | vector<io_watcher *>::iterator w; |
|
|
203 | vector<pollfd>::iterator p; |
|
|
204 | |
|
|
205 | for (w = iow.begin (), p = pfs.begin (); |
|
|
206 | fds > 0 && w < iow.end (); |
|
|
207 | ++w, ++p) |
151 | if (pfs[i].revents) |
208 | if (p->revents) |
152 | { |
209 | { |
153 | --fds; |
210 | --fds; |
154 | iow[i]->call (pfs[i].revents); |
211 | |
|
|
212 | if (p->revents & POLLNVAL) |
|
|
213 | { |
|
|
214 | slog (L_ERR, _("io_watcher started on illegal file descriptor, disabling.")); |
|
|
215 | (*w)->stop (); |
|
|
216 | } |
|
|
217 | else |
|
|
218 | (*w)->call (**w, p->revents); |
155 | } |
219 | } |
156 | } |
220 | } |
157 | } |
221 | } |
158 | |
222 | |
159 | void io_manager::idle_cb (tstamp &ts) |
223 | void io_manager::idle_cb (time_watcher &w) |
160 | { |
224 | { |
161 | ts = NOW + 86400; // wake up every day, for no good reason |
225 | w.at = NOW + 86400; // wake up every day, for no good reason |
162 | } |
226 | } |
163 | |
227 | |
164 | io_manager::io_manager () |
228 | io_manager::io_manager () |
165 | { |
229 | { |
|
|
230 | iom_valid = true; |
|
|
231 | |
166 | set_now (); |
232 | set_now (); |
167 | idle = new time_watcher (this, &io_manager::idle_cb); |
233 | idle = new time_watcher (this, &io_manager::idle_cb); |
168 | idle->start (0); |
234 | idle->start (0); |
169 | } |
235 | } |
170 | |
236 | |
171 | io_manager::~io_manager () |
237 | io_manager::~io_manager () |
172 | { |
238 | { |
173 | // |
239 | iom_valid = false; |
174 | } |
240 | } |
175 | |
241 | |