Factor pthread_cond_wait(iolock) to _xcb_wait_io.
[free-sw/xcb/libxcb] / src / xcb_conn.c
index 3d18369..9a58bff 100644 (file)
@@ -48,7 +48,7 @@ static const int error_connection = 1;
 
 static int set_fd_flags(const int fd)
 {
-    long flags = fcntl(fd, F_GETFL, 0);
+    int flags = fcntl(fd, F_GETFL, 0);
     if(flags == -1)
         return 0;
     flags |= O_NONBLOCK;
@@ -59,6 +59,21 @@ static int set_fd_flags(const int fd)
     return 1;
 }
 
+static int _xcb_xlib_init(_xcb_xlib *xlib)
+{
+    xlib->lock = 0;
+#ifndef NDEBUG
+    xlib->sloppy_lock = (getenv("LIBXCB_ALLOW_SLOPPY_LOCK") != 0);
+#endif
+    pthread_cond_init(&xlib->cond, 0);
+    return 1;
+}
+
+static void _xcb_xlib_destroy(_xcb_xlib *xlib)
+{
+    pthread_cond_destroy(&xlib->cond);
+}
+
 static int write_setup(xcb_connection_t *c, xcb_auth_info_t *auth_info)
 {
     static const char pad[3];
@@ -215,6 +230,7 @@ xcb_connection_t *xcb_connect_to_fd(int fd, xcb_auth_info_t *auth_info)
     if(!(
         set_fd_flags(fd) &&
         pthread_mutex_init(&c->iolock, 0) == 0 &&
+        _xcb_xlib_init(&c->xlib) &&
         _xcb_in_init(&c->in) &&
         _xcb_out_init(&c->out) &&
         write_setup(c, auth_info) &&
@@ -239,6 +255,7 @@ void xcb_disconnect(xcb_connection_t *c)
     close(c->fd);
 
     pthread_mutex_destroy(&c->iolock);
+    _xcb_xlib_destroy(&c->xlib);
     _xcb_in_destroy(&c->in);
     _xcb_out_destroy(&c->out);
 
@@ -258,6 +275,12 @@ void _xcb_conn_shutdown(xcb_connection_t *c)
 void _xcb_lock_io(xcb_connection_t *c)
 {
     pthread_mutex_lock(&c->iolock);
+    while(c->xlib.lock)
+    {
+        if(pthread_equal(c->xlib.thread, pthread_self()))
+            break;
+        pthread_cond_wait(&c->xlib.cond, &c->iolock);
+    }
 }
 
 void _xcb_unlock_io(xcb_connection_t *c)
@@ -265,6 +288,11 @@ void _xcb_unlock_io(xcb_connection_t *c)
     pthread_mutex_unlock(&c->iolock);
 }
 
+void _xcb_wait_io(xcb_connection_t *c, pthread_cond_t *cond)
+{
+    pthread_cond_wait(cond, &c->iolock);
+}
+
 int _xcb_conn_wait(xcb_connection_t *c, pthread_cond_t *cond, struct iovec **vector, int *count)
 {
     int ret;
@@ -273,7 +301,7 @@ int _xcb_conn_wait(xcb_connection_t *c, pthread_cond_t *cond, struct iovec **vec
     /* If the thing I should be doing is already being done, wait for it. */
     if(count ? c->out.writing : c->in.reading)
     {
-        pthread_cond_wait(cond, &c->iolock);
+        _xcb_wait_io(c, cond);
         return 1;
     }