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.1 by pcg, Fri Mar 21 20:33:36 2003 UTC vs.
Revision 1.3 by pcg, Fri Mar 21 21:21:02 2003 UTC

1#include <unistd.h>
2/* 1/*
3 iom.C -- I/O multiplexor 2 iom.C -- I/O multiplexor
4 3
5 This program is free software; you can redistribute it and/or modify 4 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 5 it under the terms of the GNU General Public License as published by
23 22
24#include <algorithm> 23#include <algorithm>
25#include <functional> 24#include <functional>
26 25
27#include "slog.h" 26#include "slog.h"
28
29#include "iom.h" 27#include "iom.h"
30 28
31inline bool lowest_first (const time_watcher *a, const time_watcher *b) 29inline bool lowest_first (const time_watcher *a, const time_watcher *b)
32{ 30{
33 return a->at > b->at; 31 return a->at > b->at;
34} 32}
35 33
36timestamp NOW; 34tstamp NOW;
37 35
38io_manager iom; 36io_manager iom;
39 37
40void time_watcher::set (timestamp when) 38void time_watcher::set (tstamp when)
41{ 39{
42 iom.unreg (this); 40 iom.unreg (this);
43 at = when; 41 at = when;
44 iom.reg (this); 42 iom.reg (this);
45} 43}
110{ 108{
111 struct timeval tv; 109 struct timeval tv;
112 110
113 gettimeofday (&tv, 0); 111 gettimeofday (&tv, 0);
114 112
115 NOW = (timestamp)tv.tv_sec + (timestamp)tv.tv_usec / 1000000; 113 NOW = (tstamp)tv.tv_sec + (tstamp)tv.tv_usec / 1000000;
116} 114}
117 115
118void io_manager::loop () 116void io_manager::loop ()
119{ 117{
120 set_now (); 118 set_now ();
121 119
122 for (;;) 120 while (!(iow.empty () && tw.empty ()))
123 { 121 {
124 int timeout = tw.empty () ? -1 : (int) ((tw[0]->at - NOW) * 1000); 122 int timeout = tw.empty ()
123 ? 3600 * 1000 // wake up at least every hour
124 : (int) ((tw[0]->at - NOW) * 1000);
125 125
126 //printf ("s%d t%d #%d <%f<%f<\n", pfs.size (), timeout, tw.size (), tw[0]->at - NOW, tw[1]->at - NOW); 126 printf ("s%d t%d #%d\n", pfs.size (), timeout, tw.size ());
127 127
128 if (timeout >= 0) 128 if (timeout >= 0)
129 { 129 {
130 int fds = poll (&pfs[0], pfs.size (), timeout); 130 int fds = poll (&pfs[0], pfs.size (), timeout);
131 131
140 } 140 }
141 141
142 while (!tw.empty () && tw[0]->at <= NOW) 142 while (!tw.empty () && tw[0]->at <= NOW)
143 { 143 {
144 pop_heap (tw.begin (), tw.end (), lowest_first); 144 pop_heap (tw.begin (), tw.end (), lowest_first);
145 time_watcher *w = tw[tw.size () - 1]; 145 (*(tw.end () - 1))->trigger ();
146 w->call (w->at);
147 push_heap (tw.begin (), tw.end (), lowest_first); 146 push_heap (tw.begin (), tw.end (), lowest_first);
148 } 147 }
149 } 148 }
150} 149}
151 150

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines