1 |
/* |
2 |
iom.C -- I/O multiplexor |
3 |
|
4 |
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 |
the Free Software Foundation; either version 2 of the License, or |
7 |
(at your option) any later version. |
8 |
|
9 |
This program is distributed in the hope that it will be useful, |
10 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 |
GNU General Public License for more details. |
13 |
|
14 |
You should have received a copy of the GNU General Public License |
15 |
along with this program; if not, write to the Free Software |
16 |
Foundation, Inc. 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
17 |
*/ |
18 |
|
19 |
#include "config.h" |
20 |
|
21 |
#include <sys/time.h> |
22 |
|
23 |
#include <algorithm> |
24 |
#include <functional> |
25 |
|
26 |
#include "slog.h" |
27 |
#include "iom.h" |
28 |
|
29 |
inline bool lowest_first (const time_watcher *a, const time_watcher *b) |
30 |
{ |
31 |
return a->at > b->at; |
32 |
} |
33 |
|
34 |
tstamp NOW; |
35 |
|
36 |
io_manager iom; |
37 |
|
38 |
void time_watcher::set (tstamp when) |
39 |
{ |
40 |
at = when; |
41 |
|
42 |
if (registered) |
43 |
iom.reschedule_time_watchers (); |
44 |
else |
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); |
62 |
} |
63 |
|
64 |
void io_manager::reg (int fd, short events, io_watcher *w) |
65 |
{ |
66 |
pollfd pfd; |
67 |
|
68 |
pfd.fd = fd; |
69 |
pfd.events = events; |
70 |
|
71 |
pfs.push_back (pfd); |
72 |
iow.push_back (w); |
73 |
} |
74 |
|
75 |
void io_manager::unreg (io_watcher *w) |
76 |
{ |
77 |
unsigned int sz = iow.size (); |
78 |
unsigned int i = find (iow.begin (), iow.end (), w) - iow.begin (); |
79 |
|
80 |
if (i != sz) |
81 |
{ |
82 |
if (sz == 1) |
83 |
{ |
84 |
pfs.clear (); |
85 |
iow.clear (); |
86 |
} |
87 |
else if (i == sz - 1) |
88 |
{ |
89 |
iow.pop_back (); |
90 |
pfs.pop_back (); |
91 |
} |
92 |
else |
93 |
{ |
94 |
iow[i] = iow[sz - 1]; iow.pop_back (); |
95 |
pfs[i] = pfs[sz - 1]; pfs.pop_back (); |
96 |
} |
97 |
} |
98 |
} |
99 |
|
100 |
void io_manager::reschedule_time_watchers () |
101 |
{ |
102 |
make_heap (tw.begin (), tw.end (), lowest_first); |
103 |
} |
104 |
|
105 |
void io_manager::reg (time_watcher *w) |
106 |
{ |
107 |
if (w->registered) |
108 |
slog (L_CRIT, "FATAL: io_manager::reg(time_watcher) called on already-registered watcher"); |
109 |
|
110 |
w->registered = true; |
111 |
|
112 |
tw.push_back (w); |
113 |
push_heap (tw.begin (), tw.end (), lowest_first); |
114 |
} |
115 |
|
116 |
void io_manager::unreg (time_watcher *w) |
117 |
{ |
118 |
if (w->registered) |
119 |
{ |
120 |
unsigned int sz = tw.size (); |
121 |
unsigned int i = find (tw.begin (), tw.end (), w) - tw.begin (); |
122 |
|
123 |
if (i != sz) |
124 |
{ |
125 |
if (i != sz - 1) |
126 |
tw[i] = tw[sz - 1]; |
127 |
|
128 |
tw.pop_back (); |
129 |
reschedule_time_watchers (); |
130 |
} |
131 |
|
132 |
w->registered = false; |
133 |
} |
134 |
} |
135 |
|
136 |
inline void set_now (void) |
137 |
{ |
138 |
struct timeval tv; |
139 |
|
140 |
gettimeofday (&tv, 0); |
141 |
|
142 |
NOW = (tstamp)tv.tv_sec + (tstamp)tv.tv_usec / 1000000; |
143 |
} |
144 |
|
145 |
void io_manager::loop () |
146 |
{ |
147 |
set_now (); |
148 |
|
149 |
for (;;) |
150 |
{ |
151 |
while (tw[0]->at <= NOW) |
152 |
{ |
153 |
// remove the first watcher |
154 |
time_watcher *w = tw[0]; |
155 |
|
156 |
pop_heap (tw.begin (), tw.end (), lowest_first); |
157 |
tw.pop_back (); |
158 |
|
159 |
w->registered = false; |
160 |
|
161 |
// call it |
162 |
w->call (w->at); |
163 |
|
164 |
// re-add it if necessary |
165 |
if (w->at >= 0 && !w->registered) |
166 |
reg (w); |
167 |
} |
168 |
|
169 |
int timeout = (int) ((tw[0]->at - NOW) * 1000); |
170 |
|
171 |
int fds = poll (&pfs[0], pfs.size (), timeout); |
172 |
|
173 |
set_now (); |
174 |
|
175 |
for (unsigned int i = iow.size (); fds > 0 && i--; ) |
176 |
if (pfs[i].revents) |
177 |
{ |
178 |
--fds; |
179 |
iow[i]->call (pfs[i].revents); |
180 |
} |
181 |
} |
182 |
} |
183 |
|
184 |
void io_manager::idle_cb (tstamp &ts) |
185 |
{ |
186 |
ts = NOW + 86400; // wake up every day, for no good reason |
187 |
} |
188 |
|
189 |
io_manager::io_manager () |
190 |
{ |
191 |
set_now (); |
192 |
idle = new time_watcher (this, &io_manager::idle_cb); |
193 |
idle->start (0); |
194 |
} |
195 |
|
196 |
io_manager::~io_manager () |
197 |
{ |
198 |
// |
199 |
} |
200 |
|