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