ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/rxvt-unicode/src/iom.h
Revision: 1.6
Committed: Fri Jan 16 22:11:09 2004 UTC (20 years, 4 months ago) by pcg
Content type: text/plain
Branch: MAIN
Changes since 1.5: +66 -49 lines
Log Message:
*** empty log message ***

File Contents

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