ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/gvpe/src/iom.h
Revision: 1.18
Committed: Sun Mar 21 12:50:05 2004 UTC (20 years, 2 months ago) by pcg
Content type: text/plain
Branch: MAIN
Changes since 1.17: +1 -3 lines
Log Message:
*** empty log message ***

File Contents

# User Rev Content
1 pcg 1.1 /*
2 pcg 1.14 iom.h -- generic I/O multiplexor
3 pcg 1.15 Copyright (C) 2003, 2004 Marc Lehmann <pcg@goof.com>
4 pcg 1.1
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 pcg 1.15 #ifndef IOM_H__
21     #define IOM_H__
22 pcg 1.1
23 pcg 1.15 // required:
24     // - a vector template like simplevec or stl's vector
25     // - defines for all watcher types required in your app
26     // edit iom_conf.h as appropriate.
27     #include "iom_conf.h"
28 pcg 1.1
29 pcg 1.15 #include "callback.h"
30 pcg 1.1
31 pcg 1.15 #ifndef IOM_IO
32     # define IOM_IO 0
33     #endif
34     #ifndef IOM_TIME
35     # define IOM_TIME 0
36     #endif
37     #ifndef IOM_CHECK
38     # define IOM_CHECK 0
39     #endif
40     #ifndef IOM_IDLE
41     # define IOM_IDLE 0
42     #endif
43 pcg 1.1
44 pcg 1.2 typedef double tstamp;
45     extern tstamp NOW;
46 pcg 1.1
47 pcg 1.15 struct watcher;
48     #if IOM_IO
49 pcg 1.1 struct io_watcher;
50 pcg 1.15 #endif
51     #if IOM_TIME
52 pcg 1.1 struct time_watcher;
53 pcg 1.15 #endif
54     #if IOM_CHECK
55     struct check_watcher;
56     #endif
57     #if IOM_IDLE
58     struct idle_watcher;
59     #endif
60    
61     template<class watcher>
62 pcg 1.18 struct io_manager_vec : vector<watcher *> {
63 pcg 1.17 #if IOM_CHECK
64     bool activity;
65     #endif
66    
67 pcg 1.15 void erase_unordered (unsigned int pos)
68     {
69     watcher *w = (*this)[size () - 1];
70     pop_back ();
71    
72     if (size ())
73     if ((*this)[pos] = w)
74     w->active = pos + 1;
75     }
76     };
77 pcg 1.1
78     class io_manager {
79 pcg 1.15 #if IOM_IO
80     io_manager_vec<io_watcher> iow;
81     #endif
82     #if IOM_CHECK
83     io_manager_vec<check_watcher> cw;
84     #endif
85     #if IOM_TIME
86     io_manager_vec<time_watcher> tw;
87     #endif
88     #if IOM_IDLE
89     io_manager_vec<idle_watcher> iw;
90     #endif
91 pcg 1.4
92 pcg 1.15 template<class watcher>
93     void reg (watcher *w, io_manager_vec<watcher> &queue);
94 pcg 1.1
95 pcg 1.15 template<class watcher>
96     void unreg (watcher *w, io_manager_vec<watcher> &queue);
97 pcg 1.6
98 pcg 1.15 public:
99 pcg 1.1 // register a watcher
100 pcg 1.15 #if IOM_IO
101     void reg (io_watcher *w); void unreg (io_watcher *w);
102     #endif
103     #if IOM_TIME
104     void reg (time_watcher *w); void unreg (time_watcher *w);
105     #endif
106     #if IOM_CHECK
107     void reg (check_watcher *w); void unreg (check_watcher *w);
108     #endif
109     #if IOM_IDLE
110     void reg (idle_watcher *w); void unreg (idle_watcher *w);
111     #endif
112 pcg 1.1
113     void loop ();
114    
115     io_manager ();
116     ~io_manager ();
117     };
118    
119 pcg 1.11 extern io_manager iom; // a singleton, together with it's construction/destruction problems.
120 pcg 1.1
121 pcg 1.15 struct watcher {
122     int active; /* 0 == inactive, else index into respective vector */
123    
124     watcher() : active(0) { }
125     };
126    
127     #if IOM_IO
128     enum { EVENT_READ = 1, EVENT_WRITE = 2 };
129    
130     struct io_watcher : watcher, callback2<void, io_watcher &, short> {
131 pcg 1.12 int fd;
132     short events;
133 pcg 1.10
134 pcg 1.15 void set (int fd_, short events_) { fd = fd_; events = events_; }
135    
136     void set (short events_) { set (fd, events_); }
137     void start () { iom.reg (this); }
138     void start (int fd_, short events_) { set (fd_, events_); iom.reg (this); }
139     void stop () { iom.unreg (this); }
140    
141 pcg 1.1 template<class O1, class O2>
142 pcg 1.10 io_watcher (O1 *object, void (O2::*method)(io_watcher &, short))
143 pcg 1.15 : callback2<void, io_watcher &, short>(object,method)
144     { }
145     ~io_watcher () { stop (); }
146     };
147     #endif
148    
149     #if IOM_TIME
150     struct time_watcher : watcher, callback1<void, time_watcher &> {
151     tstamp at;
152 pcg 1.2
153 pcg 1.15 void trigger ();
154 pcg 1.11
155 pcg 1.15 void set (tstamp when) { at = when; }
156     void operator ()() { trigger (); }
157     void start () { iom.reg (this); }
158     void start (tstamp when) { set (when); iom.reg (this); }
159     void stop () { iom.unreg (this); }
160 pcg 1.6
161 pcg 1.15 template<class O1, class O2>
162     time_watcher (O1 *object, void (O2::*method)(time_watcher &))
163 pcg 1.16 : callback1<void, time_watcher &>(object,method), at(0)
164 pcg 1.15 { }
165     ~time_watcher () { stop (); }
166     };
167     #endif
168 pcg 1.11
169 pcg 1.15 #if IOM_CHECK
170     // run before checking for new events
171     struct check_watcher : watcher, callback1<void, check_watcher &> {
172     void start () { iom.reg (this); }
173     void stop () { iom.unreg (this); }
174 pcg 1.1
175 pcg 1.15 template<class O1, class O2>
176     check_watcher (O1 *object, void (O2::*method)(check_watcher &))
177     : callback1<void, check_watcher &>(object,method)
178     { }
179     ~check_watcher () { stop (); }
180 pcg 1.1 };
181 pcg 1.15 #endif
182 pcg 1.1
183 pcg 1.15 #if IOM_IDLE
184     // run after checking for any i/o, but before waiting
185     struct idle_watcher : watcher, callback1<void, idle_watcher &> {
186     void start () { iom.reg (this); }
187     void stop () { iom.unreg (this); }
188 pcg 1.2
189     template<class O1, class O2>
190 pcg 1.15 idle_watcher (O1 *object, void (O2::*method)(idle_watcher &))
191     : callback1<void, idle_watcher &>(object,method)
192 pcg 1.2 { }
193 pcg 1.15 ~idle_watcher () { stop (); }
194 pcg 1.1 };
195 pcg 1.15 #endif
196 pcg 1.1
197     #endif
198