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.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
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#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
91 time_watcher *w; 135 time_watcher *w;
92 136
93 for (;;) 137 for (;;)
94 { 138 {
95 w = tw[0]; 139 w = tw[0];
96 140
97 for (time_watcher **i = tw.begin (); i != tw.end (); ++i) 141 for (time_watcher **i = tw.begin (); i < tw.end (); ++i)
98 if ((*i)->at < w->at) 142 if ((*i)->at < w->at)
99 w = *i; 143 w = *i;
100 144
101 if (w->at > NOW) 145 if (w->at > NOW)
102 break; 146 break;
106 150
107 if (w->at < 0) 151 if (w->at < 0)
108 unreg (w); 152 unreg (w);
109 } 153 }
110 154
155 double diff = w->at - NOW;
111 struct timeval to; 156 struct timeval to;
112 double diff = w->at - NOW;
113 to.tv_sec = (int)diff; 157 to.tv_sec = (int)diff;
114 to.tv_usec = (int)((diff - to.tv_sec) * 1000000); 158 to.tv_usec = (int)((diff - to.tv_sec) * 1000000);
159#endif
115 160
161#if IOM_IO
116 fd_set rfd, wfd; 162 fd_set rfd, wfd;
117 163
118 FD_ZERO (&rfd); 164 FD_ZERO (&rfd);
119 FD_ZERO (&wfd); 165 FD_ZERO (&wfd);
120 166
126 if ((*w)->events & EVENT_WRITE) FD_SET ((*w)->fd, &wfd); 172 if ((*w)->events & EVENT_WRITE) FD_SET ((*w)->fd, &wfd);
127 173
128 if ((*w)->fd > fds) fds = (*w)->fd; 174 if ((*w)->fd > fds) fds = (*w)->fd;
129 } 175 }
130 176
177# if IOM_TIME
131 fds = select (fds + 1, &rfd, &wfd, 0, &to); 178 fds = select (fds + 1, &rfd, &wfd, 0, &to);
132
133 set_now (); 179 set_now ();
180# else
181 fds = select (fds + 1, &rfd, &wfd, 0, 0);
182# endif
134 183
135 if (fds > 0) 184 if (fds > 0)
136 for (io_watcher **w = iow.begin (); w < iow.end (); ++w) 185 for (int i = 0; i < iow.size (); ++i)
137 { 186 {
187 io_watcher *w = iow[i];
188
138 short revents = (*w)->events; 189 short revents = w->events;
139 190
140 if (!FD_ISSET ((*w)->fd, &rfd)) revents &= ~EVENT_READ; 191 if (!FD_ISSET (w->fd, &rfd)) revents &= ~EVENT_READ;
141 if (!FD_ISSET ((*w)->fd, &wfd)) revents &= ~EVENT_WRITE; 192 if (!FD_ISSET (w->fd, &wfd)) revents &= ~EVENT_WRITE;
142 193
143 if (revents) 194 if (revents)
144 (*w)->call (**w, revents); 195 w->call (*w, revents);
145 } 196 }
146 } 197 }
198#elif IOM_TIME
199 select (0, 0, 0, 0, &to);
200 set_now ();
201#endif
147} 202}
148 203
149void io_manager::idle_cb (time_watcher &w) 204void io_manager::idle_cb (time_watcher &w)
150{ 205{
151 w.at = NOW + 1000000000; 206 w.at = NOW + 1000000000;
155{ 210{
156 set_now (); 211 set_now ();
157 212
158 iom_valid = true; 213 iom_valid = true;
159 214
215#if IOM_TIME
160 idle = new time_watcher (this, &io_manager::idle_cb); 216 idle = new time_watcher (this, &io_manager::idle_cb);
161 idle->start (0); 217 idle->start (0);
218#endif
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