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

Comparing gvpe/src/iom.C (file contents):
Revision 1.21 by pcg, Thu Sep 2 07:50:43 2004 UTC vs.
Revision 1.26 by pcg, Wed Dec 15 02:59:54 2004 UTC

1/* 1/*
2 iom.C -- generic I/O multiplexor 2 iom.C -- generic I/O multiplexer
3 Copyright (C) 2003, 2004 Marc Lehmann <pcg@goof.com> 3 Copyright (C) 2003, 2004 Marc Lehmann <pcg@goof.com>
4 4
5 This program is free software; you can redistribute it and/or modify 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 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 7 the Free Software Foundation; either version 2 of the License, or
94 } tw0; 94 } tw0;
95 95
96tstamp NOW; 96tstamp NOW;
97 97
98#if IOM_TIME 98#if IOM_TIME
99inline void set_now (void) 99tstamp io_manager::now ()
100{ 100{
101 struct timeval tv; 101 struct timeval tv;
102 102
103 gettimeofday (&tv, 0); 103 gettimeofday (&tv, 0);
104 NOW = (tstamp)tv.tv_sec + (tstamp)tv.tv_usec / 1000000.; 104 return (tstamp)tv.tv_sec + (tstamp)tv.tv_usec / 1000000.;
105}
106
107void io_manager::set_now ()
108{
109 NOW = now ();
105} 110}
106#endif 111#endif
107 112
108static bool iom_valid; 113static bool iom_valid;
109 114
118 { 123 {
119 perror ("io_manager: unable to create signal pipe, aborting."); 124 perror ("io_manager: unable to create signal pipe, aborting.");
120 abort (); 125 abort ();
121 } 126 }
122 127
123 fcntl (sigpipe[0], F_SETFL, O_NONBLOCK); 128 fcntl (sigpipe[0], F_SETFL, O_NONBLOCK); fcntl (sigpipe[0], F_SETFD, FD_CLOEXEC);
124 fcntl (sigpipe[1], F_SETFL, O_NONBLOCK); 129 fcntl (sigpipe[1], F_SETFL, O_NONBLOCK); fcntl (sigpipe[1], F_SETFD, FD_CLOEXEC);
125#endif 130#endif
126 131
127 iom_valid = true; 132 iom_valid = true;
128 133
129#if IOM_TIME 134#if IOM_TIME
130 set_now (); 135 io_manager::set_now ();
131 136
132 tw0.start (TSTAMP_MAX); 137 tw0.start (TSTAMP_MAX);
133#endif 138#endif
134 } 139 }
135 140
152{ 157{
153 init::required (); 158 init::required ();
154 159
155 if (!w.active) 160 if (!w.active)
156 { 161 {
157#if IOM_CHECK
158 queue.activity = true;
159#endif
160 queue.push_back (&w); 162 queue.push_back (&w);
161 w.active = queue.size (); 163 w.active = queue.size ();
162 } 164 }
163} 165}
164 166
273 set_now (); 275 set_now ();
274#endif 276#endif
275 277
276 for (;;) 278 for (;;)
277 { 279 {
280
281#if IOM_TIME
282 // call pending time watchers
283 {
284 bool activity;
285
286 do
287 {
288 activity = false;
289
290 for (int i = tw.size (); i--; )
291 if (!tw[i])
292 tw.erase_unordered (i);
293 else if (tw[i]->at <= NOW)
294 {
295 time_watcher &w = *tw[i];
296
297 unreg (w);
298 w.call (w);
299
300 activity = true;
301 }
302 }
303 while (activity);
304 }
305#endif
306
307#if IOM_CHECK
308 // call all check watchers
309 for (int i = cw.size (); i--; )
310 if (!cw[i])
311 cw.erase_unordered (i);
312 else
313 cw[i]->call (*cw[i]);
314#endif
315
278 struct TIMEVAL *to = 0; 316 struct TIMEVAL *to = 0;
279 struct TIMEVAL tval; 317 struct TIMEVAL tval;
280 318
281#if IOM_IDLE 319#if IOM_IDLE
282 if (iw.size ()) 320 if (iw.size ())
287 } 325 }
288 else 326 else
289#endif 327#endif
290 { 328 {
291#if IOM_TIME 329#if IOM_TIME
292 time_watcher *next; 330 // find earliest active watcher
331 time_watcher *next = tw[0]; // the first time-watcher must exist at ALL times
293 332
294 for (;;) 333 for (io_manager_vec<time_watcher>::const_iterator i = tw.end (); i-- > tw.begin (); )
334 if (*i && (*i)->at < next->at)
335 next = *i;
336
337 if (next->at > NOW && next != tw[0])
295 { 338 {
296 next = tw[0]; // the first time-watcher must exist at ALL times
297
298 for (int i = tw.size (); i--; )
299 if (!tw[i])
300 tw.erase_unordered (i);
301 else if (tw[i]->at < next->at)
302 next = tw[i];
303
304 if (next->at > NOW)
305 {
306 if (next != tw[0])
307 {
308 double diff = next->at - NOW; 339 double diff = next->at - NOW;
309 tval.tv_sec = (int)diff; 340 tval.tv_sec = (int)diff;
310 tval.TV_FRAC = (int) ((diff - tval.tv_sec) * TV_MULT); 341 tval.TV_FRAC = (int) ((diff - tval.tv_sec) * TV_MULT);
311 to = &tval; 342 to = &tval;
312 }
313 break;
314 }
315 else
316 {
317 unreg (*next);
318 next->call (*next);
319 }
320 } 343 }
321#endif
322 }
323
324#if IOM_CHECK
325 tw.activity = false;
326
327 for (int i = cw.size (); i--; )
328 if (!cw[i])
329 cw.erase_unordered (i);
330 else
331 cw[i]->call (*cw[i]);
332
333 if (tw.activity)
334 {
335 tval.tv_sec = 0;
336 tval.TV_FRAC = 0;
337 to = &tval;
338 } 344 }
339#endif 345#endif
340 346
341#if IOM_IO || IOM_SIG 347#if IOM_IO || IOM_SIG
342 fd_set rfd, wfd; 348 fd_set rfd, wfd;
345 FD_ZERO (&wfd); 351 FD_ZERO (&wfd);
346 352
347 int fds = 0; 353 int fds = 0;
348 354
349# if IOM_IO 355# if IOM_IO
350 for (io_manager_vec<io_watcher>::iterator i = iow.end (); i-- > iow.begin (); ) 356 for (io_manager_vec<io_watcher>::const_iterator i = iow.end (); i-- > iow.begin (); )
351 if (*i) 357 if (*i)
352 { 358 {
353 if ((*i)->events & EVENT_READ ) FD_SET ((*i)->fd, &rfd); 359 if ((*i)->events & EVENT_READ ) FD_SET ((*i)->fd, &rfd);
354 if ((*i)->events & EVENT_WRITE) FD_SET ((*i)->fd, &wfd); 360 if ((*i)->events & EVENT_WRITE) FD_SET ((*i)->fd, &wfd);
355 361
356 if ((*i)->fd >= fds) fds = (*i)->fd + 1; 362 if ((*i)->fd >= fds) fds = (*i)->fd + 1;
357 } 363 }
358# endif 364# endif
359 365
360 if (!to && !fds) //TODO: also check idle_watchers and check_watchers 366 if (!to && !fds) //TODO: also check idle_watchers and check_watchers?
361 break; // no events 367 break; // no events
362 368
363# if IOM_SIG 369# if IOM_SIG
364 FD_SET (sigpipe[0], &rfd); 370 FD_SET (sigpipe[0], &rfd);
365 if (sigpipe[0] >= fds) fds = sigpipe[0] + 1; 371 if (sigpipe[0] >= fds) fds = sigpipe[0] + 1;
387 char ch; 393 char ch;
388 394
389 while (read (sigpipe[0], &ch, 1) > 0) 395 while (read (sigpipe[0], &ch, 1) > 0)
390 ; 396 ;
391 397
392 for (sig_vec **svp = sw.end (); svp-- > sw.begin (); ) 398 for (vector<sig_vec *>::iterator svp = sw.end (); svp-- > sw.begin (); )
393 if (*svp && (*svp)->pending) 399 if (*svp && (*svp)->pending)
394 { 400 {
395 sig_vec &sv = **svp; 401 sig_vec &sv = **svp;
396 for (int i = sv.size (); i--; ) 402 for (int i = sv.size (); i--; )
397 if (!sv[i]) 403 if (!sv[i])

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines