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