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

Comparing gvpe/src/iom.h (file contents):
Revision 1.15 by pcg, Sat Jan 17 01:18:36 2004 UTC vs.
Revision 1.23 by pcg, Fri Nov 12 02:53:29 2004 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines