[Xcb-commit] 2 commits - src

Jamey Sharp jamey at kemper.freedesktop.org
Wed Oct 4 17:18:36 PDT 2006


 src/xcb_conn.c |   38 ++++++++++++++++++++++++++++++++++----
 src/xcb_in.c   |   16 ++++++++--------
 src/xcb_out.c  |    8 ++++----
 src/xcb_xlib.c |   21 +++++++++++++++++++++
 src/xcbint.h   |   16 ++++++++++++++++
 src/xcbxlib.h  |    3 +++
 6 files changed, 86 insertions(+), 16 deletions(-)

New commits:
diff-tree 40589db8124b8c72894deb86a825c6117b0a2cd2 (from 57b0cd8fea498a32ff2322583c7278d5e86aa4e8)
Author: Jamey Sharp <jamey at minilop.net>
Date:   Wed Oct 4 15:01:00 2006 -0700

    Add xcb_xlib_lock and xcb_xlib_unlock, a special-purpose two-level recursive lock just for libX11.

diff --git a/src/xcb_conn.c b/src/xcb_conn.c
index 3d18369..239d71b 100644
--- a/src/xcb_conn.c
+++ b/src/xcb_conn.c
@@ -59,6 +59,18 @@ static int set_fd_flags(const int fd)
     return 1;
 }
 
+static int _xcb_xlib_init(_xcb_xlib *xlib)
+{
+    xlib->lock = 0;
+    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 +227,7 @@ xcb_connection_t *xcb_connect_to_fd(int 
     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 +252,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 +272,12 @@ void _xcb_conn_shutdown(xcb_connection_t
 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)
diff --git a/src/xcb_xlib.c b/src/xcb_xlib.c
index 5e56426..f352ca2 100644
--- a/src/xcb_xlib.c
+++ b/src/xcb_xlib.c
@@ -26,6 +26,8 @@
 #include "xcbxlib.h"
 #include "xcbint.h"
 
+#include <assert.h>
+
 unsigned int xcb_get_request_sent(xcb_connection_t *c)
 {
     if(c->has_error)
@@ -39,3 +41,22 @@ pthread_mutex_t *xcb_get_io_lock(xcb_con
         return 0;
     return &c->iolock;
 }
+
+void xcb_xlib_lock(xcb_connection_t *c)
+{
+    _xcb_lock_io(c);
+    assert(!c->xlib.lock);
+    c->xlib.lock = 1;
+    c->xlib.thread = pthread_self();
+    _xcb_unlock_io(c);
+}
+
+void xcb_xlib_unlock(xcb_connection_t *c)
+{
+    _xcb_lock_io(c);
+    assert(c->xlib.lock);
+    assert(pthread_equal(c->xlib.thread, pthread_self()));
+    c->xlib.lock = 0;
+    pthread_cond_broadcast(&c->xlib.cond);
+    _xcb_unlock_io(c);
+}
diff --git a/src/xcbint.h b/src/xcbint.h
index 1dc6f93..d81e787 100644
--- a/src/xcbint.h
+++ b/src/xcbint.h
@@ -113,6 +113,15 @@ int _xcb_in_read(xcb_connection_t *c);
 int _xcb_in_read_block(xcb_connection_t *c, void *buf, int nread);
 
 
+/* xcb_xlib.c */
+
+typedef struct _xcb_xlib {
+    int lock;
+    pthread_t thread;
+    pthread_cond_t cond;
+} _xcb_xlib;
+
+
 /* xcb_xid.c */
 
 typedef struct _xcb_xid {
@@ -150,6 +159,7 @@ struct xcb_connection_t {
 
     /* I/O data */
     pthread_mutex_t iolock;
+    _xcb_xlib xlib;
     _xcb_in in;
     _xcb_out out;
 
@@ -159,8 +169,6 @@ struct xcb_connection_t {
 };
 
 void _xcb_conn_shutdown(xcb_connection_t *c);
-void _xcb_lock_io(xcb_connection_t *c);
-void _xcb_unlock_io(xcb_connection_t *c);
 int _xcb_conn_wait(xcb_connection_t *c, pthread_cond_t *cond, struct iovec **vector, int *count);
 
 
@@ -172,4 +180,10 @@ int _xcb_get_auth_info(int fd, xcb_auth_
 #pragma GCC visibility pop
 #endif
 
+
+/* xcb_conn.c symbols visible to xcb-xlib */
+
+void _xcb_lock_io(xcb_connection_t *c);
+void _xcb_unlock_io(xcb_connection_t *c);
+
 #endif
diff --git a/src/xcbxlib.h b/src/xcbxlib.h
index 7e050c2..da4a044 100644
--- a/src/xcbxlib.h
+++ b/src/xcbxlib.h
@@ -36,4 +36,7 @@ unsigned int xcb_get_request_sent(xcb_co
 
 pthread_mutex_t *xcb_get_io_lock(xcb_connection_t *c);
 
+void xcb_xlib_lock(xcb_connection_t *c);
+void xcb_xlib_unlock(xcb_connection_t *c);
+
 #endif
diff-tree 57b0cd8fea498a32ff2322583c7278d5e86aa4e8 (from e7f473afbd02c87cc6b1fc9c7c240d6c5cc26763)
Author: Jamey Sharp <jamey at minilop.net>
Date:   Wed Oct 4 12:23:45 2006 -0700

    Factor out pthread_mutex_lock and unlock calls for the iolock.

diff --git a/src/xcb_conn.c b/src/xcb_conn.c
index 9aa7cdf..3d18369 100644
--- a/src/xcb_conn.c
+++ b/src/xcb_conn.c
@@ -97,12 +97,12 @@ static int write_setup(xcb_connection_t 
     }
     assert(count <= sizeof(parts) / sizeof(*parts));
 
-    pthread_mutex_lock(&c->iolock);
+    _xcb_lock_io(c);
     {
         struct iovec *parts_ptr = parts;
         ret = _xcb_out_send(c, &parts_ptr, &count);
     }
-    pthread_mutex_unlock(&c->iolock);
+    _xcb_unlock_io(c);
     return ret;
 }
 
@@ -255,6 +255,16 @@ void _xcb_conn_shutdown(xcb_connection_t
     c->has_error = 1;
 }
 
+void _xcb_lock_io(xcb_connection_t *c)
+{
+    pthread_mutex_lock(&c->iolock);
+}
+
+void _xcb_unlock_io(xcb_connection_t *c)
+{
+    pthread_mutex_unlock(&c->iolock);
+}
+
 int _xcb_conn_wait(xcb_connection_t *c, pthread_cond_t *cond, struct iovec **vector, int *count)
 {
     int ret;
@@ -278,7 +288,7 @@ int _xcb_conn_wait(xcb_connection_t *c, 
         ++c->out.writing;
     }
 
-    pthread_mutex_unlock(&c->iolock);
+    _xcb_unlock_io(c);
     do {
 	ret = select(c->fd + 1, &rfds, &wfds, 0, 0);
     } while (ret == -1 && errno == EINTR);
@@ -287,7 +297,7 @@ int _xcb_conn_wait(xcb_connection_t *c, 
         _xcb_conn_shutdown(c);
 	ret = 0;
     }
-    pthread_mutex_lock(&c->iolock);
+    _xcb_lock_io(c);
 
     if(ret)
     {
diff --git a/src/xcb_in.c b/src/xcb_in.c
index 604faab..41764df 100644
--- a/src/xcb_in.c
+++ b/src/xcb_in.c
@@ -318,7 +318,7 @@ void *xcb_wait_for_reply(xcb_connection_
     if(c->has_error)
         return 0;
 
-    pthread_mutex_lock(&c->iolock);
+    _xcb_lock_io(c);
 
     /* If this request has not been written yet, write it. */
     if(_xcb_out_flush_to(c, request))
@@ -358,7 +358,7 @@ void *xcb_wait_for_reply(xcb_connection_
     }
 
     wake_up_next_reader(c);
-    pthread_mutex_unlock(&c->iolock);
+    _xcb_unlock_io(c);
     return ret;
 }
 
@@ -373,9 +373,9 @@ int xcb_poll_for_reply(xcb_connection_t 
         return 1; /* would not block */
     }
     assert(reply != 0);
-    pthread_mutex_lock(&c->iolock);
+    _xcb_lock_io(c);
     ret = poll_for_reply(c, request, reply, error);
-    pthread_mutex_unlock(&c->iolock);
+    _xcb_unlock_io(c);
     return ret;
 }
 
@@ -384,14 +384,14 @@ xcb_generic_event_t *xcb_wait_for_event(
     xcb_generic_event_t *ret;
     if(c->has_error)
         return 0;
-    pthread_mutex_lock(&c->iolock);
+    _xcb_lock_io(c);
     /* get_event returns 0 on empty list. */
     while(!(ret = get_event(c)))
         if(!_xcb_conn_wait(c, &c->in.event_cond, 0, 0))
             break;
 
     wake_up_next_reader(c);
-    pthread_mutex_unlock(&c->iolock);
+    _xcb_unlock_io(c);
     return ret;
 }
 
@@ -401,12 +401,12 @@ xcb_generic_event_t *xcb_poll_for_event(
     {
         xcb_generic_event_t *ret = 0;
         int success;
-        pthread_mutex_lock(&c->iolock);
+        _xcb_lock_io(c);
         /* FIXME: follow X meets Z architecture changes. */
         success = _xcb_in_read(c);
         if(success)
             ret = get_event(c);
-        pthread_mutex_unlock(&c->iolock);
+        _xcb_unlock_io(c);
         if(success)
         {
             if(error)
diff --git a/src/xcb_out.c b/src/xcb_out.c
index 162abd4..74787e3 100644
--- a/src/xcb_out.c
+++ b/src/xcb_out.c
@@ -163,7 +163,7 @@ unsigned int xcb_send_request(xcb_connec
         workaround = WORKAROUND_GLX_GET_FB_CONFIGS_BUG;
 
     /* get a sequence number and arrange for delivery. */
-    pthread_mutex_lock(&c->iolock);
+    _xcb_lock_io(c);
     /* wait for other writing threads to get out of my way. */
     while(c->out.writing)
         pthread_cond_wait(&c->out.cond, &c->iolock);
@@ -207,7 +207,7 @@ unsigned int xcb_send_request(xcb_connec
         _xcb_conn_shutdown(c);
         request = 0;
     }
-    pthread_mutex_unlock(&c->iolock);
+    _xcb_unlock_io(c);
     return request;
 }
 
@@ -216,9 +216,9 @@ int xcb_flush(xcb_connection_t *c)
     int ret;
     if(c->has_error)
         return 0;
-    pthread_mutex_lock(&c->iolock);
+    _xcb_lock_io(c);
     ret = _xcb_out_flush_to(c, c->out.request);
-    pthread_mutex_unlock(&c->iolock);
+    _xcb_unlock_io(c);
     return ret;
 }
 
diff --git a/src/xcbint.h b/src/xcbint.h
index 1a71696..1dc6f93 100644
--- a/src/xcbint.h
+++ b/src/xcbint.h
@@ -159,6 +159,8 @@ struct xcb_connection_t {
 };
 
 void _xcb_conn_shutdown(xcb_connection_t *c);
+void _xcb_lock_io(xcb_connection_t *c);
+void _xcb_unlock_io(xcb_connection_t *c);
 int _xcb_conn_wait(xcb_connection_t *c, pthread_cond_t *cond, struct iovec **vector, int *count);
 
 


More information about the xcb-commit mailing list