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

File Contents

# User Rev Content
1 pcg 1.1 /*
2     iom.C -- 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     #include "../config.h"
21    
22     #include <cstdio>
23 pcg 1.8 #include <cstdlib>
24     #include <cerrno>
25 pcg 1.1
26     #include <sys/select.h>
27     #include <sys/time.h>
28    
29     #include "iom.h"
30    
31 pcg 1.8 // TSTAMP_MAX must still fit into a positive struct timeval
32     #define TSTAMP_MAX (double)(1UL<<31)
33    
34 pcg 1.1 tstamp NOW;
35 pcg 1.8 static bool iom_valid;
36 pcg 1.1 io_manager iom;
37    
38 pcg 1.4 template<class watcher>
39     void io_manager::reg (watcher *w, simplevec<watcher *> &queue)
40     {
41 pcg 1.8 if (!iom_valid)
42     abort ();
43    
44     if (!w->active)
45     {
46     queue.push_back (w);
47     w->active = queue.size ();
48     }
49 pcg 1.4 }
50    
51     template<class watcher>
52     void io_manager::unreg (watcher *w, simplevec<watcher *> &queue)
53     {
54 pcg 1.8 if (!iom_valid)
55     return;
56 pcg 1.4
57 pcg 1.8 if (w->active)
58     {
59     queue [w->active - 1] = 0;
60     w->active = 0;
61     }
62 pcg 1.4 }
63    
64     #if IOM_TIME
65 pcg 1.1 void time_watcher::trigger ()
66     {
67     call (*this);
68    
69     iom.reg (this);
70     }
71    
72 pcg 1.8 void io_manager::reg (time_watcher *w) { reg (w, tw); }
73     void io_manager::unreg (time_watcher *w) { unreg (w, tw); }
74     #endif
75 pcg 1.3
76 pcg 1.8 #if IOM_IO
77     void io_manager::reg (io_watcher *w) { reg (w, iow); }
78     void io_manager::unreg (io_watcher *w) { unreg (w, iow); }
79 pcg 1.4 #endif
80 pcg 1.1
81 pcg 1.4 #if IOM_CHECK
82 pcg 1.8 void io_manager::reg (check_watcher *w) { reg (w, cw); }
83     void io_manager::unreg (check_watcher *w) { unreg (w, cw); }
84 pcg 1.4 #endif
85 pcg 1.1
86 pcg 1.7 #if IOM_IDLE
87 pcg 1.8 void io_manager::reg (idle_watcher *w) { reg (w, iw); }
88     void io_manager::unreg (idle_watcher *w) { unreg (w, iw); }
89 pcg 1.7 #endif
90    
91 pcg 1.4 #if IOM_TIME
92 pcg 1.1 inline void set_now (void)
93     {
94     struct timeval tv;
95    
96     gettimeofday (&tv, 0);
97    
98     NOW = (tstamp)tv.tv_sec + (tstamp)tv.tv_usec / 1000000;
99 pcg 1.4 #endif
100 pcg 1.1 }
101    
102     void io_manager::loop ()
103     {
104 pcg 1.4 #if IOM_TIME
105 pcg 1.1 set_now ();
106 pcg 1.4 #endif
107 pcg 1.1
108     for (;;)
109     {
110 pcg 1.5 struct timeval *to = 0;
111     struct timeval tval;
112 pcg 1.1
113 pcg 1.7 #if IOM_IDLE
114     if (iw.size ())
115     {
116     tval.tv_sec = 0;
117     tval.tv_usec = 0;
118     to = &tval;
119     }
120     else
121     #endif
122 pcg 1.1 {
123 pcg 1.7 #if IOM_TIME
124 pcg 1.8 time_watcher *next;
125 pcg 1.1
126 pcg 1.8 for (;;)
127 pcg 1.7 {
128 pcg 1.8 next = tw[0]; // the first time-watcher must exist at ALL times
129 pcg 1.1
130 pcg 1.8 for (int i = tw.size (); i--; )
131     if (!tw[i])
132     tw.erase_unordered (i);
133     else if (tw[i]->at < next->at)
134     next = tw[i];
135 pcg 1.7
136 pcg 1.8 if (next->at > NOW)
137 pcg 1.7 {
138 pcg 1.8 if (next != tw[0])
139     {
140     double diff = next->at - NOW;
141     tval.tv_sec = (int)diff;
142     tval.tv_usec = (int)((diff - tval.tv_sec) * 1000000);
143     to = &tval;
144     }
145 pcg 1.7 break;
146     }
147 pcg 1.8 else if (next->at >= 0)
148     {
149     unreg (next);
150     next->call (*next);
151     }
152 pcg 1.5 }
153 pcg 1.7 #endif
154 pcg 1.1 }
155 pcg 1.6
156     #if IOM_CHECK
157 pcg 1.8 for (int i = cw.size (); i--; )
158     if (!cw[i])
159     cw.erase_unordered (i);
160     else
161     cw[i]->call (*cw[i]);
162 pcg 1.4 #endif
163 pcg 1.1
164 pcg 1.4 #if IOM_IO
165 pcg 1.8 fd_set rfd, wfd, efd;
166 pcg 1.1
167     FD_ZERO (&rfd);
168     FD_ZERO (&wfd);
169    
170     int fds = 0;
171    
172 pcg 1.8 for (io_watcher **i = iow.end (); i-- > iow.begin (); )
173     if (*i)
174     {
175     if ((*i)->events & EVENT_READ ) FD_SET ((*i)->fd, &rfd);
176     if ((*i)->events & EVENT_WRITE) FD_SET ((*i)->fd, &wfd);
177 pcg 1.1
178 pcg 1.8 if ((*i)->fd >= fds) fds = (*i)->fd + 1;
179     }
180 pcg 1.1
181 pcg 1.8 if (!to && !fds) //TODO: also check idle_watchers and check_watchers
182 pcg 1.5 break; // no events
183    
184 pcg 1.8 fds = select (fds, &rfd, &wfd, &efd, to);
185 pcg 1.4 # if IOM_TIME
186 pcg 1.1 set_now ();
187 pcg 1.4 # endif
188 pcg 1.1
189     if (fds > 0)
190 pcg 1.8 for (int i = iow.size (); i--; )
191     if (!iow[i])
192     iow.erase_unordered (i);
193     else
194     {
195     short revents = iow[i]->events;
196 pcg 1.1
197 pcg 1.8 if (!FD_ISSET (iow[i]->fd, &rfd)) revents &= ~EVENT_READ;
198     if (!FD_ISSET (iow[i]->fd, &wfd)) revents &= ~EVENT_WRITE;
199 pcg 1.4
200 pcg 1.8 if (revents)
201     iow[i]->call (*iow[i], revents);
202     }
203     else if (fds < 0 && errno != EINTR)
204     {
205     perror ("Error while waiting for I/O or time event");
206     abort ();
207     }
208 pcg 1.7 #if IOM_IDLE
209 pcg 1.8 else
210     for (int i = iw.size (); i--; )
211     if (!iw[i])
212     iw.erase_unordered (i);
213     else
214     iw[i]->call (*iw[i]);
215 pcg 1.7 #endif
216    
217 pcg 1.4 #elif IOM_TIME
218 pcg 1.5 if (!to)
219     break;
220    
221 pcg 1.4 select (0, 0, 0, 0, &to);
222     set_now ();
223 pcg 1.5 #else
224     break;
225 pcg 1.4 #endif
226 pcg 1.5 }
227 pcg 1.1 }
228    
229 pcg 1.8 // this is a dummy time watcher to ensure that the first
230     // time watcher is _always_ valid, this gets rid of a lot
231     // of null-pointer-checks
232     static struct tw0 : time_watcher {
233     void cb (time_watcher &w)
234     {
235     // should never get called
236     // reached end-of-time, or tstamp has a bogus definition :)
237     abort ();
238     }
239    
240     tw0()
241     : time_watcher (this, &tw0::cb)
242     { }
243     } tw0;
244    
245 pcg 1.1 io_manager::io_manager ()
246     {
247 pcg 1.8 iom_valid = true;
248    
249 pcg 1.5 #if IOM_TIME
250 pcg 1.1 set_now ();
251 pcg 1.8
252     tw0.start (TSTAMP_MAX);
253 pcg 1.5 #endif
254 pcg 1.1 }
255    
256     io_manager::~io_manager ()
257     {
258     iom_valid = false;
259     }
260