ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/gvpe/src/iom.h
Revision: 1.10
Committed: Wed Apr 2 21:02:25 2003 UTC (21 years, 1 month ago) by pcg
Content type: text/plain
Branch: MAIN
Changes since 1.9: +16 -11 lines
Log Message:
§§

File Contents

# Content
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<io_watcher *> iow;
39 vector<time_watcher *> tw; // actually a heap
40
41 void idle_cb (time_watcher &w); time_watcher *idle;
42 public:
43
44 void reschedule_time_watchers ();
45
46 // register a watcher
47 void reg (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 : callback2<void, io_watcher &, short> {
61 int fd;
62 short events;
63
64 template<class O1, class O2>
65 io_watcher (O1 *object, void (O2::*method)(io_watcher &, short))
66 : callback2<void, io_watcher &, short>(object,method)
67 { }
68
69 ~io_watcher ()
70 {
71 iom.unreg (this);
72 }
73
74 void start (int fd_, short events_)
75 {
76 fd = fd_;
77 events = events_;
78 iom.reg (this);
79 }
80
81 void stop ()
82 {
83 iom.unreg (this);
84 }
85 };
86
87 #define TSTAMP_CANCEL -1.
88
89 struct time_watcher : callback1<void, time_watcher &> {
90 bool registered; // already registered?
91 tstamp at;
92
93 template<class O1, class O2>
94 time_watcher (O1 *object, void (O2::*method)(time_watcher &))
95 : callback1<void, time_watcher &>(object,method)
96 , registered(false)
97 { }
98
99 ~time_watcher ()
100 {
101 iom.unreg (this);
102 }
103
104 void set (tstamp when);
105 void trigger ();
106
107 void operator ()()
108 {
109 trigger ();
110 }
111
112 void start ();
113 void start (tstamp when)
114 {
115 set (when);
116 }
117
118 void stop ()
119 {
120 iom.unreg (this);
121 }
122
123 void reset (tstamp when = TSTAMP_CANCEL)
124 {
125 stop ();
126 at = when;
127 }
128 };
129
130 #endif
131