Move request_written update back where it was for now: doing it early can cause XCBWa...
[free-sw/xcb/libxcb] / src / xcb_out.c
index 62d7053..b9595af 100644 (file)
@@ -27,6 +27,7 @@
 
 #include <assert.h>
 #include <stdlib.h>
+#include <unistd.h>
 #include <string.h>
 #include <errno.h>
 
@@ -47,6 +48,40 @@ static int force_sequence_wrap(XCBConnection *c)
     return ret;
 }
 
+static int _xcb_write(const int fd, char (*buf)[], int *count)
+{
+    int n = write(fd, *buf, *count);
+    if(n > 0)
+    {
+        *count -= n;
+        if(*count)
+            memmove(*buf, *buf + n, *count);
+    }
+    return n;
+}
+
+static int _xcb_writev(const int fd, struct iovec *vec, int count)
+{
+    int n = writev(fd, vec, count);
+    if(n > 0)
+    {
+        int rem = n;
+        for(; count; --count, ++vec)
+        {
+            int cur = vec->iov_len;
+            if(cur > rem)
+                cur = rem;
+            vec->iov_len -= cur;
+            vec->iov_base = (char *) vec->iov_base + cur;
+            rem -= cur;
+            if(vec->iov_len)
+                break;
+        }
+        assert(rem == 0);
+    }
+    return n;
+}
+
 /* Public interface */
 
 CARD32 XCBGetMaximumRequestLength(XCBConnection *c)
@@ -270,9 +305,9 @@ int _xcb_out_write_block(XCBConnection *c, struct iovec *vector, size_t count)
 int _xcb_out_flush(XCBConnection *c)
 {
     int ret = 1;
-    c->out.request_written = c->out.request;
     while(ret && (c->out.queue_len || 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;
 }