1 |
/* |
2 |
iom.C -- generic I/O multiplexor |
3 |
Copyright (C) 2003 Marc Lehmann <pcg@goof.com> |
4 |
|
5 |
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 |
7 |
the Free Software Foundation; either version 2 of the License, or |
8 |
(at your option) any later version. |
9 |
|
10 |
This program is distributed in the hope that it will be useful, |
11 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13 |
GNU General Public License for more details. |
14 |
|
15 |
You should have received a copy of the GNU General Public License |
16 |
along with this program; if not, write to the Free Software |
17 |
Foundation, Inc. 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
18 |
*/ |
19 |
|
20 |
#include "../config.h" |
21 |
|
22 |
#include <cstdio> |
23 |
|
24 |
#include <sys/select.h> |
25 |
#include <sys/time.h> |
26 |
|
27 |
#include "iom.h" |
28 |
|
29 |
tstamp NOW; |
30 |
bool iom_valid; |
31 |
io_manager iom; |
32 |
|
33 |
template<class watcher> |
34 |
void io_manager::reg (watcher *w, simplevec<watcher *> &queue) |
35 |
{ |
36 |
if (find (queue.begin (), queue.end (), w) == queue.end ()) |
37 |
queue.push_back (w); |
38 |
} |
39 |
|
40 |
template<class watcher> |
41 |
void io_manager::unreg (watcher *w, simplevec<watcher *> &queue) |
42 |
{ |
43 |
queue.erase (find (queue.begin (), queue.end (), w)); |
44 |
} |
45 |
|
46 |
#if IOM_IO |
47 |
io_watcher::~io_watcher () |
48 |
{ |
49 |
if (iom_valid) |
50 |
iom.unreg (this); |
51 |
} |
52 |
|
53 |
void io_manager::reg (io_watcher *w) |
54 |
{ |
55 |
reg (w, iow); |
56 |
} |
57 |
|
58 |
void io_manager::unreg (io_watcher *w) |
59 |
{ |
60 |
unreg (w, iow); |
61 |
} |
62 |
|
63 |
#endif |
64 |
|
65 |
#if IOM_TIME |
66 |
void time_watcher::trigger () |
67 |
{ |
68 |
call (*this); |
69 |
|
70 |
iom.reg (this); |
71 |
} |
72 |
|
73 |
time_watcher::~time_watcher () |
74 |
{ |
75 |
if (iom_valid) |
76 |
iom.unreg (this); |
77 |
|
78 |
at = TSTAMP_CANCEL; |
79 |
} |
80 |
|
81 |
void io_manager::reg (time_watcher *w) |
82 |
{ |
83 |
reg (w, tw); |
84 |
} |
85 |
|
86 |
void io_manager::unreg (time_watcher *w) |
87 |
{ |
88 |
unreg (w, tw); |
89 |
} |
90 |
#endif |
91 |
|
92 |
#if IOM_CHECK |
93 |
check_watcher::~check_watcher () |
94 |
{ |
95 |
if (iom_valid) |
96 |
iom.unreg (this); |
97 |
} |
98 |
|
99 |
void io_manager::reg (check_watcher *w) |
100 |
{ |
101 |
reg (w, cw); |
102 |
} |
103 |
|
104 |
void io_manager::unreg (check_watcher *w) |
105 |
{ |
106 |
unreg (w, cw); |
107 |
} |
108 |
#endif |
109 |
|
110 |
#if IOM_TIME |
111 |
inline void set_now (void) |
112 |
{ |
113 |
struct timeval tv; |
114 |
|
115 |
gettimeofday (&tv, 0); |
116 |
|
117 |
NOW = (tstamp)tv.tv_sec + (tstamp)tv.tv_usec / 1000000; |
118 |
#endif |
119 |
} |
120 |
|
121 |
void io_manager::loop () |
122 |
{ |
123 |
#if IOM_TIME |
124 |
set_now (); |
125 |
#endif |
126 |
|
127 |
for (;;) |
128 |
{ |
129 |
#if IOM_CHECK |
130 |
for (int i = 0; i < cw.size (); ++i) |
131 |
cw[i]->call (*cw[i]); |
132 |
#endif |
133 |
|
134 |
struct timeval *to = 0; |
135 |
|
136 |
#if IOM_TIME |
137 |
struct timeval tval; |
138 |
time_watcher *w; |
139 |
|
140 |
for (;tw.size ();) |
141 |
{ |
142 |
w = tw[0]; |
143 |
|
144 |
for (time_watcher **i = tw.begin (); i < tw.end (); ++i) |
145 |
if ((*i)->at < w->at) |
146 |
w = *i; |
147 |
|
148 |
if (w->at > NOW) |
149 |
{ |
150 |
double diff = w->at - NOW; |
151 |
tval.tv_sec = (int)diff; |
152 |
tval.tv_usec = (int)((diff - tval.tv_sec) * 1000000); |
153 |
to = &tval; |
154 |
break; |
155 |
} |
156 |
else if (w->at >= 0) |
157 |
w->call (*w); |
158 |
else |
159 |
unreg (w); |
160 |
} |
161 |
#endif |
162 |
|
163 |
#if IOM_IO |
164 |
fd_set rfd, wfd; |
165 |
|
166 |
FD_ZERO (&rfd); |
167 |
FD_ZERO (&wfd); |
168 |
|
169 |
int fds = 0; |
170 |
|
171 |
for (io_watcher **w = iow.begin (); w < iow.end (); ++w) |
172 |
{ |
173 |
if ((*w)->events & EVENT_READ ) FD_SET ((*w)->fd, &rfd); |
174 |
if ((*w)->events & EVENT_WRITE) FD_SET ((*w)->fd, &wfd); |
175 |
|
176 |
if ((*w)->fd >= fds) fds = (*w)->fd + 1; |
177 |
} |
178 |
|
179 |
if (!to && !fds) |
180 |
break; // no events |
181 |
|
182 |
fds = select (fds, &rfd, &wfd, 0, to); |
183 |
# if IOM_TIME |
184 |
set_now (); |
185 |
# endif |
186 |
|
187 |
if (fds > 0) |
188 |
for (int i = 0; i < iow.size (); ++i) |
189 |
{ |
190 |
io_watcher *w = iow[i]; |
191 |
|
192 |
short revents = w->events; |
193 |
|
194 |
if (!FD_ISSET (w->fd, &rfd)) revents &= ~EVENT_READ; |
195 |
if (!FD_ISSET (w->fd, &wfd)) revents &= ~EVENT_WRITE; |
196 |
|
197 |
if (revents) |
198 |
w->call (*w, revents); |
199 |
} |
200 |
#elif IOM_TIME |
201 |
if (!to) |
202 |
break; |
203 |
|
204 |
select (0, 0, 0, 0, &to); |
205 |
set_now (); |
206 |
#else |
207 |
break; |
208 |
#endif |
209 |
} |
210 |
} |
211 |
|
212 |
io_manager::io_manager () |
213 |
{ |
214 |
#if IOM_TIME |
215 |
set_now (); |
216 |
#endif |
217 |
|
218 |
iom_valid = true; |
219 |
} |
220 |
|
221 |
io_manager::~io_manager () |
222 |
{ |
223 |
iom_valid = false; |
224 |
} |
225 |
|