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 | |
31 | inline bool lowest_first (const time_watcher *a, const time_watcher *b) |
29 | inline bool lowest_first (const time_watcher *a, const time_watcher *b) |
32 | { |
30 | { |
33 | return a->at > b->at; |
31 | return a->at > b->at; |
… | |
… | |
37 | |
35 | |
38 | io_manager iom; |
36 | io_manager iom; |
39 | |
37 | |
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); |
|
|
46 | } |
|
|
47 | |
|
|
48 | void time_watcher::trigger () |
|
|
49 | { |
|
|
50 | call (at); |
|
|
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); |
45 | } |
62 | } |
46 | |
63 | |
47 | void io_manager::reg (int fd, short events, io_watcher *w) |
64 | void io_manager::reg (int fd, short events, io_watcher *w) |
48 | { |
65 | { |
49 | pollfd pfd; |
66 | pollfd pfd; |
… | |
… | |
53 | |
70 | |
54 | pfs.push_back (pfd); |
71 | pfs.push_back (pfd); |
55 | iow.push_back (w); |
72 | iow.push_back (w); |
56 | } |
73 | } |
57 | |
74 | |
58 | void io_manager::unreg (io_watcher *w) |
75 | void io_manager::unreg (const io_watcher *w) |
59 | { |
76 | { |
60 | unsigned int sz = iow.size (); |
77 | unsigned int sz = iow.size (); |
61 | unsigned int i = find (iow.begin (), iow.end (), w) - iow.begin (); |
78 | unsigned int i = find (iow.begin (), iow.end (), w) - iow.begin (); |
62 | |
79 | |
63 | if (i != sz) |
80 | if (i != sz) |
… | |
… | |
78 | pfs[i] = pfs[sz - 1]; pfs.pop_back (); |
95 | pfs[i] = pfs[sz - 1]; pfs.pop_back (); |
79 | } |
96 | } |
80 | } |
97 | } |
81 | } |
98 | } |
82 | |
99 | |
|
|
100 | void io_manager::reschedule_time_watchers () |
|
|
101 | { |
|
|
102 | make_heap (tw.begin (), tw.end (), lowest_first); |
|
|
103 | } |
|
|
104 | |
83 | void io_manager::reg (time_watcher *w) |
105 | void io_manager::reg (time_watcher *w) |
84 | { |
106 | { |
|
|
107 | if (w->registered) |
|
|
108 | slog (L_CRIT, "FATAL: io_manager::reg(time_watcher) called on already-registered watcher"); |
|
|
109 | |
85 | tw.push_back (w); |
110 | tw.push_back (w); |
86 | push_heap (tw.begin (), tw.end (), lowest_first); |
111 | push_heap (tw.begin (), tw.end (), lowest_first); |
87 | } |
112 | } |
88 | |
113 | |
89 | void io_manager::unreg (time_watcher *w) |
114 | void io_manager::unreg (const time_watcher *w) |
90 | { |
115 | { |
91 | unsigned int sz = tw.size (); |
116 | unsigned int sz = tw.size (); |
92 | unsigned int i = find (tw.begin (), tw.end (), w) - tw.begin (); |
117 | unsigned int i = find (tw.begin (), tw.end (), w) - tw.begin (); |
93 | |
118 | |
94 | if (i != sz) |
119 | if (i != sz) |
95 | { |
120 | { |
96 | if (sz == 1) |
|
|
97 | tw.clear (); |
|
|
98 | else |
|
|
99 | { |
|
|
100 | if (i != sz - 1) |
121 | if (i != sz - 1) |
101 | tw[i] = tw[sz - 1]; |
122 | tw[i] = tw[sz - 1]; |
102 | |
123 | |
103 | tw.pop_back (); |
124 | tw.pop_back (); |
104 | make_heap (tw.begin (), tw.end (), lowest_first); |
125 | reschedule_time_watchers (); |
105 | } |
|
|
106 | } |
126 | } |
107 | } |
127 | } |
108 | |
128 | |
109 | inline void set_now (void) |
129 | inline void set_now (void) |
110 | { |
130 | { |
… | |
… | |
117 | |
137 | |
118 | void io_manager::loop () |
138 | void io_manager::loop () |
119 | { |
139 | { |
120 | set_now (); |
140 | set_now (); |
121 | |
141 | |
122 | while (!(iow.empty () && tw.empty ())) |
142 | for (;;) |
123 | { |
143 | { |
124 | int timeout = tw.empty () |
144 | while (tw[0]->at <= NOW) |
125 | ? 3600 * 1000 // wake up at least every hour |
145 | { |
126 | : (int) ((tw[0]->at - NOW) * 1000); |
146 | pop_heap (tw.begin (), tw.end (), lowest_first); |
|
|
147 | time_watcher *w = *(tw.end () - 1); |
|
|
148 | tw.pop_back (); |
127 | |
149 | |
128 | printf ("s%d t%d #%d\n", pfs.size (), timeout, tw.size ()); |
150 | if (w->at >= 0) |
|
|
151 | { |
|
|
152 | w->call (w->at); |
129 | |
153 | |
130 | if (timeout >= 0) |
154 | if (!w->registered) |
131 | { |
155 | reg (w); |
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 | } |
156 | } |
142 | } |
157 | } |
143 | |
158 | |
144 | while (!tw.empty () && tw[0]->at <= NOW) |
159 | int timeout = (int) ((tw[0]->at - NOW) * 1000); |
|
|
160 | |
|
|
161 | int fds = poll (&pfs[0], pfs.size (), timeout); |
|
|
162 | |
|
|
163 | set_now (); |
|
|
164 | |
|
|
165 | for (unsigned int i = iow.size (); fds > 0 && i--; ) |
|
|
166 | if (pfs[i].revents) |
145 | { |
167 | { |
146 | pop_heap (tw.begin (), tw.end (), lowest_first); |
168 | --fds; |
147 | (*(tw.end () - 1))->trigger (); |
169 | iow[i]->call (pfs[i].revents); |
148 | push_heap (tw.begin (), tw.end (), lowest_first); |
|
|
149 | } |
170 | } |
150 | } |
171 | } |
|
|
172 | } |
|
|
173 | |
|
|
174 | void io_manager::idle_cb (tstamp &ts) |
|
|
175 | { |
|
|
176 | ts = NOW + 86400; // wake up every day, for no good reason |
151 | } |
177 | } |
152 | |
178 | |
153 | io_manager::io_manager () |
179 | io_manager::io_manager () |
154 | { |
180 | { |
155 | set_now (); |
181 | set_now (); |
|
|
182 | idle = new time_watcher (this, &io_manager::idle_cb); |
|
|
183 | idle->start (0); |
156 | } |
184 | } |
157 | |
185 | |
158 | io_manager::~io_manager () |
186 | io_manager::~io_manager () |
159 | { |
187 | { |
160 | // |
188 | // |