- while (c->out.return_socket) {
- /* we are about to release the lock,
- so make a copy of the current status */
- xcb_return_socket_func_t return_socket = c->out.return_socket;
- void *socket_closure = c->out.socket_closure;
- int socket_seq = c->out.socket_seq;
-
- pthread_mutex_unlock(&c->iolock);
- return_socket(socket_closure);
- pthread_mutex_lock(&c->iolock);
-
- /* make sure nobody else has acquired the socket */
- if (socket_seq == c->out.socket_seq) {
- c->out.return_socket = 0;
- c->out.socket_closure = 0;
- _xcb_in_replies_done(c);
- }
+ while(c->out.return_socket && c->out.socket_moving)
+ pthread_cond_wait(&c->out.socket_cond, &c->iolock);
+ if(!c->out.return_socket)
+ return;
+
+ c->out.socket_moving = 1;
+ pthread_mutex_unlock(&c->iolock);
+ c->out.return_socket(c->out.socket_closure);
+ pthread_mutex_lock(&c->iolock);
+ c->out.socket_moving = 0;
+
+ pthread_cond_broadcast(&c->out.socket_cond);
+ c->out.return_socket = 0;
+ c->out.socket_closure = 0;
+ _xcb_in_replies_done(c);
+}
+
+static void prepare_socket_request(xcb_connection_t *c)
+{
+ /* We're about to append data to out.queue, so we need to
+ * atomically test for an external socket owner *and* some other
+ * thread currently writing.
+ *
+ * If we have an external socket owner, we have to get the socket back
+ * before we can use it again.
+ *
+ * If some other thread is writing to the socket, we assume it's
+ * writing from out.queue, and so we can't stick data there.
+ *
+ * We satisfy this condition by first calling get_socket_back
+ * (which may drop the lock, but will return when XCB owns the
+ * socket again) and then checking for another writing thread and
+ * escaping the loop if we're ready to go.
+ */
+ for (;;) {
+ if(c->has_error)
+ return;
+ get_socket_back(c);
+ if (!c->out.writing)
+ break;
+ pthread_cond_wait(&c->out.cond, &c->iolock);