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 <cassert> |
25 |
|
26 |
#include <sys/poll.h> |
27 |
|
28 |
#include "callback.h" |
29 |
#include "slog.h" |
30 |
|
31 |
typedef double tstamp; |
32 |
|
33 |
extern tstamp NOW; |
34 |
|
35 |
struct io_watcher; |
36 |
struct time_watcher; |
37 |
|
38 |
class io_manager { |
39 |
vector<pollfd> pfs; |
40 |
vector<io_watcher *> iow; |
41 |
vector<time_watcher *> tw; // actually a heap |
42 |
|
43 |
void idle_cb (time_watcher &w); time_watcher *idle; |
44 |
public: |
45 |
|
46 |
void reschedule_time_watchers (); |
47 |
|
48 |
// register a watcher |
49 |
void reg (io_watcher *w); |
50 |
void unreg (io_watcher *w); |
51 |
void reg (time_watcher *w); |
52 |
void unreg (time_watcher *w); |
53 |
|
54 |
void loop (); |
55 |
|
56 |
io_manager (); |
57 |
~io_manager (); |
58 |
}; |
59 |
|
60 |
extern io_manager iom; // a singleton, together with it's construction/destruction problems. |
61 |
|
62 |
struct io_watcher : callback2<void, io_watcher &, short> { |
63 |
pollfd *p; |
64 |
|
65 |
template<class O1, class O2> |
66 |
io_watcher (O1 *object, void (O2::*method)(io_watcher &, short)) |
67 |
: callback2<void, io_watcher &, short>(object,method) |
68 |
{ } |
69 |
|
70 |
~io_watcher (); |
71 |
|
72 |
void set(int fd, short events) |
73 |
{ |
74 |
assert (p); |
75 |
p->fd = fd; |
76 |
p->events = events; |
77 |
} |
78 |
|
79 |
void set(short events) |
80 |
{ |
81 |
assert (p); |
82 |
p->events = events; |
83 |
} |
84 |
|
85 |
void start (int fd, short events) |
86 |
{ |
87 |
iom.reg (this); // make sure pfd is set |
88 |
|
89 |
p->fd = fd; |
90 |
p->events = events; |
91 |
} |
92 |
|
93 |
void stop () |
94 |
{ |
95 |
iom.unreg (this); |
96 |
} |
97 |
}; |
98 |
|
99 |
#define TSTAMP_CANCEL -1. |
100 |
|
101 |
struct time_watcher : callback1<void, time_watcher &> { |
102 |
bool registered; // already registered? |
103 |
tstamp at; |
104 |
|
105 |
template<class O1, class O2> |
106 |
time_watcher (O1 *object, void (O2::*method)(time_watcher &)) |
107 |
: callback1<void, time_watcher &>(object,method) |
108 |
, registered(false) |
109 |
{ } |
110 |
|
111 |
~time_watcher (); |
112 |
|
113 |
void set (tstamp when); |
114 |
void trigger (); |
115 |
|
116 |
void operator ()() |
117 |
{ |
118 |
trigger (); |
119 |
} |
120 |
|
121 |
void start (); |
122 |
void start (tstamp when) |
123 |
{ |
124 |
set (when); |
125 |
} |
126 |
|
127 |
void stop () |
128 |
{ |
129 |
iom.unreg (this); |
130 |
} |
131 |
|
132 |
void reset (tstamp when = TSTAMP_CANCEL) |
133 |
{ |
134 |
stop (); |
135 |
|
136 |
at = when; |
137 |
} |
138 |
}; |
139 |
|
140 |
#endif |
141 |
|