[PATCH] event-loop: Only read one epoll event per loop dispatch.

Jonas Ådahl jadahl at gmail.com
Sat Mar 17 01:39:02 PDT 2012


In order for users of the event loop to manipulate the state of it or
others, only one event may be processed per epoll_wait. When multiple
events are queued up and a user removes an event input source from an
event loop from within a dispatch, if the removed source was one of the
queued up events the program may crash with a segmentation faults.

In order to allow queuing up multiple events there needs to be a way to
manipulate the state without destroying the source event.

Signed-off-by: Jonas Ådahl <jadahl at gmail.com>
---
 src/event-loop.c |   20 ++++++++++----------
 1 files changed, 10 insertions(+), 10 deletions(-)

diff --git a/src/event-loop.c b/src/event-loop.c
index 2dfe0ae..cc67383 100644
--- a/src/event-loop.c
+++ b/src/event-loop.c
@@ -447,23 +447,23 @@ dispatch_idle_sources(struct wl_event_loop *loop)
 WL_EXPORT int
 wl_event_loop_dispatch(struct wl_event_loop *loop, int timeout)
 {
-	struct epoll_event ep[32];
+	struct epoll_event ep;
 	struct wl_event_source *source;
-	int i, count, n;
+	int count, n;
 
 	dispatch_idle_sources(loop);
 
-	count = epoll_wait(loop->epoll_fd, ep, ARRAY_LENGTH(ep), timeout);
+	count = epoll_wait(loop->epoll_fd, &ep, 1, timeout);
 	if (count < 0)
 		return -1;
-	n = 0;
-	for (i = 0; i < count; i++) {
-		source = ep[i].data.ptr;
-		n += source->interface->dispatch(source, &ep[i]);
-	}
 
-	while (n > 0)
-		n = post_dispatch_check(loop);
+	if (count > 0) {
+		source = ep.data.ptr;
+		n = source->interface->dispatch(source, &ep);
+
+		if (n > 0)
+			post_dispatch_check(loop);
+	}
 		
 	return 0;
 }
-- 
1.7.5.4



More information about the wayland-devel mailing list