1 | #include <unistd.h> |
|
|
2 | /* |
1 | /* |
3 | iom.C -- I/O multiplexor |
2 | iom.C -- generic I/O multiplexor |
|
|
3 | Copyright (C) 2003 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. |
… | |
… | |
22 | #include <sys/time.h> |
22 | #include <sys/time.h> |
23 | |
23 | |
24 | #include <algorithm> |
24 | #include <algorithm> |
25 | #include <functional> |
25 | #include <functional> |
26 | |
26 | |
|
|
27 | #include "gettext.h" |
|
|
28 | |
27 | #include "slog.h" |
29 | #include "slog.h" |
28 | |
|
|
29 | #include "iom.h" |
30 | #include "iom.h" |
30 | |
31 | |
|
|
32 | tstamp NOW; |
|
|
33 | bool iom_valid; |
|
|
34 | io_manager iom; |
|
|
35 | |
31 | 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) |
32 | { |
37 | { |
33 | return a->at > b->at; |
38 | return a->at > b->at; |
34 | } |
39 | } |
35 | |
40 | |
36 | tstamp NOW; |
|
|
37 | |
|
|
38 | io_manager iom; |
|
|
39 | |
|
|
40 | void time_watcher::set (tstamp when) |
41 | void time_watcher::set (tstamp when) |
41 | { |
42 | { |
42 | iom.unreg (this); |
|
|
43 | at = when; |
43 | at = when; |
|
|
44 | |
|
|
45 | if (registered) |
|
|
46 | iom.reschedule_time_watchers (); |
|
|
47 | } |
|
|
48 | |
|
|
49 | void time_watcher::trigger () |
|
|
50 | { |
|
|
51 | call (*this); |
|
|
52 | |
|
|
53 | if (registered) |
|
|
54 | iom.reschedule_time_watchers (); |
|
|
55 | else |
44 | iom.reg (this); |
56 | iom.reg (this); |
45 | } |
57 | } |
46 | |
58 | |
47 | void io_manager::reg (int fd, short events, io_watcher *w) |
59 | time_watcher::~time_watcher () |
48 | { |
60 | { |
49 | pollfd pfd; |
61 | if (iom_valid) |
|
|
62 | iom.unreg (this); |
|
|
63 | } |
50 | |
64 | |
51 | pfd.fd = fd; |
65 | void io_watcher::set(int fd_, short events_) |
|
|
66 | { |
|
|
67 | fd = fd_; |
52 | pfd.events = events; |
68 | events = events_; |
53 | |
69 | |
|
|
70 | if (registered) |
|
|
71 | { |
|
|
72 | iom.unreg (this); |
|
|
73 | iom.reg (this); |
|
|
74 | } |
|
|
75 | } |
|
|
76 | |
|
|
77 | io_watcher::~io_watcher () |
|
|
78 | { |
|
|
79 | if (iom_valid) |
|
|
80 | iom.unreg (this); |
|
|
81 | } |
|
|
82 | |
|
|
83 | void io_manager::reg (io_watcher *w) |
|
|
84 | { |
|
|
85 | if (!w->registered) |
|
|
86 | { |
|
|
87 | w->registered = true; |
|
|
88 | |
|
|
89 | pollfd pfd; |
|
|
90 | |
|
|
91 | pfd.fd = w->fd; |
|
|
92 | pfd.events = w->events; |
|
|
93 | |
54 | pfs.push_back (pfd); |
94 | pfs.push_back (pfd); |
55 | iow.push_back (w); |
95 | iow.push_back (w); |
|
|
96 | } |
56 | } |
97 | } |
57 | |
98 | |
58 | void io_manager::unreg (io_watcher *w) |
99 | void io_manager::unreg (io_watcher *w) |
59 | { |
100 | { |
|
|
101 | if (w->registered) |
|
|
102 | { |
|
|
103 | w->registered = false; |
|
|
104 | |
60 | unsigned int sz = iow.size (); |
105 | unsigned int sz = iow.size (); |
61 | unsigned int i = find (iow.begin (), iow.end (), w) - iow.begin (); |
106 | unsigned int i = find (iow.begin (), iow.end (), w) - iow.begin (); |
62 | |
107 | |
63 | if (i != sz) |
108 | assert (i != sz); |
64 | { |
109 | |
65 | if (sz == 1) |
110 | if (sz == 1) |
66 | { |
111 | { |
67 | pfs.clear (); |
112 | pfs.clear (); |
68 | iow.clear (); |
113 | iow.clear (); |
69 | } |
114 | } |
… | |
… | |
78 | pfs[i] = pfs[sz - 1]; pfs.pop_back (); |
123 | pfs[i] = pfs[sz - 1]; pfs.pop_back (); |
79 | } |
124 | } |
80 | } |
125 | } |
81 | } |
126 | } |
82 | |
127 | |
|
|
128 | void io_manager::reschedule_time_watchers () |
|
|
129 | { |
|
|
130 | make_heap (tw.begin (), tw.end (), earliest_first); |
|
|
131 | } |
|
|
132 | |
83 | void io_manager::reg (time_watcher *w) |
133 | void io_manager::reg (time_watcher *w) |
84 | { |
134 | { |
|
|
135 | if (!w->registered) |
|
|
136 | { |
|
|
137 | w->registered = true; |
|
|
138 | |
85 | tw.push_back (w); |
139 | tw.push_back (w); |
86 | push_heap (tw.begin (), tw.end (), lowest_first); |
140 | push_heap (tw.begin (), tw.end (), earliest_first); |
|
|
141 | } |
87 | } |
142 | } |
88 | |
143 | |
89 | void io_manager::unreg (time_watcher *w) |
144 | void io_manager::unreg (time_watcher *w) |
90 | { |
145 | { |
|
|
146 | if (w->registered) |
|
|
147 | { |
|
|
148 | w->registered = false; |
|
|
149 | |
91 | unsigned int sz = tw.size (); |
150 | unsigned int sz = tw.size (); |
92 | unsigned int i = find (tw.begin (), tw.end (), w) - tw.begin (); |
151 | unsigned int i = find (tw.begin (), tw.end (), w) - tw.begin (); |
93 | |
152 | |
94 | if (i != sz) |
153 | assert (i != sz); |
95 | { |
|
|
96 | if (sz == 1) |
|
|
97 | tw.clear (); |
|
|
98 | else |
|
|
99 | { |
154 | |
100 | if (i != sz - 1) |
155 | if (i != sz - 1) |
101 | tw[i] = tw[sz - 1]; |
156 | tw[i] = tw[sz - 1]; |
102 | |
157 | |
|
|
158 | tw.pop_back (); |
|
|
159 | reschedule_time_watchers (); |
|
|
160 | } |
|
|
161 | } |
|
|
162 | |
|
|
163 | inline void set_now (void) |
|
|
164 | { |
|
|
165 | struct timeval tv; |
|
|
166 | |
|
|
167 | gettimeofday (&tv, 0); |
|
|
168 | |
|
|
169 | NOW = (tstamp)tv.tv_sec + (tstamp)tv.tv_usec / 1000000; |
|
|
170 | } |
|
|
171 | |
|
|
172 | void io_manager::loop () |
|
|
173 | { |
|
|
174 | set_now (); |
|
|
175 | |
|
|
176 | for (;;) |
|
|
177 | { |
|
|
178 | while (tw[0]->at <= NOW) |
|
|
179 | { |
|
|
180 | // remove the first watcher |
|
|
181 | time_watcher *w = tw[0]; |
|
|
182 | |
|
|
183 | pop_heap (tw.begin (), tw.end (), earliest_first); |
103 | tw.pop_back (); |
184 | tw.pop_back (); |
104 | make_heap (tw.begin (), tw.end (), lowest_first); |
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); |
|
|
194 | } |
|
|
195 | |
|
|
196 | int timeout = (int) ((tw[0]->at - NOW) * 1000); |
|
|
197 | |
|
|
198 | int fds = poll (&pfs[0], pfs.size (), timeout); |
|
|
199 | |
|
|
200 | set_now (); |
|
|
201 | |
|
|
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) |
|
|
208 | if (p->revents) |
|
|
209 | { |
|
|
210 | --fds; |
|
|
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); |
105 | } |
219 | } |
106 | } |
220 | } |
107 | } |
221 | } |
108 | |
222 | |
109 | inline void set_now (void) |
223 | void io_manager::idle_cb (time_watcher &w) |
110 | { |
224 | { |
111 | struct timeval tv; |
225 | w.at = NOW + 86400; // wake up every day, for no good reason |
112 | |
|
|
113 | gettimeofday (&tv, 0); |
|
|
114 | |
|
|
115 | NOW = (tstamp)tv.tv_sec + (tstamp)tv.tv_usec / 1000000; |
|
|
116 | } |
226 | } |
117 | |
227 | |
118 | void io_manager::loop () |
228 | io_manager::io_manager () |
119 | { |
229 | { |
|
|
230 | iom_valid = true; |
|
|
231 | |
120 | set_now (); |
232 | set_now (); |
121 | |
233 | idle = new time_watcher (this, &io_manager::idle_cb); |
122 | while (!(iow.empty () && tw.empty ())) |
234 | idle->start (0); |
123 | { |
|
|
124 | int timeout = tw.empty () |
|
|
125 | ? 3600 * 1000 // wake up at least every hour |
|
|
126 | : (int) ((tw[0]->at - NOW) * 1000); |
|
|
127 | |
|
|
128 | printf ("s%d t%d #%d\n", pfs.size (), timeout, tw.size ()); |
|
|
129 | |
|
|
130 | if (timeout >= 0) |
|
|
131 | { |
|
|
132 | int fds = poll (&pfs[0], pfs.size (), timeout); |
|
|
133 | |
|
|
134 | set_now (); |
|
|
135 | |
|
|
136 | for (unsigned int i = iow.size (); fds && i--; ) |
|
|
137 | if (pfs[i].revents) |
|
|
138 | { |
|
|
139 | --fds; |
|
|
140 | iow[i]->call (pfs[i].revents); |
|
|
141 | } |
|
|
142 | } |
|
|
143 | |
|
|
144 | while (!tw.empty () && tw[0]->at <= NOW) |
|
|
145 | { |
|
|
146 | pop_heap (tw.begin (), tw.end (), lowest_first); |
|
|
147 | (*(tw.end () - 1))->trigger (); |
|
|
148 | push_heap (tw.begin (), tw.end (), lowest_first); |
|
|
149 | } |
|
|
150 | } |
|
|
151 | } |
|
|
152 | |
|
|
153 | io_manager::io_manager () |
|
|
154 | { |
|
|
155 | set_now (); |
|
|
156 | } |
235 | } |
157 | |
236 | |
158 | io_manager::~io_manager () |
237 | io_manager::~io_manager () |
159 | { |
238 | { |
160 | // |
239 | iom_valid = false; |
161 | } |
240 | } |
162 | |
241 | |