ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/gvpe/src/iom.h
Revision: 1.7
Committed: Fri Mar 28 05:40:54 2003 UTC (21 years, 2 months ago) by pcg
Content type: text/plain
Branch: MAIN
Changes since 1.6: +4 -4 lines
Log Message:
*** empty log message ***

File Contents

# Content
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 typedef double tstamp;
29
30 extern tstamp NOW;
31
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<const io_watcher *> iow;
39 vector<time_watcher *> tw; // actually a heap
40
41 void idle_cb (tstamp &ts); time_watcher *idle;
42 public:
43
44 void reschedule_time_watchers ();
45
46 // register a watcher
47 void reg (int fd, short events, io_watcher *w);
48 void unreg (io_watcher *w);
49 void reg (time_watcher *w);
50 void unreg (time_watcher *w);
51
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 virtual R call (void *obj, R (object::*meth)(A), A arg) = 0;
70 };
71 template<class O1, class O2>
72 struct proxy : proxy_base {
73 virtual R call (void *obj, R (object::*meth)(A), A arg)
74 {
75 ((reinterpret_cast<O1 *>(obj)) ->* (reinterpret_cast<R (O2::*)(A)>(meth)))
76 (arg);
77 }
78 };
79
80 proxy_base *prxy;
81
82 public:
83 template<class O1, class O2>
84 callback (O1 *object, R (O2::*method)(A))
85 {
86 static proxy<O1,O2> p;
87 obj = reinterpret_cast<void *>(object);
88 meth = reinterpret_cast<R (object::*)(A)>(method);
89 prxy = &p;
90 }
91
92 R call(A arg) const
93 {
94 return prxy->call (obj, meth, arg);
95 }
96
97 R operator ()(A arg) const
98 {
99 return call (arg);
100 }
101 };
102
103 struct io_watcher : callback<void, short> {
104 template<class O1, class O2>
105 io_watcher (O1 *object, void (O2::*method)(short revents))
106 : callback<void, short>(object,method)
107 { }
108
109 ~io_watcher ()
110 {
111 iom.unreg (this);
112 }
113
114 void start (int fd, short events)
115 {
116 iom.reg (fd, events, this);
117 }
118
119 void stop ()
120 {
121 iom.unreg (this);
122 }
123 };
124
125 #define TSTAMP_CANCEL -1.
126
127 struct time_watcher : callback<void, tstamp &> {
128 bool registered; // already registered?
129 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 , registered(false)
135 { }
136
137 ~time_watcher ()
138 {
139 iom.unreg (this);
140 }
141
142 void set (tstamp when);
143 void trigger ();
144
145 void operator ()()
146 {
147 trigger ();
148 }
149
150 void start ();
151 void start (tstamp when)
152 {
153 set (when);
154 }
155
156 void stop ()
157 {
158 iom.unreg (this);
159 }
160
161 void reset (tstamp when = TSTAMP_CANCEL)
162 {
163 stop ();
164 at = when;
165 }
166 };
167
168 #endif
169