1 |
pcg |
1.1 |
/* |
2 |
|
|
iom.h -- 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 |
|
|
#ifndef VPE_IOM_H__ |
21 |
|
|
#define VPE_IOM_H__ |
22 |
|
|
|
23 |
|
|
#include <cassert> |
24 |
|
|
|
25 |
|
|
#include "rxvtvec.h" |
26 |
|
|
#include "callback.h" |
27 |
|
|
|
28 |
pcg |
1.2 |
#define IOM_IO 1 |
29 |
|
|
#define IOM_TIME 1 |
30 |
pcg |
1.4 |
#define IOM_CHECK 1 |
31 |
pcg |
1.5 |
#define IOM_IDLE 0 |
32 |
pcg |
1.2 |
|
33 |
|
|
#if IOM_IO |
34 |
|
|
typedef double tstamp; |
35 |
|
|
extern tstamp NOW; |
36 |
|
|
|
37 |
|
|
struct io_watcher; |
38 |
|
|
#endif |
39 |
|
|
#if IOM_TIME |
40 |
|
|
struct time_watcher; |
41 |
|
|
#endif |
42 |
|
|
#if IOM_CHECK |
43 |
|
|
struct check_watcher; |
44 |
|
|
#endif |
45 |
pcg |
1.5 |
#if IOM_IDLE |
46 |
|
|
struct idle_watcher; |
47 |
|
|
#endif |
48 |
pcg |
1.1 |
|
49 |
pcg |
1.2 |
class io_manager { |
50 |
|
|
#if IOM_IO |
51 |
|
|
simplevec<io_watcher *> iow; |
52 |
|
|
#endif |
53 |
|
|
#if IOM_CHECK |
54 |
|
|
simplevec<check_watcher *> cw; |
55 |
|
|
#endif |
56 |
|
|
#if IOM_TIME |
57 |
|
|
simplevec<time_watcher *> tw; |
58 |
|
|
#endif |
59 |
pcg |
1.5 |
#if IOM_IDLE |
60 |
|
|
simplevec<idle_watcher *> iw; |
61 |
|
|
#endif |
62 |
pcg |
1.1 |
|
63 |
pcg |
1.2 |
template<class watcher> |
64 |
|
|
void reg (watcher *w, simplevec<watcher *> &queue); |
65 |
pcg |
1.1 |
|
66 |
pcg |
1.2 |
template<class watcher> |
67 |
|
|
void unreg (watcher *w, simplevec<watcher *> &queue); |
68 |
pcg |
1.1 |
|
69 |
|
|
public: |
70 |
|
|
// register a watcher |
71 |
pcg |
1.2 |
#if IOM_IO |
72 |
pcg |
1.5 |
void reg (io_watcher *w); void unreg (io_watcher *w); |
73 |
pcg |
1.2 |
#endif |
74 |
|
|
#if IOM_TIME |
75 |
pcg |
1.5 |
void reg (time_watcher *w); void unreg (time_watcher *w); |
76 |
pcg |
1.2 |
#endif |
77 |
|
|
#if IOM_CHECK |
78 |
|
|
void reg (check_watcher *w); void unreg (check_watcher *w); |
79 |
|
|
#endif |
80 |
pcg |
1.5 |
#if IOM_IDLE |
81 |
|
|
void reg (idle_watcher *w); void unreg (idle_watcher *w); |
82 |
|
|
#endif |
83 |
pcg |
1.1 |
|
84 |
|
|
void loop (); |
85 |
|
|
|
86 |
|
|
io_manager (); |
87 |
|
|
~io_manager (); |
88 |
|
|
}; |
89 |
|
|
|
90 |
|
|
extern io_manager iom; // a singleton, together with it's construction/destruction problems. |
91 |
|
|
|
92 |
pcg |
1.2 |
#if IOM_IO |
93 |
pcg |
1.1 |
enum { EVENT_READ = 1, EVENT_WRITE = 2 }; |
94 |
|
|
|
95 |
|
|
struct io_watcher : callback2<void, io_watcher &, short> { |
96 |
|
|
int fd; |
97 |
|
|
short events; |
98 |
|
|
|
99 |
|
|
template<class O1, class O2> |
100 |
|
|
io_watcher (O1 *object, void (O2::*method)(io_watcher &, short)) |
101 |
|
|
: callback2<void, io_watcher &, short>(object,method) |
102 |
|
|
{ } |
103 |
|
|
|
104 |
|
|
~io_watcher (); |
105 |
|
|
|
106 |
pcg |
1.5 |
void set (int fd_, short events_) { fd = fd_; events = events_; } |
107 |
pcg |
1.1 |
|
108 |
pcg |
1.5 |
void set (short events_) { set (fd, events_); } |
109 |
|
|
void start () { iom.reg (this); } |
110 |
pcg |
1.1 |
void start (int fd_, short events_) { set (fd_, events_); iom.reg (this); } |
111 |
|
|
void stop () { iom.unreg (this); } |
112 |
|
|
}; |
113 |
pcg |
1.2 |
#endif |
114 |
pcg |
1.1 |
|
115 |
pcg |
1.2 |
#if IOM_TIME |
116 |
|
|
enum { TSTAMP_CANCEL = -1 }; |
117 |
pcg |
1.1 |
|
118 |
|
|
struct time_watcher : callback1<void, time_watcher &> { |
119 |
|
|
tstamp at; |
120 |
|
|
|
121 |
|
|
template<class O1, class O2> |
122 |
|
|
time_watcher (O1 *object, void (O2::*method)(time_watcher &)) |
123 |
|
|
: callback1<void, time_watcher &>(object,method) |
124 |
|
|
{ } |
125 |
|
|
|
126 |
|
|
~time_watcher (); |
127 |
|
|
|
128 |
|
|
void trigger (); |
129 |
|
|
|
130 |
|
|
void set (tstamp when) { at = when; } |
131 |
|
|
void operator ()() { trigger (); } |
132 |
|
|
void start () { iom.reg (this); } |
133 |
|
|
void start (tstamp when) { set (when); iom.reg (this); } |
134 |
|
|
void stop () { iom.unreg (this); } |
135 |
|
|
|
136 |
|
|
void reset (tstamp when = TSTAMP_CANCEL) |
137 |
|
|
{ |
138 |
|
|
stop (); |
139 |
|
|
at = when; |
140 |
|
|
} |
141 |
|
|
}; |
142 |
pcg |
1.2 |
#endif |
143 |
|
|
|
144 |
|
|
#if IOM_CHECK |
145 |
|
|
// run before checking for new events |
146 |
|
|
struct check_watcher : callback1<void, check_watcher &> { |
147 |
|
|
template<class O1, class O2> |
148 |
|
|
check_watcher (O1 *object, void (O2::*method)(check_watcher &)) |
149 |
|
|
: callback1<void, check_watcher &>(object,method) |
150 |
|
|
{ } |
151 |
|
|
|
152 |
|
|
~check_watcher (); |
153 |
pcg |
1.5 |
|
154 |
|
|
void start () { iom.reg (this); } |
155 |
|
|
void stop () { iom.unreg (this); } |
156 |
|
|
}; |
157 |
|
|
#endif |
158 |
|
|
|
159 |
|
|
#if IOM_IDLE |
160 |
|
|
// run after checking for any i/o, but before waiting |
161 |
|
|
struct idle_watcher : callback1<void, idle_watcher &> { |
162 |
|
|
template<class O1, class O2> |
163 |
|
|
idle_watcher (O1 *object, void (O2::*method)(idle_watcher &)) |
164 |
|
|
: callback1<void, idle_watcher &>(object,method) |
165 |
|
|
{ } |
166 |
|
|
|
167 |
|
|
~idle_watcher (); |
168 |
pcg |
1.2 |
|
169 |
|
|
void start () { iom.reg (this); } |
170 |
|
|
void stop () { iom.unreg (this); } |
171 |
|
|
}; |
172 |
|
|
#endif |
173 |
pcg |
1.1 |
|
174 |
|
|
#endif |
175 |
|
|
|