[Xcb] [PATCH libxcb 1/3] Introduce a variant of xcb_poll_for_event for examining event queue.

Rami Ylimäki rami.ylimaki at vincit.fi
Tue Mar 22 05:33:23 PDT 2011


In some circumstances using xcb_poll_for_event is suboptimal because
it checks the connection for new events. This may lead to a lot of
failed nonblocking read system calls.

Signed-off-by: Rami Ylimäki <rami.ylimaki at vincit.fi>
---
 src/xcb.h    |   16 ++++++++++++++++
 src/xcb_in.c |   14 ++++++++++++--
 2 files changed, 28 insertions(+), 2 deletions(-)

diff --git a/src/xcb.h b/src/xcb.h
index 3e61ebd..8a81bb9 100644
--- a/src/xcb.h
+++ b/src/xcb.h
@@ -272,6 +272,22 @@ xcb_generic_event_t *xcb_wait_for_event(xcb_connection_t *c);
 xcb_generic_event_t *xcb_poll_for_event(xcb_connection_t *c);
 
 /**
+ * @brief Returns the next event without reading from the connection.
+ * @param c: The connection to the X server.
+ * @return The next already queued event from the server.
+ *
+ * This is a version of xcb_poll_for_event that only examines the
+ * event queue for new events. The function doesn't try to read new
+ * events from the connection if no queued events are found.
+ *
+ * This function is useful for callers that know in advance that all
+ * interesting events have already been read from the connection. For
+ * example, callers might use xcb_wait_for_reply and be interested
+ * only of events that preceded a specific reply.
+ */
+xcb_generic_event_t *xcb_poll_for_queued_event(xcb_connection_t *c);
+
+/**
  * @brief Returns the next event or error that precedes the given request.
  * @param c: The connection to the X server.
  * @param request: The limiting sequence number.
diff --git a/src/xcb_in.c b/src/xcb_in.c
index ca75169..2d15d3b 100644
--- a/src/xcb_in.c
+++ b/src/xcb_in.c
@@ -535,7 +535,7 @@ xcb_generic_event_t *xcb_wait_for_event(xcb_connection_t *c)
     return ret;
 }
 
-xcb_generic_event_t *xcb_poll_for_event(xcb_connection_t *c)
+static xcb_generic_event_t *poll_for_next_event(xcb_connection_t *c, int queued)
 {
     xcb_generic_event_t *ret = 0;
     if(!c->has_error)
@@ -543,13 +543,23 @@ xcb_generic_event_t *xcb_poll_for_event(xcb_connection_t *c)
         pthread_mutex_lock(&c->iolock);
         /* FIXME: follow X meets Z architecture changes. */
         ret = get_event(c);
-        if(!ret && _xcb_in_read(c)) /* _xcb_in_read shuts down the connection on error */
+        if(!ret && !queued && _xcb_in_read(c)) /* _xcb_in_read shuts down the connection on error */
             ret = get_event(c);
         pthread_mutex_unlock(&c->iolock);
     }
     return ret;
 }
 
+xcb_generic_event_t *xcb_poll_for_event(xcb_connection_t *c)
+{
+    return poll_for_next_event(c, 0);
+}
+
+xcb_generic_event_t *xcb_poll_for_queued_event(xcb_connection_t *c)
+{
+    return poll_for_next_event(c, 1);
+}
+
 static xcb_generic_event_t *get_event_until(xcb_connection_t *c, uint64_t request)
 {
     if(c->in.events && XCB_SEQUENCE_COMPARE(c->in.events->sequence, <=, request))
-- 
1.6.3.3



More information about the Xcb mailing list