Krolyxon
Commit abb5fee

Branches

Refs →

nsxivabb5fee

Corrected timeout handling

Author
Bert
Date
2011-09-02 18:54 +0200
Parent
dcd04e5
Commit
abb5feeb786a711e57a63fa6027ca06002226c68

+7 −6

1 file changed

  1. M main.c +7 −6
M main.c View
@@ -160,21 +160,23 @@ void reset_timeout(timeout_f handler) {
160160 }
161161
162162 int check_timeouts(struct timeval *t) {
163− int i, tdiff, tmin = -1;
163+ int i = 0, tdiff, tmin = -1;
164164 struct timeval now;
165165
166166 gettimeofday(&now, 0);
167− for (i = 0; i < LEN(timeouts); i++) {
167+ while (i < LEN(timeouts)) {
168168 if (timeouts[i].active) {
169169 tdiff = TIMEDIFF(&timeouts[i].when, &now);
170170 if (tdiff <= 0) {
171171 timeouts[i].active = False;
172172 if (timeouts[i].handler)
173173 timeouts[i].handler();
174+ i = tmin = -1;
174175 } else if (tmin < 0 || tdiff < tmin) {
175176 tmin = tdiff;
176177 }
177178 }
179+ i++;
178180 }
179181 if (tmin > 0 && t)
180182 MSEC_TO_TIMEVAL(tmin, t);
@@ -375,13 +377,12 @@ void run() {
375377 }
376378 }
377379
378− if (!XPending(win.env.dpy) && check_timeouts(&timeout)) {
379− /* handle timeouts */
380+ while (!XPending(win.env.dpy) && check_timeouts(&timeout)) {
381+ /* wait for timeouts */
380382 xfd = ConnectionNumber(win.env.dpy);
381383 FD_ZERO(&fds);
382384 FD_SET(xfd, &fds);
383− if (!select(xfd + 1, &fds, 0, 0, &timeout))
384− check_timeouts(NULL);
385+ select(xfd + 1, &fds, 0, 0, &timeout);
385386 }
386387
387388 if (!XNextEvent(win.env.dpy, &ev)) {