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 "callback.h" |
27 |
#include "slog.h" |
28 |
|
29 |
typedef double tstamp; |
30 |
|
31 |
extern tstamp NOW; |
32 |
|
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 |
struct io_watcher : callback<void, short> { |
61 |
template<class O1, class O2> |
62 |
io_watcher (O1 *object, void (O2::*method)(short revents)) |
63 |
: callback<void, short>(object,method) |
64 |
{ } |
65 |
|
66 |
~io_watcher () |
67 |
{ |
68 |
iom.unreg (this); |
69 |
} |
70 |
|
71 |
void start (int fd, short events) |
72 |
{ |
73 |
iom.reg (fd, events, this); |
74 |
} |
75 |
|
76 |
void stop () |
77 |
{ |
78 |
iom.unreg (this); |
79 |
} |
80 |
}; |
81 |
|
82 |
#define TSTAMP_CANCEL -1. |
83 |
|
84 |
struct time_watcher : callback<void, tstamp &> { |
85 |
bool registered; // already registered? |
86 |
tstamp at; |
87 |
|
88 |
template<class O1, class O2> |
89 |
time_watcher (O1 *object, void (O2::*method)(tstamp &)) |
90 |
: callback<void, tstamp &>(object,method) |
91 |
, registered(false) |
92 |
{ } |
93 |
|
94 |
~time_watcher () |
95 |
{ |
96 |
iom.unreg (this); |
97 |
} |
98 |
|
99 |
void set (tstamp when); |
100 |
void trigger (); |
101 |
|
102 |
void operator ()() |
103 |
{ |
104 |
trigger (); |
105 |
} |
106 |
|
107 |
void start (); |
108 |
void start (tstamp when) |
109 |
{ |
110 |
set (when); |
111 |
} |
112 |
|
113 |
void stop () |
114 |
{ |
115 |
iom.unreg (this); |
116 |
} |
117 |
|
118 |
void reset (tstamp when = TSTAMP_CANCEL) |
119 |
{ |
120 |
stop (); |
121 |
at = when; |
122 |
} |
123 |
}; |
124 |
|
125 |
#endif |
126 |
|