ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/rxvt-unicode/src/iom.h
(Generate patch)

Comparing rxvt-unicode/src/iom.h (file contents):
Revision 1.1 by pcg, Mon Nov 24 17:28:08 2003 UTC vs.
Revision 1.6 by pcg, Fri Jan 16 22:11:09 2004 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines