1 | #include <unistd.h> |
|
|
2 | /* |
1 | /* |
3 | iom.C -- I/O multiplexor |
2 | iom.C -- I/O multiplexor |
4 | |
3 | |
5 | This program is free software; you can redistribute it and/or modify |
4 | 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 |
5 | it under the terms of the GNU General Public License as published by |
… | |
… | |
23 | |
22 | |
24 | #include <algorithm> |
23 | #include <algorithm> |
25 | #include <functional> |
24 | #include <functional> |
26 | |
25 | |
27 | #include "slog.h" |
26 | #include "slog.h" |
28 | |
|
|
29 | #include "iom.h" |
27 | #include "iom.h" |
30 | |
28 | |
|
|
29 | tstamp NOW; |
|
|
30 | bool iom_valid; |
|
|
31 | io_manager iom; |
|
|
32 | |
31 | inline bool lowest_first (const time_watcher *a, const time_watcher *b) |
33 | inline bool earliest_first (const time_watcher *a, const time_watcher *b) |
32 | { |
34 | { |
33 | return a->at > b->at; |
35 | return a->at > b->at; |
34 | } |
36 | } |
35 | |
37 | |
36 | tstamp NOW; |
|
|
37 | |
|
|
38 | io_manager iom; |
|
|
39 | |
|
|
40 | void time_watcher::set (tstamp when) |
38 | void time_watcher::set (tstamp when) |
41 | { |
39 | { |
42 | iom.unreg (this); |
|
|
43 | at = when; |
40 | at = when; |
|
|
41 | |
|
|
42 | if (registered) |
|
|
43 | iom.reschedule_time_watchers (); |
|
|
44 | else |
44 | iom.reg (this); |
45 | iom.reg (this); |
45 | } |
46 | } |
46 | |
47 | |
|
|
48 | void time_watcher::trigger () |
|
|
49 | { |
|
|
50 | call (*this); |
|
|
51 | |
|
|
52 | if (registered) |
|
|
53 | iom.reschedule_time_watchers (); |
|
|
54 | else |
|
|
55 | iom.reg (this); |
|
|
56 | } |
|
|
57 | |
|
|
58 | void time_watcher::start () |
|
|
59 | { |
|
|
60 | if (!registered) |
|
|
61 | iom.reg (this); |
|
|
62 | } |
|
|
63 | |
|
|
64 | time_watcher::~time_watcher () |
|
|
65 | { |
|
|
66 | if (iom_valid) |
|
|
67 | iom.unreg (this); |
|
|
68 | } |
|
|
69 | |
|
|
70 | io_watcher::~io_watcher () |
|
|
71 | { |
|
|
72 | if (iom_valid) |
|
|
73 | iom.unreg (this); |
|
|
74 | } |
|
|
75 | |
47 | void io_manager::reg (int fd, short events, io_watcher *w) |
76 | void io_manager::reg (io_watcher *w) |
48 | { |
77 | { |
49 | pollfd pfd; |
78 | pollfd pfd; |
50 | |
|
|
51 | pfd.fd = fd; |
|
|
52 | pfd.events = events; |
|
|
53 | |
79 | |
54 | pfs.push_back (pfd); |
80 | pfs.push_back (pfd); |
55 | iow.push_back (w); |
81 | iow.push_back (w); |
|
|
82 | |
|
|
83 | w->p = &(*(pfs.end () - 1)); |
56 | } |
84 | } |
57 | |
85 | |
58 | void io_manager::unreg (io_watcher *w) |
86 | void io_manager::unreg (io_watcher *w) |
59 | { |
87 | { |
60 | unsigned int sz = iow.size (); |
88 | if (w->p) |
61 | unsigned int i = find (iow.begin (), iow.end (), w) - iow.begin (); |
|
|
62 | |
|
|
63 | if (i != sz) |
|
|
64 | { |
89 | { |
|
|
90 | unsigned int sz = iow.size (); |
|
|
91 | unsigned int i = find (iow.begin (), iow.end (), w) - iow.begin (); |
|
|
92 | |
|
|
93 | assert (i != sz); |
|
|
94 | |
65 | if (sz == 1) |
95 | if (sz == 1) |
66 | { |
96 | { |
67 | pfs.clear (); |
97 | pfs.clear (); |
68 | iow.clear (); |
98 | iow.clear (); |
69 | } |
99 | } |
… | |
… | |
75 | else |
105 | else |
76 | { |
106 | { |
77 | iow[i] = iow[sz - 1]; iow.pop_back (); |
107 | iow[i] = iow[sz - 1]; iow.pop_back (); |
78 | pfs[i] = pfs[sz - 1]; pfs.pop_back (); |
108 | pfs[i] = pfs[sz - 1]; pfs.pop_back (); |
79 | } |
109 | } |
|
|
110 | |
|
|
111 | w->p = 0; |
80 | } |
112 | } |
81 | } |
113 | } |
82 | |
114 | |
|
|
115 | void io_manager::reschedule_time_watchers () |
|
|
116 | { |
|
|
117 | make_heap (tw.begin (), tw.end (), earliest_first); |
|
|
118 | } |
|
|
119 | |
83 | void io_manager::reg (time_watcher *w) |
120 | void io_manager::reg (time_watcher *w) |
84 | { |
121 | { |
|
|
122 | w->registered = true; |
|
|
123 | |
85 | tw.push_back (w); |
124 | tw.push_back (w); |
86 | push_heap (tw.begin (), tw.end (), lowest_first); |
125 | push_heap (tw.begin (), tw.end (), earliest_first); |
87 | } |
126 | } |
88 | |
127 | |
89 | void io_manager::unreg (time_watcher *w) |
128 | void io_manager::unreg (time_watcher *w) |
90 | { |
129 | { |
91 | unsigned int sz = tw.size (); |
130 | if (w->registered) |
92 | unsigned int i = find (tw.begin (), tw.end (), w) - tw.begin (); |
|
|
93 | |
|
|
94 | if (i != sz) |
|
|
95 | { |
131 | { |
96 | if (sz == 1) |
132 | unsigned int sz = tw.size (); |
97 | tw.clear (); |
133 | unsigned int i = find (tw.begin (), tw.end (), w) - tw.begin (); |
98 | else |
134 | |
|
|
135 | assert (i != sz); |
99 | { |
136 | |
100 | if (i != sz - 1) |
137 | if (i != sz - 1) |
101 | tw[i] = tw[sz - 1]; |
138 | tw[i] = tw[sz - 1]; |
102 | |
139 | |
|
|
140 | tw.pop_back (); |
|
|
141 | reschedule_time_watchers (); |
|
|
142 | |
|
|
143 | w->registered = false; |
|
|
144 | } |
|
|
145 | } |
|
|
146 | |
|
|
147 | inline void set_now (void) |
|
|
148 | { |
|
|
149 | struct timeval tv; |
|
|
150 | |
|
|
151 | gettimeofday (&tv, 0); |
|
|
152 | |
|
|
153 | NOW = (tstamp)tv.tv_sec + (tstamp)tv.tv_usec / 1000000; |
|
|
154 | } |
|
|
155 | |
|
|
156 | void io_manager::loop () |
|
|
157 | { |
|
|
158 | set_now (); |
|
|
159 | |
|
|
160 | for (;;) |
|
|
161 | { |
|
|
162 | while (tw[0]->at <= NOW) |
|
|
163 | { |
|
|
164 | // remove the first watcher |
|
|
165 | time_watcher *w = tw[0]; |
|
|
166 | |
|
|
167 | pop_heap (tw.begin (), tw.end (), earliest_first); |
103 | tw.pop_back (); |
168 | tw.pop_back (); |
104 | make_heap (tw.begin (), tw.end (), lowest_first); |
169 | |
|
|
170 | w->registered = false; |
|
|
171 | |
|
|
172 | // call it |
|
|
173 | w->call (*w); |
|
|
174 | |
|
|
175 | // re-add it if necessary |
|
|
176 | if (w->at >= 0 && !w->registered) |
|
|
177 | reg (w); |
|
|
178 | } |
|
|
179 | |
|
|
180 | int timeout = (int) ((tw[0]->at - NOW) * 1000); |
|
|
181 | |
|
|
182 | int fds = poll (&pfs[0], pfs.size (), timeout); |
|
|
183 | |
|
|
184 | set_now (); |
|
|
185 | |
|
|
186 | vector<io_watcher *>::iterator w; |
|
|
187 | vector<pollfd>::iterator p; |
|
|
188 | |
|
|
189 | for (w = iow.begin (), p = pfs.begin (); |
|
|
190 | fds > 0 && w < iow.end (); |
|
|
191 | ++w, ++p) |
|
|
192 | if (p->revents) |
|
|
193 | { |
|
|
194 | --fds; |
|
|
195 | (*w)->call (**w, p->revents); |
105 | } |
196 | } |
106 | } |
197 | } |
107 | } |
198 | } |
108 | |
199 | |
109 | inline void set_now (void) |
200 | void io_manager::idle_cb (time_watcher &w) |
110 | { |
201 | { |
111 | struct timeval tv; |
202 | 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 | } |
203 | } |
117 | |
204 | |
118 | void io_manager::loop () |
205 | io_manager::io_manager () |
119 | { |
206 | { |
|
|
207 | iom_valid = true; |
|
|
208 | |
120 | set_now (); |
209 | set_now (); |
121 | |
210 | idle = new time_watcher (this, &io_manager::idle_cb); |
122 | while (!(iow.empty () && tw.empty ())) |
211 | 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 | } |
212 | } |
157 | |
213 | |
158 | io_manager::~io_manager () |
214 | io_manager::~io_manager () |
159 | { |
215 | { |
160 | // |
216 | iom_valid = false; |
161 | } |
217 | } |
162 | |
218 | |