1 |
pcg |
1.1 |
/* |
2 |
|
|
iom.h -- 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 |
|
|
#ifndef VPE_IOM_H__ |
20 |
|
|
#define VPE_IOM_H__ |
21 |
|
|
|
22 |
|
|
#include <vector> |
23 |
|
|
|
24 |
|
|
#include <sys/poll.h> |
25 |
|
|
|
26 |
|
|
#include "slog.h" |
27 |
|
|
|
28 |
pcg |
1.2 |
typedef double tstamp; |
29 |
pcg |
1.1 |
|
30 |
pcg |
1.2 |
extern tstamp NOW; |
31 |
pcg |
1.1 |
|
32 |
|
|
template<class R, class A> class callback; |
33 |
|
|
struct io_watcher; |
34 |
|
|
struct time_watcher; |
35 |
|
|
|
36 |
|
|
class io_manager { |
37 |
|
|
vector<pollfd> pfs; |
38 |
pcg |
1.5 |
vector<const io_watcher *> iow; |
39 |
pcg |
1.1 |
vector<time_watcher *> tw; // actually a heap |
40 |
pcg |
1.4 |
|
41 |
|
|
void idle_cb (tstamp &ts); time_watcher *idle; |
42 |
pcg |
1.1 |
public: |
43 |
|
|
|
44 |
pcg |
1.6 |
void reschedule_time_watchers (); |
45 |
|
|
|
46 |
pcg |
1.1 |
// register a watcher |
47 |
|
|
void reg (int fd, short events, io_watcher *w); |
48 |
pcg |
1.5 |
void unreg (const io_watcher *w); |
49 |
pcg |
1.1 |
void reg (time_watcher *w); |
50 |
pcg |
1.5 |
void unreg (const time_watcher *w); |
51 |
pcg |
1.1 |
|
52 |
|
|
void loop (); |
53 |
|
|
|
54 |
|
|
io_manager (); |
55 |
|
|
~io_manager (); |
56 |
|
|
}; |
57 |
|
|
|
58 |
|
|
extern io_manager iom; |
59 |
|
|
|
60 |
|
|
template<class R, class A> |
61 |
|
|
class callback { |
62 |
|
|
struct object { }; |
63 |
|
|
|
64 |
|
|
void *obj; |
65 |
|
|
R (object::*meth)(A arg); |
66 |
|
|
|
67 |
|
|
// a proxy is a kind of recipe on how to call a specific class method |
68 |
|
|
struct proxy_base { |
69 |
pcg |
1.5 |
virtual R call (void *obj, R (object::*meth)(A), A arg) = 0; |
70 |
pcg |
1.1 |
}; |
71 |
|
|
template<class O1, class O2> |
72 |
|
|
struct proxy : proxy_base { |
73 |
pcg |
1.5 |
virtual R call (void *obj, R (object::*meth)(A), A arg) |
74 |
pcg |
1.1 |
{ |
75 |
pcg |
1.5 |
((reinterpret_cast<O1 *>(obj)) ->* (reinterpret_cast<R (O2::*)(A)>(meth))) |
76 |
pcg |
1.1 |
(arg); |
77 |
|
|
} |
78 |
|
|
}; |
79 |
|
|
|
80 |
|
|
proxy_base *prxy; |
81 |
|
|
|
82 |
|
|
public: |
83 |
|
|
template<class O1, class O2> |
84 |
pcg |
1.5 |
callback (O1 *object, R (O2::*method)(A)) |
85 |
pcg |
1.1 |
{ |
86 |
|
|
static proxy<O1,O2> p; |
87 |
|
|
obj = reinterpret_cast<void *>(object); |
88 |
pcg |
1.5 |
meth = reinterpret_cast<R (object::*)(A)>(method); |
89 |
pcg |
1.1 |
prxy = &p; |
90 |
|
|
} |
91 |
|
|
|
92 |
pcg |
1.5 |
R call(A arg) const |
93 |
pcg |
1.1 |
{ |
94 |
|
|
return prxy->call (obj, meth, arg); |
95 |
|
|
} |
96 |
pcg |
1.2 |
|
97 |
pcg |
1.5 |
R operator ()(A arg) const |
98 |
pcg |
1.2 |
{ |
99 |
pcg |
1.4 |
return call (arg); |
100 |
pcg |
1.2 |
} |
101 |
pcg |
1.1 |
}; |
102 |
|
|
|
103 |
|
|
struct io_watcher : callback<void, short> { |
104 |
|
|
template<class O1, class O2> |
105 |
pcg |
1.2 |
io_watcher (O1 *object, void (O2::*method)(short revents)) |
106 |
pcg |
1.1 |
: callback<void, short>(object,method) |
107 |
pcg |
1.2 |
{ } |
108 |
|
|
|
109 |
pcg |
1.6 |
~io_watcher () |
110 |
|
|
{ |
111 |
|
|
iom.unreg (this); |
112 |
|
|
} |
113 |
|
|
|
114 |
pcg |
1.2 |
void start (int fd, short events) |
115 |
pcg |
1.1 |
{ |
116 |
|
|
iom.reg (fd, events, this); |
117 |
|
|
} |
118 |
|
|
|
119 |
pcg |
1.5 |
void stop () const |
120 |
pcg |
1.4 |
{ |
121 |
|
|
iom.unreg (this); |
122 |
|
|
} |
123 |
pcg |
1.1 |
}; |
124 |
|
|
|
125 |
pcg |
1.4 |
#define TSTAMP_CANCEL -1. |
126 |
|
|
|
127 |
pcg |
1.2 |
struct time_watcher : callback<void, tstamp &> { |
128 |
pcg |
1.6 |
bool registered; // already registered? |
129 |
pcg |
1.2 |
tstamp at; |
130 |
|
|
|
131 |
|
|
template<class O1, class O2> |
132 |
|
|
time_watcher (O1 *object, void (O2::*method)(tstamp &)) |
133 |
|
|
: callback<void, tstamp &>(object,method) |
134 |
pcg |
1.6 |
, registered(false) |
135 |
pcg |
1.2 |
{ } |
136 |
pcg |
1.6 |
|
137 |
|
|
~time_watcher () |
138 |
|
|
{ |
139 |
|
|
iom.unreg (this); |
140 |
|
|
} |
141 |
pcg |
1.1 |
|
142 |
pcg |
1.2 |
void set (tstamp when); |
143 |
pcg |
1.4 |
void trigger (); |
144 |
pcg |
1.1 |
|
145 |
pcg |
1.4 |
void operator ()() |
146 |
pcg |
1.1 |
{ |
147 |
pcg |
1.4 |
trigger (); |
148 |
pcg |
1.1 |
} |
149 |
|
|
|
150 |
pcg |
1.4 |
void start (); |
151 |
|
|
void start (tstamp when) |
152 |
pcg |
1.1 |
{ |
153 |
pcg |
1.2 |
set (when); |
154 |
pcg |
1.4 |
} |
155 |
|
|
|
156 |
pcg |
1.5 |
void stop () const |
157 |
pcg |
1.4 |
{ |
158 |
|
|
iom.unreg (this); |
159 |
|
|
} |
160 |
|
|
|
161 |
|
|
void reset (tstamp when = TSTAMP_CANCEL) |
162 |
|
|
{ |
163 |
|
|
stop (); |
164 |
|
|
at = when; |
165 |
pcg |
1.1 |
} |
166 |
|
|
}; |
167 |
|
|
|
168 |
|
|
#endif |
169 |
|
|
|