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 earliest_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 | 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); |
41 | |
52 | |
42 | if (registered) |
53 | if (registered) |
43 | iom.reschedule_time_watchers (); |
54 | iom.reschedule_time_watchers (); |
44 | else |
55 | else |
45 | iom.reg (this); |
56 | iom.reg (this); |
46 | } |
57 | } |
47 | |
58 | |
48 | void time_watcher::trigger () |
59 | time_watcher::~time_watcher () |
49 | { |
60 | { |
50 | call (*this); |
61 | if (iom_valid) |
|
|
62 | iom.unreg (this); |
|
|
63 | } |
|
|
64 | |
|
|
65 | void io_watcher::set(int fd_, short events_) |
|
|
66 | { |
|
|
67 | fd = fd_; |
|
|
68 | events = events_; |
51 | |
69 | |
52 | if (registered) |
70 | if (registered) |
53 | iom.reschedule_time_watchers (); |
71 | { |
54 | else |
72 | iom.unreg (this); |
55 | iom.reg (this); |
73 | iom.reg (this); |
|
|
74 | } |
56 | } |
75 | } |
57 | |
76 | |
58 | void time_watcher::start () |
77 | io_watcher::~io_watcher () |
59 | { |
78 | { |
60 | if (!registered) |
79 | if (iom_valid) |
61 | iom.reg (this); |
80 | iom.unreg (this); |
62 | } |
81 | } |
63 | |
82 | |
64 | void io_manager::reg (io_watcher *w) |
83 | void io_manager::reg (io_watcher *w) |
65 | { |
84 | { |
|
|
85 | if (!w->registered) |
|
|
86 | { |
|
|
87 | w->registered = true; |
|
|
88 | |
66 | pollfd pfd; |
89 | pollfd pfd; |
67 | |
90 | |
68 | pfd.fd = w->fd; |
91 | pfd.fd = w->fd; |
69 | pfd.events = w->events; |
92 | pfd.events = w->events; |
70 | |
93 | |
71 | pfs.push_back (pfd); |
94 | pfs.push_back (pfd); |
72 | iow.push_back (w); |
95 | iow.push_back (w); |
|
|
96 | } |
73 | } |
97 | } |
74 | |
98 | |
75 | void io_manager::unreg (io_watcher *w) |
99 | void io_manager::unreg (io_watcher *w) |
76 | { |
100 | { |
|
|
101 | if (w->registered) |
|
|
102 | { |
|
|
103 | w->registered = false; |
|
|
104 | |
77 | unsigned int sz = iow.size (); |
105 | unsigned int sz = iow.size (); |
78 | unsigned int i = find (iow.begin (), iow.end (), w) - iow.begin (); |
106 | unsigned int i = find (iow.begin (), iow.end (), w) - iow.begin (); |
79 | |
107 | |
80 | if (i != sz) |
108 | assert (i != sz); |
81 | { |
109 | |
82 | if (sz == 1) |
110 | if (sz == 1) |
83 | { |
111 | { |
84 | pfs.clear (); |
112 | pfs.clear (); |
85 | iow.clear (); |
113 | iow.clear (); |
86 | } |
114 | } |
… | |
… | |
102 | make_heap (tw.begin (), tw.end (), earliest_first); |
130 | make_heap (tw.begin (), tw.end (), earliest_first); |
103 | } |
131 | } |
104 | |
132 | |
105 | void io_manager::reg (time_watcher *w) |
133 | void io_manager::reg (time_watcher *w) |
106 | { |
134 | { |
|
|
135 | if (!w->registered) |
|
|
136 | { |
|
|
137 | w->registered = true; |
|
|
138 | |
|
|
139 | tw.push_back (w); |
|
|
140 | push_heap (tw.begin (), tw.end (), earliest_first); |
|
|
141 | } |
|
|
142 | } |
|
|
143 | |
|
|
144 | void io_manager::unreg (time_watcher *w) |
|
|
145 | { |
107 | if (w->registered) |
146 | if (w->registered) |
108 | slog (L_CRIT, "FATAL: io_manager::reg(time_watcher) called on already-registered watcher"); |
147 | { |
109 | |
|
|
110 | w->registered = true; |
148 | w->registered = false; |
111 | |
149 | |
112 | tw.push_back (w); |
|
|
113 | push_heap (tw.begin (), tw.end (), earliest_first); |
|
|
114 | } |
|
|
115 | |
|
|
116 | void io_manager::unreg (time_watcher *w) |
|
|
117 | { |
|
|
118 | if (w->registered) |
|
|
119 | { |
|
|
120 | unsigned int sz = tw.size (); |
150 | unsigned int sz = tw.size (); |
121 | unsigned int i = find (tw.begin (), tw.end (), w) - tw.begin (); |
151 | unsigned int i = find (tw.begin (), tw.end (), w) - tw.begin (); |
122 | |
152 | |
123 | if (i != sz) |
153 | assert (i != sz); |
124 | { |
154 | |
125 | if (i != sz - 1) |
155 | if (i != sz - 1) |
126 | tw[i] = tw[sz - 1]; |
156 | tw[i] = tw[sz - 1]; |
127 | |
157 | |
128 | tw.pop_back (); |
158 | tw.pop_back (); |
129 | reschedule_time_watchers (); |
159 | reschedule_time_watchers (); |
130 | } |
|
|
131 | |
|
|
132 | w->registered = false; |
|
|
133 | } |
160 | } |
134 | } |
161 | } |
135 | |
162 | |
136 | inline void set_now (void) |
163 | inline void set_now (void) |
137 | { |
164 | { |
… | |
… | |
179 | fds > 0 && w < iow.end (); |
206 | fds > 0 && w < iow.end (); |
180 | ++w, ++p) |
207 | ++w, ++p) |
181 | if (p->revents) |
208 | if (p->revents) |
182 | { |
209 | { |
183 | --fds; |
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 |
184 | (*w)->call (**w, p->revents); |
218 | (*w)->call (**w, p->revents); |
185 | } |
219 | } |
186 | } |
220 | } |
187 | } |
221 | } |
188 | |
222 | |
189 | void io_manager::idle_cb (time_watcher &w) |
223 | void io_manager::idle_cb (time_watcher &w) |
… | |
… | |
191 | w.at = NOW + 86400; // wake up every day, for no good reason |
225 | w.at = NOW + 86400; // wake up every day, for no good reason |
192 | } |
226 | } |
193 | |
227 | |
194 | io_manager::io_manager () |
228 | io_manager::io_manager () |
195 | { |
229 | { |
|
|
230 | iom_valid = true; |
|
|
231 | |
196 | set_now (); |
232 | set_now (); |
197 | idle = new time_watcher (this, &io_manager::idle_cb); |
233 | idle = new time_watcher (this, &io_manager::idle_cb); |
198 | idle->start (0); |
234 | idle->start (0); |
199 | } |
235 | } |
200 | |
236 | |
201 | io_manager::~io_manager () |
237 | io_manager::~io_manager () |
202 | { |
238 | { |
203 | // |
239 | iom_valid = false; |
204 | } |
240 | } |
205 | |
241 | |