*** empty log message ***
[dana/urxvt.git] / src / iom.C
1 /*
2     iom.C -- generic I/O multiplexer
3     Copyright (C) 2003, 2004 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 "iom.h"
21
22 #include <cstdio>
23 #include <cstdlib>
24 #include <cerrno>
25
26 #include <sys/time.h>
27
28 #include <assert.h>
29
30 #if 1 // older unices need these includes for select (2)
31 # include <unistd.h>
32 # include <sys/types.h>
33 # include <time.h>
34 #endif
35
36 // for IOM_SIG
37 #if IOM_SIG
38 # include <signal.h>
39 # include <fcntl.h>
40 #endif
41
42 // if the BSDs would at least be marginally POSIX-compatible.. *sigh*
43 // until that happens, sys/select.h must come last
44 #include <sys/select.h>
45
46 // TSTAMP_MAX must still fit into a positive struct timeval
47 #define TSTAMP_MAX (double)(1UL<<31)
48
49 #define TIMEVAL timeval
50 #define TV_FRAC tv_usec
51 #define TV_MULT 1000000L
52
53 #if IOM_IO
54 static io_manager_vec<io_watcher>    iow;
55 #endif
56 #if IOM_CHECK
57 static io_manager_vec<check_watcher> cw;
58 #endif
59 #if IOM_TIME
60 static io_manager_vec<time_watcher>  tw;
61 #endif
62 #if IOM_IDLE
63 static io_manager_vec<idle_watcher>  iw;
64 #endif
65 #if IOM_SIG
66 static int sigpipe[2]; // signal signalling pipe
67 static sigset_t sigs;
68 struct sig_vec : io_manager_vec<sig_watcher> {
69   int pending;
70   sig_vec ()
71     : pending (false)
72     { }
73 };
74 static vector<sig_vec *> sw;
75 #endif
76
77 // this is a dummy time watcher to ensure that the first
78 // time watcher is _always_ valid, this gets rid of a lot
79 // of null-pointer-checks
80 // (must come _before_ iom is being defined)
81 static struct tw0 : time_watcher
82   {
83     void cb (time_watcher &w)
84     {
85       // should never get called
86       // reached end-of-time, or tstamp has a bogus definition,
87       // or compiler initialisation order broken, or something else :)
88       abort ();
89     }
90
91     tw0 ()
92       : time_watcher (this, &tw0::cb)
93       { }
94   } tw0;
95
96 tstamp NOW;
97
98 #if IOM_TIME
99 inline void set_now (void)
100 {
101   struct timeval tv;
102
103   gettimeofday (&tv, 0);
104   NOW = (tstamp)tv.tv_sec + (tstamp)tv.tv_usec / 1000000.;
105 }
106 #endif
107
108 static bool iom_valid;
109
110 // used for initialisation only
111 static struct init {
112   init ()
113   {
114 #if IOM_SIG
115     sigemptyset (&sigs);
116
117     if (pipe (sigpipe))
118       {
119         perror ("io_manager: unable to create signal pipe, aborting.");
120         abort ();
121       }
122
123     fcntl (sigpipe[0], F_SETFL, O_NONBLOCK);
124     fcntl (sigpipe[1], F_SETFL, O_NONBLOCK);
125 #endif
126
127     iom_valid = true;
128
129 #if IOM_TIME
130     set_now ();
131
132     tw0.start (TSTAMP_MAX);
133 #endif
134   }
135
136   static void required ();
137 } init;
138
139 void
140 init::required ()
141 {
142   if (!iom_valid)
143     {
144       write (2, "io_manager: early registration attempt, aborting.\n",
145                 sizeof ("io_manager: early registration attempt, aborting.\n") - 1);
146       abort ();
147     }
148 }
149
150 template<class watcher>
151 void io_manager::reg (watcher &w, io_manager_vec<watcher> &queue)
152 {
153   init::required ();
154
155   if (!w.active)
156     {
157       queue.push_back (&w);
158       w.active = queue.size ();
159     }
160 }
161
162 template<class watcher>
163 void io_manager::unreg (watcher &w, io_manager_vec<watcher> &queue)
164 {
165   if (!iom_valid)
166     return;
167
168   if (w.active)
169     {
170       queue [w.active - 1] = 0;
171       w.active = 0;
172     }
173 }
174
175 #if IOM_TIME
176 void time_watcher::trigger ()
177 {
178   call (*this);
179   io_manager::reg (*this);
180 }
181
182 void io_manager::reg   (time_watcher &w)  { io_manager::reg   (w, tw); }
183 void io_manager::unreg (time_watcher &w)  { io_manager::unreg (w, tw); }
184 #endif
185
186 #if IOM_IO
187 void io_manager::reg   (io_watcher &w)    { io_manager::reg   (w, iow); }
188 void io_manager::unreg (io_watcher &w)    { io_manager::unreg (w, iow); }
189 #endif
190
191 #if IOM_CHECK
192 void io_manager::reg   (check_watcher &w) { io_manager::reg   (w, cw); }
193 void io_manager::unreg (check_watcher &w) { io_manager::unreg (w, cw); }
194 #endif
195
196 #if IOM_IDLE
197 void io_manager::reg   (idle_watcher &w)  { io_manager::reg   (w, iw); }
198 void io_manager::unreg (idle_watcher &w)  { io_manager::unreg (w, iw); }
199 #endif
200
201 #if IOM_SIG
202 static void
203 sighandler (int signum)
204 {
205   sw [signum - 1]->pending = true;
206
207   // we use a pipe for signal notifications, as most current
208   // OSes (Linux...) do not implement pselect correctly. ugh.
209   char ch = signum; // actual content not used
210   write (sigpipe[1], &ch, 1);
211 }
212
213 void io_manager::reg (sig_watcher &w)
214 {
215   assert (0 < w.signum);
216
217   sw.reserve (w.signum);
218
219   while (sw.size () < w.signum) // pathetic
220     sw.push_back (0);
221
222   sig_vec *&sv = sw[w.signum - 1];
223
224   if (!sv)
225     {
226       sv = new sig_vec;
227
228       sigaddset (&sigs, w.signum);
229       sigprocmask (SIG_BLOCK, &sigs, NULL);
230
231       struct sigaction sa;
232       sa.sa_handler = sighandler;
233       sigfillset (&sa.sa_mask);
234       sa.sa_flags = SA_RESTART;
235
236       if (sigaction (w.signum, &sa, 0))
237         {
238           perror ("io_manager: error while installing signal handler, ignoring.");
239           abort ();
240         }
241
242     }
243
244   io_manager::reg (w, *sv);
245 }
246
247 void io_manager::unreg (sig_watcher &w)
248 {
249   if (!w.active)
250     return;
251
252   assert (0 < w.signum && w.signum <= sw.size ());
253   
254   io_manager::unreg (w, *sw[w.signum - 1]);
255 }
256
257 void sig_watcher::start (int signum)
258 {
259   stop ();
260   this->signum = signum;
261   io_manager::reg (*this);
262 }
263 #endif
264
265 void io_manager::loop ()
266 {
267   init::required ();
268
269 #if IOM_TIME
270   set_now ();
271 #endif
272
273   for (;;)
274     {
275
276 #if IOM_TIME
277       // call pending time watchers
278       {
279         bool activity;
280
281         do
282           {
283             activity = false;
284
285             for (int i = tw.size (); i--; )
286               if (!tw[i])
287                 tw.erase_unordered (i);
288               else if (tw[i]->at <= NOW)
289                 {
290                   time_watcher &w = *tw[i];
291                   
292                   unreg (w);
293                   w.call (w);
294
295                   activity = true;
296                 }
297           }
298         while (activity);
299       }
300 #endif
301
302 #if IOM_CHECK
303       // call all check watchers
304       for (int i = cw.size (); i--; )
305         if (!cw[i])
306           cw.erase_unordered (i);
307         else
308           cw[i]->call (*cw[i]);
309 #endif
310
311       struct TIMEVAL *to = 0;
312       struct TIMEVAL tval;
313
314 #if IOM_IDLE
315       if (iw.size ())
316         {
317           tval.tv_sec  = 0;
318           tval.TV_FRAC = 0;
319           to = &tval;
320         }
321       else
322 #endif
323         {
324 #if IOM_TIME
325           // find earliest active watcher
326           time_watcher *next = tw[0]; // the first time-watcher must exist at ALL times
327
328           for (io_manager_vec<time_watcher>::const_iterator i = tw.end (); i-- > tw.begin (); )
329             if (*i && (*i)->at < next->at)
330               next = *i;
331
332           if (next->at > NOW && next != tw[0])
333             {
334               double diff = next->at - NOW;
335               tval.tv_sec  = (int)diff;
336               tval.TV_FRAC = (int) ((diff - tval.tv_sec) * TV_MULT);
337               to = &tval;
338             }
339         }
340 #endif
341
342 #if IOM_IO || IOM_SIG
343       fd_set rfd, wfd;
344
345       FD_ZERO (&rfd);
346       FD_ZERO (&wfd);
347
348       int fds = 0;
349
350 # if IOM_IO
351       for (io_manager_vec<io_watcher>::const_iterator i = iow.end (); i-- > iow.begin (); )
352         if (*i)
353           {
354             if ((*i)->events & EVENT_READ ) FD_SET ((*i)->fd, &rfd);
355             if ((*i)->events & EVENT_WRITE) FD_SET ((*i)->fd, &wfd);
356
357             if ((*i)->fd >= fds) fds = (*i)->fd + 1;
358           }
359 # endif
360
361       if (!to && !fds) //TODO: also check idle_watchers and check_watchers?
362         break; // no events
363
364 # if IOM_SIG
365       FD_SET (sigpipe[0], &rfd);
366       if (sigpipe[0] >= fds) fds = sigpipe[0] + 1;
367 # endif
368
369 # if IOM_SIG
370       // there is no race, as we use a pipe for signals, so select
371       // will return if a signal is caught.
372       sigprocmask (SIG_UNBLOCK, &sigs, NULL);
373 # endif
374       fds = select (fds, &rfd, &wfd, NULL, to);
375 # if IOM_SIG
376       sigprocmask (SIG_BLOCK, &sigs, NULL);
377 # endif
378
379 # if IOM_TIME
380       set_now ();
381 # endif
382
383       if (fds > 0)
384         {
385 # if IOM_SIG
386           if (FD_ISSET (sigpipe[0], &rfd))
387             {
388               char ch;
389
390               while (read (sigpipe[0], &ch, 1) > 0)
391                 ;
392
393               for (vector<sig_vec *>::iterator svp = sw.end (); svp-- > sw.begin (); )
394                 if (*svp && (*svp)->pending)
395                   {
396                     sig_vec &sv = **svp;
397                     for (int i = sv.size (); i--; )
398                       if (!sv[i])
399                         sv.erase_unordered (i);
400                       else
401                         sv[i]->call (*sv[i]);
402
403                     sv.pending = false;
404                   }
405             }
406 # endif
407
408 # if IOM_IO
409           for (int i = iow.size (); i--; )
410             if (!iow[i])
411               iow.erase_unordered (i);
412             else
413               {
414                 short revents = iow[i]->events;
415
416                 if (!FD_ISSET (iow[i]->fd, &rfd)) revents &= ~EVENT_READ;
417                 if (!FD_ISSET (iow[i]->fd, &wfd)) revents &= ~EVENT_WRITE;
418
419                 if (revents)
420                   iow[i]->call (*iow[i], revents);
421               }
422 #endif
423         }
424       else if (fds < 0 && errno != EINTR)
425         {
426           perror ("io_manager: fatal error while waiting for I/O or time event, aborting.");
427           abort ();
428         }
429 #if IOM_IDLE
430       else
431         for (int i = iw.size (); i--; )
432           if (!iw[i])
433             iw.erase_unordered (i);
434           else
435             iw[i]->call (*iw[i]);
436 #endif
437
438 #elif IOM_TIME
439       if (!to)
440         break;
441
442       select (0, 0, 0, 0, &to);
443       set_now ();
444 #else
445       break;
446 #endif
447     }
448 }
449