[Xcb-commit] 4 commits - xcb

Jamey Sharp jamey at kemper.freedesktop.org
Wed Apr 19 20:32:10 PDT 2006


 xcb/src/xcb.h     |    5 +
 xcb/src/xcb_in.c  |  139 +++++++++++++++++++++++++++++++-----------------------
 xcb/src/xcbext.h  |    1 
 xcb/src/xcbxlib.h |    2 
 4 files changed, 86 insertions(+), 61 deletions(-)

New commits:
diff-tree 2c7e6991e263ac0190cb3b2c75627ababfd3ee4f (from 629d08fa6c9d5e220c0e61ed79063c92dc7ab313)
Author: Jamey Sharp <jamey at minilop.net>
Date:   Wed Apr 19 20:31:20 2006 -0700

    Remove the last goto in XCB: XCBWaitForReply now permits multiple threads to force the same cookie.

diff --git a/xcb/src/xcb_in.c b/xcb/src/xcb_in.c
index 15bc915..db9d1ca 100644
--- a/xcb/src/xcb_in.c
+++ b/xcb/src/xcb_in.c
@@ -312,9 +312,7 @@ void *XCBWaitForReply(XCBConnection *c, 
         reader_list **prev_reader;
 
         for(prev_reader = &c->in.readers; *prev_reader && (*prev_reader)->request <= request; prev_reader = &(*prev_reader)->next)
-            if((*prev_reader)->request == request)
-                goto done; /* error */
-
+            /* empty */;
         reader.request = request;
         reader.data = &cond;
         reader.next = *prev_reader;
@@ -324,7 +322,6 @@ void *XCBWaitForReply(XCBConnection *c, 
             if(!_xcb_conn_wait(c, &cond, 0, 0))
                 break;
 
-done:
         for(prev_reader = &c->in.readers; *prev_reader && (*prev_reader)->request <= request; prev_reader = &(*prev_reader)->next)
             if(*prev_reader == &reader)
             {
diff-tree 629d08fa6c9d5e220c0e61ed79063c92dc7ab313 (from 0cbe121e133887e3c0e04e2ea745001420eea2c7)
Author: Jamey Sharp <jamey at minilop.net>
Date:   Wed Apr 19 20:23:37 2006 -0700

    Restructure XCBWaitForReply to eliminate two gotos.

diff --git a/xcb/src/xcb_in.c b/xcb/src/xcb_in.c
index 24fb041..15bc915 100644
--- a/xcb/src/xcb_in.c
+++ b/xcb/src/xcb_in.c
@@ -298,9 +298,6 @@ static int poll_for_reply(XCBConnection 
 
 void *XCBWaitForReply(XCBConnection *c, unsigned int request, XCBGenericError **e)
 {
-    pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
-    reader_list reader;
-    reader_list **prev_reader;
     void *ret = 0;
     if(e)
         *e = 0;
@@ -308,32 +305,34 @@ void *XCBWaitForReply(XCBConnection *c, 
     pthread_mutex_lock(&c->iolock);
 
     /* If this request has not been written yet, write it. */
-    if(!_xcb_out_flush_to(c, request))
-        goto done; /* error */
-
-    for(prev_reader = &c->in.readers; *prev_reader && (*prev_reader)->request <= request; prev_reader = &(*prev_reader)->next)
-        if((*prev_reader)->request == request)
-            goto done; /* error */
-
-    reader.request = request;
-    reader.data = &cond;
-    reader.next = *prev_reader;
-    *prev_reader = &reader;
-
-    /* If this request has not completed yet and has no reply waiting,
-     * wait for one. */
-    while(!poll_for_reply(c, request, &ret, e))
-        if(!_xcb_conn_wait(c, &cond, 0, 0))
-            goto done;
+    if(_xcb_out_flush_to(c, request))
+    {
+        pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
+        reader_list reader;
+        reader_list **prev_reader;
+
+        for(prev_reader = &c->in.readers; *prev_reader && (*prev_reader)->request <= request; prev_reader = &(*prev_reader)->next)
+            if((*prev_reader)->request == request)
+                goto done; /* error */
+
+        reader.request = request;
+        reader.data = &cond;
+        reader.next = *prev_reader;
+        *prev_reader = &reader;
+
+        while(!poll_for_reply(c, request, &ret, e))
+            if(!_xcb_conn_wait(c, &cond, 0, 0))
+                break;
 
 done:
-    for(prev_reader = &c->in.readers; *prev_reader && (*prev_reader)->request <= request; prev_reader = &(*prev_reader)->next)
-        if(*prev_reader == &reader)
-        {
-            *prev_reader = (*prev_reader)->next;
-            break;
-        }
-    pthread_cond_destroy(&cond);
+        for(prev_reader = &c->in.readers; *prev_reader && (*prev_reader)->request <= request; prev_reader = &(*prev_reader)->next)
+            if(*prev_reader == &reader)
+            {
+                *prev_reader = (*prev_reader)->next;
+                break;
+            }
+        pthread_cond_destroy(&cond);
+    }
 
     wake_up_next_reader(c);
     pthread_mutex_unlock(&c->iolock);
diff-tree 0cbe121e133887e3c0e04e2ea745001420eea2c7 (from c9ab5b6fb67ab93c215b282e85fffcebd609ebec)
Author: Jamey Sharp <jamey at minilop.net>
Date:   Wed Apr 19 20:15:15 2006 -0700

    Fixed poll_for_reply, added comments, and refactored XCBWaitForReply to call poll_for_reply.

diff --git a/xcb/src/xcb_in.c b/xcb/src/xcb_in.c
index 4ad4654..24fb041 100644
--- a/xcb/src/xcb_in.c
+++ b/xcb/src/xcb_in.c
@@ -243,23 +243,33 @@ static int poll_for_reply(XCBConnection 
 {
     struct reply_list *head;
 
+    /* If an error occurred when issuing the request, fail immediately. */
     if(!request)
         head = 0;
-    else if(c->in.request_completed >= request)
+    /* We've read requests past the one we want, so if it has replies we have
+     * them all and they're in the replies map. */
+    else if(request < c->in.request_read)
     {
         head = _xcb_map_remove(c->in.replies, request);
         if(head && head->next)
             _xcb_map_put(c->in.replies, request, head->next);
     }
-    else if(c->in.request_read == request && c->in.current_reply)
+    /* We're currently processing the responses to the request we want, and we
+     * have a reply ready to return. So just return it without blocking. */
+    else if(request == c->in.request_read && c->in.current_reply)
     {
         head = c->in.current_reply;
         c->in.current_reply = head->next;
         if(!head->next)
             c->in.current_reply_tail = &c->in.current_reply;
     }
+    /* We know this request can't have any more replies, and we've already
+     * established it doesn't have a reply now. Don't bother blocking. */
+    else if(request == c->in.request_completed)
+        head = 0;
+    /* We may have more replies on the way for this request: block until we're
+     * sure. */
     else
-        /* Would block: do nothing. */
         return 0;
 
     if(error)
@@ -291,15 +301,10 @@ void *XCBWaitForReply(XCBConnection *c, 
     pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
     reader_list reader;
     reader_list **prev_reader;
-    struct reply_list *head;
     void *ret = 0;
     if(e)
         *e = 0;
 
-    /* If an error occurred when issuing the request, fail immediately. */
-    if(!request)
-        return 0;
-
     pthread_mutex_lock(&c->iolock);
 
     /* If this request has not been written yet, write it. */
@@ -317,43 +322,10 @@ void *XCBWaitForReply(XCBConnection *c, 
 
     /* If this request has not completed yet and has no reply waiting,
      * wait for one. */
-    while(c->in.request_completed < request &&
-            !(c->in.request_read == request && c->in.current_reply))
+    while(!poll_for_reply(c, request, &ret, e))
         if(!_xcb_conn_wait(c, &cond, 0, 0))
             goto done;
 
-    if(c->in.request_read != request)
-    {
-        head = _xcb_map_remove(c->in.replies, request);
-        if(head && head->next)
-            _xcb_map_put(c->in.replies, request, head->next);
-    }
-    else
-    {
-        head = c->in.current_reply;
-        if(head)
-        {
-            c->in.current_reply = head->next;
-            if(!head->next)
-                c->in.current_reply_tail = &c->in.current_reply;
-        }
-    }
-
-    if(head)
-    {
-        ret = head->reply;
-        free(head);
-
-        if(((XCBGenericRep *) ret)->response_type == XCBError)
-        {
-            if(e)
-                *e = ret;
-            else
-                free(ret);
-            ret = 0;
-        }
-    }
-
 done:
     for(prev_reader = &c->in.readers; *prev_reader && (*prev_reader)->request <= request; prev_reader = &(*prev_reader)->next)
         if(*prev_reader == &reader)
diff-tree c9ab5b6fb67ab93c215b282e85fffcebd609ebec (from dc95b4e98f99d69a22fa7427fe24f4d1c6439299)
Author: Jamey Sharp <jamey at minilop.net>
Date:   Wed Apr 19 16:49:32 2006 -0700

    Add XCBPollForReply and deprecate XCBGetRequestRead and XCBGetQueuedRequestRead.

diff --git a/xcb/src/xcb.h b/xcb/src/xcb.h
index 204164a..d4d02b4 100644
--- a/xcb/src/xcb.h
+++ b/xcb/src/xcb.h
@@ -273,9 +273,10 @@ XCBGenericEvent *XCBPollForEvent(XCBConn
  * processed. This function enables applications to determine whether
  * forcing a cookie is going to block.
  *
- * @todo review that function.
+ * @deprecated This function is deprecated in favor of XCBPollForReply.
+ * It must not be used in newly written code.
  */
-unsigned int XCBGetRequestRead(XCBConnection *c);
+unsigned int XCBGetRequestRead(XCBConnection *c) deprecated;
 
 
 /* xcb_ext.c */
diff --git a/xcb/src/xcb_in.c b/xcb/src/xcb_in.c
index 76f9702..4ad4654 100644
--- a/xcb/src/xcb_in.c
+++ b/xcb/src/xcb_in.c
@@ -239,6 +239,51 @@ static int read_block(const int fd, void
     return len;
 }
 
+static int poll_for_reply(XCBConnection *c, unsigned int request, void **reply, XCBGenericError **error)
+{
+    struct reply_list *head;
+
+    if(!request)
+        head = 0;
+    else if(c->in.request_completed >= request)
+    {
+        head = _xcb_map_remove(c->in.replies, request);
+        if(head && head->next)
+            _xcb_map_put(c->in.replies, request, head->next);
+    }
+    else if(c->in.request_read == request && c->in.current_reply)
+    {
+        head = c->in.current_reply;
+        c->in.current_reply = head->next;
+        if(!head->next)
+            c->in.current_reply_tail = &c->in.current_reply;
+    }
+    else
+        /* Would block: do nothing. */
+        return 0;
+
+    if(error)
+        *error = 0;
+    *reply = 0;
+
+    if(head)
+    {
+        if(((XCBGenericRep *) head->reply)->response_type == XCBError)
+        {
+            if(error)
+                *error = head->reply;
+            else
+                free(head->reply);
+        }
+        else
+            *reply = head->reply;
+
+        free(head);
+    }
+
+    return 1;
+}
+
 /* Public interface */
 
 void *XCBWaitForReply(XCBConnection *c, unsigned int request, XCBGenericError **e)
@@ -323,6 +368,16 @@ done:
     return ret;
 }
 
+int XCBPollForReply(XCBConnection *c, unsigned int request, void **reply, XCBGenericError **error)
+{
+    int ret;
+    assert(reply != 0);
+    pthread_mutex_lock(&c->iolock);
+    ret = poll_for_reply(c, request, reply, error);
+    pthread_mutex_unlock(&c->iolock);
+    return ret;
+}
+
 XCBGenericEvent *XCBWaitEvent(XCBConnection *c)
 {
     return XCBWaitForEvent(c);
diff --git a/xcb/src/xcbext.h b/xcb/src/xcbext.h
index 0d172e9..508ebf0 100644
--- a/xcb/src/xcbext.h
+++ b/xcb/src/xcbext.h
@@ -63,6 +63,7 @@ unsigned int XCBSendRequest(XCBConnectio
 /* xcb_in.c */
 
 void *XCBWaitForReply(XCBConnection *c, unsigned int request, XCBGenericError **e);
+int XCBPollForReply(XCBConnection *c, unsigned int request, void **reply, XCBGenericError **error);
 
 
 /* xcb_xid.c */
diff --git a/xcb/src/xcbxlib.h b/xcb/src/xcbxlib.h
index 462e2e3..4ceb03e 100644
--- a/xcb/src/xcbxlib.h
+++ b/xcb/src/xcbxlib.h
@@ -32,7 +32,7 @@
 #include "xcb.h"
 
 /* This function must be called with the IOLock held. */
-unsigned int XCBGetQueuedRequestRead(XCBConnection *c);
+unsigned int XCBGetQueuedRequestRead(XCBConnection *c) deprecated;
 
 /* This function must be called with the IOLock held. */
 unsigned int XCBGetRequestSent(XCBConnection *c);


More information about the xcb-commit mailing list