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.8 by pcg, Wed Apr 2 05:15:00 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);
43 at = when; 40 at = when;
41
42 if (registered)
43 iom.reschedule_time_watchers ();
44 else
44 iom.reg (this); 45 iom.reg (this);
46}
47
48void time_watcher::trigger ()
49{
50 call (at);
51
52 if (registered)
53 iom.reschedule_time_watchers ();
54 else
55 iom.reg (this);
56}
57
58void time_watcher::start ()
59{
60 if (!registered)
61 iom.reg (this);
45} 62}
46 63
47void io_manager::reg (int fd, short events, io_watcher *w) 64void io_manager::reg (int fd, short events, io_watcher *w)
48{ 65{
49 pollfd pfd; 66 pollfd pfd;
78 pfs[i] = pfs[sz - 1]; pfs.pop_back (); 95 pfs[i] = pfs[sz - 1]; pfs.pop_back ();
79 } 96 }
80 } 97 }
81} 98}
82 99
100void io_manager::reschedule_time_watchers ()
101{
102 make_heap (tw.begin (), tw.end (), lowest_first);
103}
104
83void io_manager::reg (time_watcher *w) 105void io_manager::reg (time_watcher *w)
84{ 106{
107 if (w->registered)
108 slog (L_CRIT, "FATAL: io_manager::reg(time_watcher) called on already-registered watcher");
109
110 w->registered = true;
111
85 tw.push_back (w); 112 tw.push_back (w);
86 push_heap (tw.begin (), tw.end (), lowest_first); 113 push_heap (tw.begin (), tw.end (), lowest_first);
87} 114}
88 115
89void io_manager::unreg (time_watcher *w) 116void io_manager::unreg (time_watcher *w)
90{ 117{
91 unsigned int sz = tw.size (); 118 if (w->registered)
92 unsigned int i = find (tw.begin (), tw.end (), w) - tw.begin ();
93
94 if (i != sz)
95 { 119 {
96 if (sz == 1) 120 unsigned int sz = tw.size ();
97 tw.clear (); 121 unsigned int i = find (tw.begin (), tw.end (), w) - tw.begin ();
98 else 122
123 if (i != sz)
99 { 124 {
100 if (i != sz - 1) 125 if (i != sz - 1)
101 tw[i] = tw[sz - 1]; 126 tw[i] = tw[sz - 1];
102 127
103 tw.pop_back (); 128 tw.pop_back ();
104 make_heap (tw.begin (), tw.end (), lowest_first); 129 reschedule_time_watchers ();
105 } 130 }
131
132 w->registered = false;
106 } 133 }
107} 134}
108 135
109inline void set_now (void) 136inline void set_now (void)
110{ 137{
111 struct timeval tv; 138 struct timeval tv;
112 139
113 gettimeofday (&tv, 0); 140 gettimeofday (&tv, 0);
114 141
115 NOW = (timestamp)tv.tv_sec + (timestamp)tv.tv_usec / 1000000; 142 NOW = (tstamp)tv.tv_sec + (tstamp)tv.tv_usec / 1000000;
116} 143}
117 144
118void io_manager::loop () 145void io_manager::loop ()
119{ 146{
120 set_now (); 147 set_now ();
121 148
122 for (;;) 149 for (;;)
123 { 150 {
124 int timeout = tw.empty () ? -1 : (int) ((tw[0]->at - NOW) * 1000); 151 while (tw[0]->at <= NOW)
125
126 //printf ("s%d t%d #%d <%f<%f<\n", pfs.size (), timeout, tw.size (), tw[0]->at - NOW, tw[1]->at - NOW);
127
128 if (timeout >= 0)
129 {
130 int fds = poll (&pfs[0], pfs.size (), timeout);
131
132 set_now ();
133
134 for (unsigned int i = iow.size (); fds && i--; )
135 if (pfs[i].revents)
136 {
137 --fds;
138 iow[i]->call (pfs[i].revents);
139 }
140 } 152 {
153 // remove the first watcher
154 time_watcher *w = tw[0];
141 155
142 while (!tw.empty () && tw[0]->at <= NOW)
143 {
144 pop_heap (tw.begin (), tw.end (), lowest_first); 156 pop_heap (tw.begin (), tw.end (), lowest_first);
145 time_watcher *w = tw[tw.size () - 1]; 157 tw.pop_back ();
158
159 w->registered = false;
160
161 // call it
146 w->call (w->at); 162 w->call (w->at);
147 push_heap (tw.begin (), tw.end (), lowest_first); 163
164 // re-add it if necessary
165 if (w->at >= 0 && !w->registered)
166 reg (w);
167 }
168
169 int timeout = (int) ((tw[0]->at - NOW) * 1000);
170
171 int fds = poll (&pfs[0], pfs.size (), timeout);
172
173 set_now ();
174
175 for (unsigned int i = 0; fds > 0 && i < iow.size (); ++i)
176 if (pfs[i].revents)
177 {
178 --fds;
179 iow[i]->call (pfs[i].fd, pfs[i].revents);
148 } 180 }
149 } 181 }
150} 182}
151 183
184void io_manager::idle_cb (tstamp &ts)
185{
186 ts = NOW + 86400; // wake up every day, for no good reason
187}
188
152io_manager::io_manager () 189io_manager::io_manager ()
153{ 190{
154 set_now (); 191 set_now ();
192 idle = new time_watcher (this, &io_manager::idle_cb);
193 idle->start (0);
155} 194}
156 195
157io_manager::~io_manager () 196io_manager::~io_manager ()
158{ 197{
159 // 198 //

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines