ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/gvpe/src/iom.h
Revision: 1.12
Committed: Sat Apr 5 02:32:40 2003 UTC (21 years, 1 month ago) by pcg
Content type: text/plain
Branch: MAIN
CVS Tags: VPE_0_9, VPE_1_0
Changes since 1.11: +16 -17 lines
Log Message:
*** empty 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 <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 bool registered; // already registered?
64 int fd;
65 short events;
66
67 template<class O1, class O2>
68 io_watcher (O1 *object, void (O2::*method)(io_watcher &, short))
69 : callback2<void, io_watcher &, short>(object,method)
70 , registered(false)
71 { }
72
73 ~io_watcher ();
74
75 void set(int fd_, short events_);
76
77 void set(short events_)
78 {
79 set (fd, events_);
80 }
81
82 void start (int fd_, short events_)
83 {
84 set (fd_, events_);
85 iom.reg (this);
86 }
87
88 void stop ()
89 {
90 iom.unreg (this);
91 }
92 };
93
94 #define TSTAMP_CANCEL -1.
95
96 struct time_watcher : callback1<void, time_watcher &> {
97 bool registered; // already registered?
98 tstamp at;
99
100 template<class O1, class O2>
101 time_watcher (O1 *object, void (O2::*method)(time_watcher &))
102 : callback1<void, time_watcher &>(object,method)
103 , registered(false)
104 { }
105
106 ~time_watcher ();
107
108 void set (tstamp when);
109 void trigger ();
110
111 void operator ()()
112 {
113 trigger ();
114 }
115
116 void start ()
117 {
118 iom.reg (this);
119 }
120
121 void start (tstamp when)
122 {
123 set (when);
124 iom.reg (this);
125 }
126
127 void stop ()
128 {
129 iom.unreg (this);
130 }
131
132 void reset (tstamp when = TSTAMP_CANCEL)
133 {
134 stop ();
135 at = when;
136 }
137 };
138
139 #endif
140