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 |
|
|
typedef double tstamp; |
29 |
|
|
|
30 |
|
|
extern tstamp NOW; |
31 |
|
|
|
32 |
|
|
struct io_watcher; |
33 |
|
|
struct time_watcher; |
34 |
|
|
|
35 |
|
|
class io_manager { |
36 |
|
|
simplevec<io_watcher *> iow; |
37 |
|
|
simplevec<time_watcher *> tw; // actually a heap |
38 |
|
|
|
39 |
|
|
void idle_cb (time_watcher &w); time_watcher *idle; |
40 |
|
|
public: |
41 |
|
|
// register a watcher |
42 |
|
|
void reg (io_watcher *w); |
43 |
|
|
void unreg (io_watcher *w); |
44 |
|
|
void reg (time_watcher *w); |
45 |
|
|
void unreg (time_watcher *w); |
46 |
|
|
|
47 |
|
|
void loop (); |
48 |
|
|
|
49 |
|
|
io_manager (); |
50 |
|
|
~io_manager (); |
51 |
|
|
}; |
52 |
|
|
|
53 |
|
|
extern io_manager iom; // a singleton, together with it's construction/destruction problems. |
54 |
|
|
|
55 |
|
|
enum { EVENT_READ = 1, EVENT_WRITE = 2 }; |
56 |
|
|
|
57 |
|
|
struct io_watcher : callback2<void, io_watcher &, short> { |
58 |
|
|
int fd; |
59 |
|
|
short events; |
60 |
|
|
|
61 |
|
|
template<class O1, class O2> |
62 |
|
|
io_watcher (O1 *object, void (O2::*method)(io_watcher &, short)) |
63 |
|
|
: callback2<void, io_watcher &, short>(object,method) |
64 |
|
|
{ } |
65 |
|
|
|
66 |
|
|
~io_watcher (); |
67 |
|
|
|
68 |
|
|
void set(int fd_, short events_) { fd = fd_; events = events_; } |
69 |
|
|
|
70 |
|
|
void set(short events_) { set (fd, events_); } |
71 |
|
|
void start (int fd_, short events_) { set (fd_, events_); iom.reg (this); } |
72 |
|
|
void stop () { iom.unreg (this); } |
73 |
|
|
}; |
74 |
|
|
|
75 |
|
|
#define TSTAMP_CANCEL -1. |
76 |
|
|
|
77 |
|
|
struct time_watcher : callback1<void, time_watcher &> { |
78 |
|
|
tstamp at; |
79 |
|
|
|
80 |
|
|
template<class O1, class O2> |
81 |
|
|
time_watcher (O1 *object, void (O2::*method)(time_watcher &)) |
82 |
|
|
: callback1<void, time_watcher &>(object,method) |
83 |
|
|
{ } |
84 |
|
|
|
85 |
|
|
~time_watcher (); |
86 |
|
|
|
87 |
|
|
void trigger (); |
88 |
|
|
|
89 |
|
|
void set (tstamp when) { at = when; } |
90 |
|
|
void operator ()() { trigger (); } |
91 |
|
|
void start () { iom.reg (this); } |
92 |
|
|
void start (tstamp when) { set (when); iom.reg (this); } |
93 |
|
|
void stop () { iom.unreg (this); } |
94 |
|
|
|
95 |
|
|
void reset (tstamp when = TSTAMP_CANCEL) |
96 |
|
|
{ |
97 |
|
|
stop (); |
98 |
|
|
at = when; |
99 |
|
|
} |
100 |
|
|
}; |
101 |
|
|
|
102 |
|
|
#endif |
103 |
|
|
|