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

Comparing rxvt-unicode/src/iom.C (file contents):
Revision 1.2 by pcg, Tue Nov 25 15:25:17 2003 UTC vs.
Revision 1.4 by pcg, Wed Nov 26 10:42:34 2003 UTC

28 28
29tstamp NOW; 29tstamp NOW;
30bool iom_valid; 30bool iom_valid;
31io_manager iom; 31io_manager iom;
32 32
33void time_watcher::trigger () 33template<class watcher>
34void io_manager::reg (watcher *w, simplevec<watcher *> &queue)
34{ 35{
35 call (*this); 36 if (find (queue.begin (), queue.end (), w) == queue.end ())
36 37 queue.push_back (w);
37 iom.reg (this);
38} 38}
39 39
40template<class watcher>
41void io_manager::unreg (watcher *w, simplevec<watcher *> &queue)
42{
43 queue.erase (find (queue.begin (), queue.end (), w));
44}
45
46#if IOM_IO
40time_watcher::~time_watcher () 47io_watcher::~io_watcher ()
41{ 48{
42 if (iom_valid) 49 if (iom_valid)
43 iom.unreg (this); 50 iom.unreg (this);
44} 51}
45 52
53void io_manager::reg (io_watcher *w)
54{
55 reg (w, iow);
56}
57
58void io_manager::unreg (io_watcher *w)
59{
60 unreg (w, iow);
61}
62
63#endif
64
65#if IOM_TIME
66void time_watcher::trigger ()
67{
68 call (*this);
69
70 iom.reg (this);
71}
72
46io_watcher::~io_watcher () 73time_watcher::~time_watcher ()
47{ 74{
48 if (iom_valid) 75 if (iom_valid)
49 iom.unreg (this); 76 iom.unreg (this);
50}
51 77
52void io_manager::reg (io_watcher *w) 78 at = TSTAMP_CANCEL;
53{
54 if (find (iow.begin (), iow.end (), w) == iow.end ())
55 iow.push_back (w);
56}
57
58void io_manager::unreg (io_watcher *w)
59{
60 iow.erase (find (iow.begin (), iow.end (), w));
61} 79}
62 80
63void io_manager::reg (time_watcher *w) 81void io_manager::reg (time_watcher *w)
64{ 82{
65 if (find (tw.begin (), tw.end (), w) == tw.end ()) 83 reg (w, tw);
66 tw.push_back (w);
67} 84}
68 85
69void io_manager::unreg (time_watcher *w) 86void io_manager::unreg (time_watcher *w)
70{ 87{
71 tw.erase (find (tw.begin (), tw.end (), w)); 88 unreg (w, tw);
72} 89}
90#endif
73 91
92#if IOM_CHECK
93check_watcher::~check_watcher ()
94{
95 if (iom_valid)
96 iom.unreg (this);
97}
98
99void io_manager::reg (check_watcher *w)
100{
101 reg (w, cw);
102}
103
104void io_manager::unreg (check_watcher *w)
105{
106 unreg (w, cw);
107}
108#endif
109
110#if IOM_TIME
74inline void set_now (void) 111inline void set_now (void)
75{ 112{
76 struct timeval tv; 113 struct timeval tv;
77 114
78 gettimeofday (&tv, 0); 115 gettimeofday (&tv, 0);
79 116
80 NOW = (tstamp)tv.tv_sec + (tstamp)tv.tv_usec / 1000000; 117 NOW = (tstamp)tv.tv_sec + (tstamp)tv.tv_usec / 1000000;
118#endif
81} 119}
82 120
83void io_manager::loop () 121void io_manager::loop ()
84{ 122{
123#if IOM_TIME
85 set_now (); 124 set_now ();
125#endif
86 126
87 for (;;) 127 for (;;)
88 { 128 {
129#if IOM_CHECK
130 for (int i = 0; i < cw.size (); ++i)
131 cw[i]->call (*cw[i]);
132#endif
133
134#if IOM_TIME
89 time_watcher *w; 135 time_watcher *w;
90 136
91 for (;;) 137 for (;;)
92 { 138 {
93 w = tw[0]; 139 w = tw[0];
94 140
95 for (time_watcher **i = tw.begin (); i != tw.end (); ++i) 141 for (time_watcher **i = tw.begin (); i < tw.end (); ++i)
96 if ((*i)->at < w->at) 142 if ((*i)->at < w->at)
97 w = *i; 143 w = *i;
98 144
99 if (w->at > NOW) 145 if (w->at > NOW)
100 break; 146 break;
101 147
102 // call it 148 // call it
103 w->call (*w); 149 w->call (*w);
104 150
105 // re-add it if necessary
106 if (w->at >= 0) 151 if (w->at < 0)
107 reg (w); 152 unreg (w);
108 } 153 }
109 154
155 double diff = w->at - NOW;
110 struct timeval to; 156 struct timeval to;
111 double diff = w->at - NOW;
112 to.tv_sec = (int)diff; 157 to.tv_sec = (int)diff;
113 to.tv_usec = (int)((diff - to.tv_sec) * 1000000); 158 to.tv_usec = (int)((diff - to.tv_sec) * 1000000);
159#endif
114 160
161#if IOM_IO
115 fd_set rfd, wfd; 162 fd_set rfd, wfd;
116 163
117 FD_ZERO (&rfd); 164 FD_ZERO (&rfd);
118 FD_ZERO (&wfd); 165 FD_ZERO (&wfd);
119 166
125 if ((*w)->events & EVENT_WRITE) FD_SET ((*w)->fd, &wfd); 172 if ((*w)->events & EVENT_WRITE) FD_SET ((*w)->fd, &wfd);
126 173
127 if ((*w)->fd > fds) fds = (*w)->fd; 174 if ((*w)->fd > fds) fds = (*w)->fd;
128 } 175 }
129 176
177# if IOM_TIME
130 fds = select (fds + 1, &rfd, &wfd, 0, &to); 178 fds = select (fds + 1, &rfd, &wfd, 0, &to);
131
132 set_now (); 179 set_now ();
180# else
181 fds = select (fds + 1, &rfd, &wfd, 0, 0);
182# endif
133 183
134 if (fds > 0) 184 if (fds > 0)
135 for (io_watcher **w = iow.begin (); w < iow.end (); ++w) 185 for (int i = 0; i < iow.size (); ++i)
136 { 186 {
187 io_watcher *w = iow[i];
188
137 short revents = (*w)->events; 189 short revents = w->events;
138 190
139 if (!FD_ISSET ((*w)->fd, &rfd)) revents &= ~EVENT_READ; 191 if (!FD_ISSET (w->fd, &rfd)) revents &= ~EVENT_READ;
140 if (!FD_ISSET ((*w)->fd, &wfd)) revents &= ~EVENT_WRITE; 192 if (!FD_ISSET (w->fd, &wfd)) revents &= ~EVENT_WRITE;
141 193
142 if (revents) 194 if (revents)
143 (*w)->call (**w, revents); 195 w->call (*w, revents);
144 } 196 }
145 } 197 }
198#elif IOM_TIME
199 select (0, 0, 0, 0, &to);
200 set_now ();
201#endif
146} 202}
147 203
148void io_manager::idle_cb (time_watcher &w) 204void io_manager::idle_cb (time_watcher &w)
149{ 205{
150 w.at = NOW + 1000000000; 206 w.at = NOW + 1000000000;
154{ 210{
155 set_now (); 211 set_now ();
156 212
157 iom_valid = true; 213 iom_valid = true;
158 214
215#if IOM_TIME
159 idle = new time_watcher (this, &io_manager::idle_cb); 216 idle = new time_watcher (this, &io_manager::idle_cb);
160 idle->start (0); 217 idle->start (0);
218#endif
161} 219}
162 220
163io_manager::~io_manager () 221io_manager::~io_manager ()
164{ 222{
165 iom_valid = false; 223 iom_valid = false;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines