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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines