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 |
#define IOM_IO 1 |
29 |
#define IOM_TIME 1 |
30 |
#undef IOM_CHECK |
31 |
|
32 |
#if IOM_IO |
33 |
typedef double tstamp; |
34 |
extern tstamp NOW; |
35 |
|
36 |
struct io_watcher; |
37 |
#endif |
38 |
#if IOM_TIME |
39 |
struct time_watcher; |
40 |
#endif |
41 |
#if IOM_CHECK |
42 |
struct check_watcher; |
43 |
#endif |
44 |
|
45 |
class io_manager { |
46 |
#if IOM_IO |
47 |
simplevec<io_watcher *> iow; |
48 |
#endif |
49 |
#if IOM_CHECK |
50 |
simplevec<check_watcher *> cw; |
51 |
#endif |
52 |
#if IOM_TIME |
53 |
simplevec<time_watcher *> tw; |
54 |
|
55 |
void idle_cb (time_watcher &w); time_watcher *idle; |
56 |
#endif |
57 |
|
58 |
template<class watcher> |
59 |
void reg (watcher *w, simplevec<watcher *> &queue); |
60 |
|
61 |
template<class watcher> |
62 |
void unreg (watcher *w, simplevec<watcher *> &queue); |
63 |
|
64 |
public: |
65 |
// register a watcher |
66 |
#if IOM_IO |
67 |
void reg (io_watcher *w); void unreg (io_watcher *w); |
68 |
#endif |
69 |
#if IOM_TIME |
70 |
void reg (time_watcher *w); void unreg (time_watcher *w); |
71 |
#endif |
72 |
#if IOM_CHECK |
73 |
void reg (check_watcher *w); void unreg (check_watcher *w); |
74 |
#endif |
75 |
|
76 |
void loop (); |
77 |
|
78 |
io_manager (); |
79 |
~io_manager (); |
80 |
}; |
81 |
|
82 |
extern io_manager iom; // a singleton, together with it's construction/destruction problems. |
83 |
|
84 |
#if IOM_IO |
85 |
enum { EVENT_READ = 1, EVENT_WRITE = 2 }; |
86 |
|
87 |
struct io_watcher : callback2<void, io_watcher &, short> { |
88 |
int fd; |
89 |
short events; |
90 |
|
91 |
template<class O1, class O2> |
92 |
io_watcher (O1 *object, void (O2::*method)(io_watcher &, short)) |
93 |
: callback2<void, io_watcher &, short>(object,method) |
94 |
{ } |
95 |
|
96 |
~io_watcher (); |
97 |
|
98 |
void set(int fd_, short events_) { fd = fd_; events = events_; } |
99 |
|
100 |
void set(short events_) { set (fd, events_); } |
101 |
void start (int fd_, short events_) { set (fd_, events_); iom.reg (this); } |
102 |
void stop () { iom.unreg (this); } |
103 |
}; |
104 |
#endif |
105 |
|
106 |
#if IOM_TIME |
107 |
enum { TSTAMP_CANCEL = -1 }; |
108 |
|
109 |
struct time_watcher : callback1<void, time_watcher &> { |
110 |
tstamp at; |
111 |
|
112 |
template<class O1, class O2> |
113 |
time_watcher (O1 *object, void (O2::*method)(time_watcher &)) |
114 |
: callback1<void, time_watcher &>(object,method) |
115 |
{ } |
116 |
|
117 |
~time_watcher (); |
118 |
|
119 |
void trigger (); |
120 |
|
121 |
void set (tstamp when) { at = when; } |
122 |
void operator ()() { trigger (); } |
123 |
void start () { iom.reg (this); } |
124 |
void start (tstamp when) { set (when); iom.reg (this); } |
125 |
void stop () { iom.unreg (this); } |
126 |
|
127 |
void reset (tstamp when = TSTAMP_CANCEL) |
128 |
{ |
129 |
stop (); |
130 |
at = when; |
131 |
} |
132 |
}; |
133 |
#endif |
134 |
|
135 |
#if IOM_CHECK |
136 |
// run before checking for new events |
137 |
struct check_watcher : callback1<void, check_watcher &> { |
138 |
template<class O1, class O2> |
139 |
check_watcher (O1 *object, void (O2::*method)(check_watcher &)) |
140 |
: callback1<void, check_watcher &>(object,method) |
141 |
{ } |
142 |
|
143 |
~check_watcher (); |
144 |
|
145 |
void start () { iom.reg (this); } |
146 |
void stop () { iom.unreg (this); } |
147 |
}; |
148 |
#endif |
149 |
|
150 |
#endif |
151 |
|