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 |
|
|
vector<io_watcher *> iow; |
39 |
|
|
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 |
|
|
// register a watcher |
45 |
|
|
void reg (int fd, short events, io_watcher *w); |
46 |
|
|
void unreg (io_watcher *w); |
47 |
|
|
void reg (time_watcher *w); |
48 |
|
|
void unreg (time_watcher *w); |
49 |
|
|
|
50 |
|
|
void loop (); |
51 |
|
|
|
52 |
|
|
io_manager (); |
53 |
|
|
~io_manager (); |
54 |
|
|
}; |
55 |
|
|
|
56 |
|
|
extern io_manager iom; |
57 |
|
|
|
58 |
|
|
template<class R, class A> |
59 |
|
|
class callback { |
60 |
|
|
struct object { }; |
61 |
|
|
|
62 |
|
|
void *obj; |
63 |
|
|
R (object::*meth)(A arg); |
64 |
|
|
|
65 |
|
|
// a proxy is a kind of recipe on how to call a specific class method |
66 |
|
|
struct proxy_base { |
67 |
|
|
virtual R call (void *obj, void (object::*meth)(A), A arg) = 0; |
68 |
|
|
}; |
69 |
|
|
template<class O1, class O2> |
70 |
|
|
struct proxy : proxy_base { |
71 |
|
|
virtual R call (void *obj, void (object::*meth)(A), A arg) |
72 |
|
|
{ |
73 |
|
|
((reinterpret_cast<O1 *>(obj)) ->* (reinterpret_cast<void (O2::*)(A)>(meth))) |
74 |
|
|
(arg); |
75 |
|
|
} |
76 |
|
|
}; |
77 |
|
|
|
78 |
|
|
proxy_base *prxy; |
79 |
|
|
|
80 |
|
|
public: |
81 |
|
|
template<class O1, class O2> |
82 |
|
|
callback (O1 *object, void (O2::*method)(A)) |
83 |
|
|
{ |
84 |
|
|
static proxy<O1,O2> p; |
85 |
|
|
obj = reinterpret_cast<void *>(object); |
86 |
|
|
meth = reinterpret_cast<void (object::*)(A)>(method); |
87 |
|
|
prxy = &p; |
88 |
|
|
} |
89 |
|
|
|
90 |
|
|
R call(A arg) |
91 |
|
|
{ |
92 |
|
|
return prxy->call (obj, meth, arg); |
93 |
|
|
} |
94 |
pcg |
1.2 |
|
95 |
pcg |
1.4 |
R operator ()(A arg) |
96 |
pcg |
1.2 |
{ |
97 |
pcg |
1.4 |
return call (arg); |
98 |
pcg |
1.2 |
} |
99 |
pcg |
1.1 |
}; |
100 |
|
|
|
101 |
|
|
struct io_watcher : callback<void, short> { |
102 |
|
|
template<class O1, class O2> |
103 |
pcg |
1.2 |
io_watcher (O1 *object, void (O2::*method)(short revents)) |
104 |
pcg |
1.1 |
: callback<void, short>(object,method) |
105 |
pcg |
1.2 |
{ } |
106 |
|
|
|
107 |
|
|
void start (int fd, short events) |
108 |
pcg |
1.1 |
{ |
109 |
|
|
iom.reg (fd, events, this); |
110 |
|
|
} |
111 |
|
|
|
112 |
pcg |
1.4 |
void stop () |
113 |
|
|
{ |
114 |
|
|
iom.unreg (this); |
115 |
|
|
} |
116 |
pcg |
1.1 |
}; |
117 |
|
|
|
118 |
pcg |
1.4 |
#define TSTAMP_CANCEL -1. |
119 |
|
|
|
120 |
pcg |
1.2 |
struct time_watcher : callback<void, tstamp &> { |
121 |
|
|
tstamp at; |
122 |
|
|
|
123 |
|
|
template<class O1, class O2> |
124 |
|
|
time_watcher (O1 *object, void (O2::*method)(tstamp &)) |
125 |
|
|
: callback<void, tstamp &>(object,method) |
126 |
|
|
{ } |
127 |
pcg |
1.1 |
|
128 |
pcg |
1.2 |
void set (tstamp when); |
129 |
pcg |
1.4 |
void trigger (); |
130 |
pcg |
1.1 |
|
131 |
pcg |
1.4 |
void operator ()() |
132 |
pcg |
1.1 |
{ |
133 |
pcg |
1.4 |
trigger (); |
134 |
pcg |
1.1 |
} |
135 |
|
|
|
136 |
pcg |
1.4 |
void start (); |
137 |
|
|
void start (tstamp when) |
138 |
pcg |
1.1 |
{ |
139 |
pcg |
1.2 |
set (when); |
140 |
pcg |
1.4 |
} |
141 |
|
|
|
142 |
|
|
void stop () |
143 |
|
|
{ |
144 |
|
|
iom.unreg (this); |
145 |
|
|
} |
146 |
|
|
|
147 |
|
|
void reset (tstamp when = TSTAMP_CANCEL) |
148 |
|
|
{ |
149 |
|
|
stop (); |
150 |
|
|
at = when; |
151 |
pcg |
1.1 |
} |
152 |
|
|
}; |
153 |
|
|
|
154 |
|
|
#endif |
155 |
|
|
|