Merge branch 'master' of git+ssh://git.freedesktop.org/git/xcb
authorJamey Sharp <jamey@minilop.net>
Sun, 12 Mar 2006 21:36:33 +0000 (13:36 -0800)
committerJamey Sharp <jamey@minilop.net>
Sun, 12 Mar 2006 21:36:33 +0000 (13:36 -0800)
src/xcb_conn.c
src/xcb_in.c
src/xcb_out.c
src/xcbint.h

index 2fed0ba..51f8eaa 100644 (file)
@@ -33,6 +33,7 @@
 #include <netinet/in.h>
 #include <sys/select.h>
 #include <sys/fcntl.h>
+#include <errno.h>
 
 #include "xcb.h"
 #include "xcbint.h"
@@ -89,8 +90,10 @@ static int write_setup(XCBConnection *c, XCBAuthInfo *auth_info)
     assert(count <= sizeof(parts) / sizeof(*parts));
 
     pthread_mutex_lock(&c->iolock);
-    _xcb_out_write_block(c, parts, count);
-    ret = _xcb_out_flush(c);
+    {
+        struct iovec *parts_ptr = parts;
+        ret = _xcb_out_send(c, &parts_ptr, &count);
+    }
     pthread_mutex_unlock(&c->iolock);
     return ret;
 }
@@ -136,6 +139,34 @@ static int read_setup(XCBConnection *c)
     return 1;
 }
 
+/* precondition: there must be something for us to write. */
+static int write_vec(XCBConnection *c, struct iovec **vector, int *count)
+{
+    int n;
+    assert(!c->out.queue_len);
+    n = writev(c->fd, *vector, *count);
+    if(n < 0 && errno == EAGAIN)
+        return 1;
+    if(n <= 0)
+        return 0;
+
+    for(; *count; --*count, ++*vector)
+    {
+        int cur = (*vector)->iov_len;
+        if(cur > n)
+            cur = n;
+        (*vector)->iov_len -= cur;
+        (*vector)->iov_base = (char *) (*vector)->iov_base + cur;
+        n -= cur;
+        if((*vector)->iov_len)
+            break;
+    }
+    if(!*count)
+        *vector = 0;
+    assert(n == 0);
+    return 1;
+}
+
 /* Public interface */
 
 XCBConnSetupSuccessRep *XCBGetSetup(XCBConnection *c)
@@ -198,13 +229,13 @@ void XCBDisconnect(XCBConnection *c)
 
 /* Private interface */
 
-int _xcb_conn_wait(XCBConnection *c, const int should_write, pthread_cond_t *cond)
+int _xcb_conn_wait(XCBConnection *c, pthread_cond_t *cond, struct iovec **vector, int *count)
 {
     int ret;
     fd_set rfds, wfds;
 
     /* If the thing I should be doing is already being done, wait for it. */
-    if(should_write ? c->out.writing : c->in.reading)
+    if(count ? c->out.writing : c->in.reading)
     {
         pthread_cond_wait(cond, &c->iolock);
         return 1;
@@ -215,7 +246,7 @@ int _xcb_conn_wait(XCBConnection *c, const int should_write, pthread_cond_t *con
     ++c->in.reading;
 
     FD_ZERO(&wfds);
-    if(should_write)
+    if(count)
     {
         FD_SET(c->fd, &wfds);
         ++c->out.writing;
@@ -231,10 +262,10 @@ int _xcb_conn_wait(XCBConnection *c, const int should_write, pthread_cond_t *con
             ret = ret && _xcb_in_read(c);
 
         if(FD_ISSET(c->fd, &wfds))
-            ret = ret && _xcb_out_write(c);
+            ret = ret && write_vec(c, vector, count);
     }
 
-    if(should_write)
+    if(count)
         --c->out.writing;
     --c->in.reading;
 
index 4661bc4..fa13e90 100644 (file)
@@ -256,9 +256,8 @@ void *XCBWaitForReply(XCBConnection *c, unsigned int request, XCBGenericError **
     pthread_mutex_lock(&c->iolock);
 
     /* If this request has not been written yet, write it. */
-    if((signed int) (c->out.request_written - request) < 0)
-        if(!_xcb_out_flush(c))
-            goto done; /* error */
+    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)
@@ -273,7 +272,7 @@ void *XCBWaitForReply(XCBConnection *c, unsigned int request, XCBGenericError **
      * wait for one. */
     while(c->in.request_completed < request &&
             !(c->in.request_read == request && c->in.current_reply))
-        if(!_xcb_conn_wait(c, /*should_write*/ 0, &cond))
+        if(!_xcb_conn_wait(c, &cond, 0, 0))
             goto done;
 
     if(c->in.request_read != request)
@@ -333,7 +332,7 @@ XCBGenericEvent *XCBWaitForEvent(XCBConnection *c)
     pthread_mutex_lock(&c->iolock);
     /* get_event returns 0 on empty list. */
     while(!(ret = get_event(c)))
-        if(!_xcb_conn_wait(c, /*should_write*/ 0, &c->in.event_cond))
+        if(!_xcb_conn_wait(c, &c->in.event_cond, 0, 0))
             break;
 
     wake_up_next_reader(c);
index 10ef775..eb41e59 100644 (file)
 #include <stdlib.h>
 #include <unistd.h>
 #include <string.h>
-#include <errno.h>
 
 #include "xcb.h"
 #include "xcbext.h"
 #include "xcbint.h"
 #include "extensions/bigreq.h"
 
+static int write_block(XCBConnection *c, struct iovec *vector, int count)
+{
+    while(count && c->out.queue_len + vector[0].iov_len <= sizeof(c->out.queue))
+    {
+        memcpy(c->out.queue + c->out.queue_len, vector[0].iov_base, vector[0].iov_len);
+        c->out.queue_len += vector[0].iov_len;
+        vector[0].iov_base = (char *) vector[0].iov_base + vector[0].iov_len;
+        vector[0].iov_len = 0;
+        ++vector, --count;
+    }
+    if(!count)
+        return 1;
+
+    --vector, ++count;
+    vector[0].iov_base = c->out.queue;
+    vector[0].iov_len = c->out.queue_len;
+    c->out.queue_len = 0;
+    return _xcb_out_send(c, &vector, &count);
+}
+
 /* Public interface */
 
 CARD32 XCBGetMaximumRequestLength(XCBConnection *c)
@@ -163,7 +182,7 @@ unsigned int XCBSendRequest(XCBConnection *c, int flags, struct iovec *vector, c
         vector[0].iov_base = prefix + !prefix[0];
     }
 
-    if(!_xcb_out_write_block(c, vector, veclen))
+    if(!write_block(c, vector, veclen))
         request = 0;
     pthread_mutex_unlock(&c->iolock);
     return request;
@@ -173,7 +192,7 @@ int XCBFlush(XCBConnection *c)
 {
     int ret;
     pthread_mutex_lock(&c->iolock);
-    ret = _xcb_out_flush(c);
+    ret = _xcb_out_flush_to(c, c->out.request);
     pthread_mutex_unlock(&c->iolock);
     return ret;
 }
@@ -187,8 +206,6 @@ int _xcb_out_init(_xcb_out *out)
     out->writing = 0;
 
     out->queue_len = 0;
-    out->vec = 0;
-    out->vec_len = 0;
 
     out->request = 0;
     out->request_written = 0;
@@ -206,74 +223,32 @@ void _xcb_out_destroy(_xcb_out *out)
     pthread_mutex_destroy(&out->reqlenlock);
 }
 
-/* precondition: there must be something for us to write. */
-int _xcb_out_write(XCBConnection *c)
+int _xcb_out_send(XCBConnection *c, struct iovec **vector, int *count)
 {
-    int n;
-    assert(!c->out.queue_len);
-    n = writev(c->fd, c->out.vec, c->out.vec_len);
-    if(n < 0 && errno == EAGAIN)
-        return 1;
-    if(n <= 0)
-        return 0;
-
-    for(; c->out.vec_len; --c->out.vec_len, ++c->out.vec)
-    {
-        int cur = c->out.vec->iov_len;
-        if(cur > n)
-            cur = n;
-        c->out.vec->iov_len -= cur;
-        c->out.vec->iov_base = (char *) c->out.vec->iov_base + cur;
-        n -= cur;
-        if(c->out.vec->iov_len)
-            break;
-    }
-    if(!c->out.vec_len)
-        c->out.vec = 0;
-    assert(n == 0);
-    return 1;
+    int ret = 1;
+    while(ret && *count)
+        ret = _xcb_conn_wait(c, &c->out.cond, vector, count);
+    c->out.request_written = c->out.request;
+    pthread_cond_broadcast(&c->out.cond);
+    return ret;
 }
 
-int _xcb_out_write_block(XCBConnection *c, struct iovec *vector, size_t count)
+int _xcb_out_flush_to(XCBConnection *c, unsigned int request)
 {
-    assert(!c->out.vec && !c->out.vec_len);
-    while(count && c->out.queue_len + vector[0].iov_len < sizeof(c->out.queue))
-    {
-        memcpy(c->out.queue + c->out.queue_len, vector[0].iov_base, vector[0].iov_len);
-        c->out.queue_len += vector[0].iov_len;
-        vector[0].iov_base = (char *) vector[0].iov_base + vector[0].iov_len;
-        vector[0].iov_len = 0;
-        ++vector, --count;
-    }
-    if(!count)
+    assert(request <= c->out.request);
+    if(c->out.request_written >= request)
         return 1;
-
-    --vector, ++count;
-    vector[0].iov_base = c->out.queue;
-    vector[0].iov_len = c->out.queue_len;
-    c->out.queue_len = 0;
-
-    c->out.vec_len = count;
-    c->out.vec = vector;
-    return _xcb_out_flush(c);
-}
-
-int _xcb_out_flush(XCBConnection *c)
-{
-    int ret = 1;
-    struct iovec vec;
     if(c->out.queue_len)
     {
-        assert(!c->out.vec && !c->out.vec_len);
+        struct iovec vec, *vec_ptr = &vec;
+        int count = 1;
         vec.iov_base = c->out.queue;
         vec.iov_len = c->out.queue_len;
-        c->out.vec = &vec;
-        c->out.vec_len = 1;
         c->out.queue_len = 0;
+        return _xcb_out_send(c, &vec_ptr, &count);
     }
-    while(ret && c->out.vec_len)
-        ret = _xcb_conn_wait(c, /*should_write*/ 1, &c->out.cond);
-    c->out.request_written = c->out.request;
-    pthread_cond_broadcast(&c->out.cond);
-    return ret;
+    while(c->out.writing)
+        pthread_cond_wait(&c->out.cond, &c->iolock);
+    assert(c->out.request_written >= request);
+    return 1;
 }
index 7c8f331..7b32248 100644 (file)
@@ -63,8 +63,6 @@ typedef struct _xcb_out {
 
     char queue[4096];
     int queue_len;
-    struct iovec *vec;
-    int vec_len;
 
     unsigned int request;
     unsigned int request_written;
@@ -76,9 +74,8 @@ typedef struct _xcb_out {
 int _xcb_out_init(_xcb_out *out);
 void _xcb_out_destroy(_xcb_out *out);
 
-int _xcb_out_write(XCBConnection *c);
-int _xcb_out_write_block(XCBConnection *c, struct iovec *vector, size_t count);
-int _xcb_out_flush(XCBConnection *c);
+int _xcb_out_send(XCBConnection *c, struct iovec **vector, int *count);
+int _xcb_out_flush_to(XCBConnection *c, unsigned int request);
 
 
 /* xcb_in.c */
@@ -157,7 +154,7 @@ struct XCBConnection {
     _xcb_xid xid;
 };
 
-int _xcb_conn_wait(XCBConnection *c, const int should_write, pthread_cond_t *cond);
+int _xcb_conn_wait(XCBConnection *c, pthread_cond_t *cond, struct iovec **vector, int *count);
 
 #ifdef GCC_HAS_VISIBILITY
 #pragma GCC visibility pop