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

Comparing rxvt-unicode/src/iom.C (file contents):
Revision 1.1 by pcg, Mon Nov 24 17:28:08 2003 UTC vs.
Revision 1.5 by pcg, Sat Nov 29 18:42:07 2003 UTC

28 28
29tstamp NOW; 29tstamp NOW;
30bool iom_valid; 30bool iom_valid;
31io_manager iom; 31io_manager iom;
32 32
33void time_watcher::trigger () 33template<class watcher>
34void io_manager::reg (watcher *w, simplevec<watcher *> &queue)
34{ 35{
35 call (*this); 36 if (find (queue.begin (), queue.end (), w) == queue.end ())
36 37 queue.push_back (w);
37 iom.reg (this);
38} 38}
39 39
40template<class watcher>
41void io_manager::unreg (watcher *w, simplevec<watcher *> &queue)
42{
43 queue.erase (find (queue.begin (), queue.end (), w));
44}
45
46#if IOM_IO
40time_watcher::~time_watcher () 47io_watcher::~io_watcher ()
41{ 48{
42 if (iom_valid) 49 if (iom_valid)
43 iom.unreg (this); 50 iom.unreg (this);
44} 51}
45 52
53void io_manager::reg (io_watcher *w)
54{
55 reg (w, iow);
56}
57
58void io_manager::unreg (io_watcher *w)
59{
60 unreg (w, iow);
61}
62
63#endif
64
65#if IOM_TIME
66void time_watcher::trigger ()
67{
68 call (*this);
69
70 iom.reg (this);
71}
72
46io_watcher::~io_watcher () 73time_watcher::~time_watcher ()
47{ 74{
48 if (iom_valid) 75 if (iom_valid)
49 iom.unreg (this); 76 iom.unreg (this);
50}
51 77
52void io_manager::reg (io_watcher *w) 78 at = TSTAMP_CANCEL;
53{
54 if (find (iow.begin (), iow.end (), w) == iow.end ())
55 iow.push_back (w);
56}
57
58void io_manager::unreg (io_watcher *w)
59{
60 iow.erase (find (iow.begin (), iow.end (), w));
61} 79}
62 80
63void io_manager::reg (time_watcher *w) 81void io_manager::reg (time_watcher *w)
64{ 82{
65 if (find (tw.begin (), tw.end (), w) == tw.end ()) 83 reg (w, tw);
66 tw.push_back (w);
67} 84}
68 85
69void io_manager::unreg (time_watcher *w) 86void io_manager::unreg (time_watcher *w)
70{ 87{
71 tw.erase (find (tw.begin (), tw.end (), w)); 88 unreg (w, tw);
72} 89}
90#endif
73 91
92#if IOM_CHECK
93check_watcher::~check_watcher ()
94{
95 if (iom_valid)
96 iom.unreg (this);
97}
98
99void io_manager::reg (check_watcher *w)
100{
101 reg (w, cw);
102}
103
104void io_manager::unreg (check_watcher *w)
105{
106 unreg (w, cw);
107}
108#endif
109
110#if IOM_TIME
74inline void set_now (void) 111inline void set_now (void)
75{ 112{
76 struct timeval tv; 113 struct timeval tv;
77 114
78 gettimeofday (&tv, 0); 115 gettimeofday (&tv, 0);
79 116
80 NOW = (tstamp)tv.tv_sec + (tstamp)tv.tv_usec / 1000000; 117 NOW = (tstamp)tv.tv_sec + (tstamp)tv.tv_usec / 1000000;
118#endif
81} 119}
82 120
83void io_manager::loop () 121void io_manager::loop ()
84{ 122{
123#if IOM_TIME
85 set_now (); 124 set_now ();
125#endif
86 126
87 for (;;) 127 for (;;)
88 { 128 {
129#if IOM_CHECK
130 for (int i = 0; i < cw.size (); ++i)
131 cw[i]->call (*cw[i]);
132#endif
133
134 struct timeval *to = 0;
135
136#if IOM_TIME
137 struct timeval tval;
89 time_watcher *w; 138 time_watcher *w;
90 139
91 for (;;) 140 for (;tw.size ();)
92 { 141 {
93 w = tw[0]; 142 w = tw[0];
94 143
95 for (time_watcher **i = tw.begin (); i != tw.end (); ++i) 144 for (time_watcher **i = tw.begin (); i < tw.end (); ++i)
96 if ((*i)->at < w->at) 145 if ((*i)->at < w->at)
97 w = *i; 146 w = *i;
98 147
99 if (w->at > NOW) 148 if (w->at > NOW)
149 {
150 double diff = w->at - NOW;
151 tval.tv_sec = (int)diff;
152 tval.tv_usec = (int)((diff - tval.tv_sec) * 1000000);
153 to = &tval;
100 break; 154 break;
101 155 }
102 // call it 156 else if (w->at >= 0)
103 w->call (*w); 157 w->call (*w);
104 158 else
105 // re-add it if necessary
106 if (w->at >= 0)
107 reg (w); 159 unreg (w);
108 } 160 }
161#endif
109 162
110 struct timeval to; 163#if IOM_IO
111 double diff = w->at - NOW;
112 to.tv_sec = (int)diff;
113 to.tv_usec = (int)((diff - to.tv_sec) * 1000000);
114
115 fd_set rfd, wfd; 164 fd_set rfd, wfd;
116 165
117 FD_ZERO (&rfd); 166 FD_ZERO (&rfd);
118 FD_ZERO (&wfd); 167 FD_ZERO (&wfd);
119 168
120 int fds = 0; 169 int fds = 0;
121 170
122 for (io_watcher **w = iow.begin (); w < iow.end (); ++w) 171 for (io_watcher **w = iow.begin (); w < iow.end (); ++w)
123 { 172 {
124 if ((*w)->events & EVENT_READ ) FD_SET ((*w)->fd, &rfd); 173 if ((*w)->events & EVENT_READ ) FD_SET ((*w)->fd, &rfd);
125 if ((*w)->events & EVENT_WRITE) FD_SET ((*w)->fd, &wfd); 174 if ((*w)->events & EVENT_WRITE) FD_SET ((*w)->fd, &wfd);
126 175
127 if ((*w)->fd > fds) fds = (*w)->fd; 176 if ((*w)->fd >= fds) fds = (*w)->fd + 1;
128 } 177 }
129 178
179 if (!to && !fds)
180 break; // no events
181
130 fds = select (fds + 1, &rfd, &wfd, 0, &to); 182 fds = select (fds, &rfd, &wfd, 0, to);
131 183# if IOM_TIME
132 set_now (); 184 set_now ();
185# endif
133 186
134 if (fds > 0) 187 if (fds > 0)
135 for (io_watcher **w = iow.begin (); w < iow.end (); w++) 188 for (int i = 0; i < iow.size (); ++i)
136 { 189 {
190 io_watcher *w = iow[i];
191
137 short revents = (*w)->events; 192 short revents = w->events;
138 193
139 if (!FD_ISSET ((*w)->fd, &rfd)) revents &= ~EVENT_READ; 194 if (!FD_ISSET (w->fd, &rfd)) revents &= ~EVENT_READ;
140 if (!FD_ISSET ((*w)->fd, &wfd)) revents &= ~EVENT_WRITE; 195 if (!FD_ISSET (w->fd, &wfd)) revents &= ~EVENT_WRITE;
141 196
142 if (revents) 197 if (revents)
143 (*w)->call (**w, revents); 198 w->call (*w, revents);
144 } 199 }
200#elif IOM_TIME
201 if (!to)
202 break;
203
204 select (0, 0, 0, 0, &to);
205 set_now ();
206#else
207 break;
208#endif
145 } 209 }
146} 210}
147 211
148void io_manager::idle_cb (time_watcher &w)
149{
150 w.at = NOW + 1000000000;
151}
152
153io_manager::io_manager () 212io_manager::io_manager ()
154{ 213{
214#if IOM_TIME
155 set_now (); 215 set_now ();
216#endif
156 217
157 iom_valid = true; 218 iom_valid = true;
158
159 idle = new time_watcher (this, &io_manager::idle_cb);
160 idle->start (0);
161} 219}
162 220
163io_manager::~io_manager () 221io_manager::~io_manager ()
164{ 222{
165 iom_valid = false; 223 iom_valid = false;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines