+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);
+ }
+}
+