From cdaca02c2909452de23244738630f408b8eee8e1 Mon Sep 17 00:00:00 2001 From: Nick Mathewson Date: Wed, 27 May 2009 15:35:00 +0000 Subject: Activate fd events in a pseudorandom order on older backends. New backends like poll and kqueue and so on add fds to the queue in the order that they are triggered. But the select backend currently activates low-numbered fds first, whereas the poll and win32 backends currently favor whatever fds have been on for the longest. This is no good for fairness. svn:r1318 --- poll.c | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) (limited to 'poll.c') diff --git a/poll.c b/poll.c index 222b849e..08e148cb 100644 --- a/poll.c +++ b/poll.c @@ -117,7 +117,7 @@ poll_check_ok(struct pollop *pop) static int poll_dispatch(struct event_base *base, struct timeval *tv) { - int res, i, msec = -1, nfds; + int res, i, j, msec = -1, nfds; struct pollop *pop = base->evbase; poll_check_ok(pop); @@ -142,11 +142,15 @@ poll_dispatch(struct event_base *base, struct timeval *tv) event_debug(("%s: poll reports %d", __func__, res)); - if (res == 0) + if (res == 0 || nfds == 0) return (0); - for (i = 0; i < nfds; i++) { - int what = pop->event_set[i].revents; + i = random() % nfds; + for (j = 0; j < nfds; j++) { + int what; + if (++i == nfds) + i = 0; + what = pop->event_set[i].revents; if (!what) continue; -- cgit v1.2.1