1602e473dSmrg/* Copyright (C) 2001-2004 Bart Massey and Jamey Sharp.
2602e473dSmrg *
3602e473dSmrg * Permission is hereby granted, free of charge, to any person obtaining a
4602e473dSmrg * copy of this software and associated documentation files (the "Software"),
5602e473dSmrg * to deal in the Software without restriction, including without limitation
6602e473dSmrg * the rights to use, copy, modify, merge, publish, distribute, sublicense,
7602e473dSmrg * and/or sell copies of the Software, and to permit persons to whom the
8602e473dSmrg * Software is furnished to do so, subject to the following conditions:
9602e473dSmrg *
10602e473dSmrg * The above copyright notice and this permission notice shall be included in
11602e473dSmrg * all copies or substantial portions of the Software.
12602e473dSmrg *
13602e473dSmrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14602e473dSmrg * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15602e473dSmrg * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16602e473dSmrg * AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
17602e473dSmrg * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
18602e473dSmrg * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
19602e473dSmrg *
20602e473dSmrg * Except as contained in this notice, the names of the authors or their
21602e473dSmrg * institutions shall not be used in advertising or otherwise to promote the
22602e473dSmrg * sale, use or other dealings in this Software without prior written
23602e473dSmrg * authorization from the authors.
24602e473dSmrg */
25602e473dSmrg
26602e473dSmrg/* Stuff that sends stuff to the server. */
27602e473dSmrg
2821298544Smrg#ifdef HAVE_CONFIG_H
2921298544Smrg#include "config.h"
3021298544Smrg#endif
3121298544Smrg
32602e473dSmrg#include <assert.h>
33602e473dSmrg#include <stdlib.h>
348ffb90f1Smrg#ifdef _WIN32
358ffb90f1Smrg#include <io.h>
368ffb90f1Smrg#else
37602e473dSmrg#include <unistd.h>
388ffb90f1Smrg#endif
39602e473dSmrg#include <string.h>
40602e473dSmrg
41602e473dSmrg#include "xcb.h"
42602e473dSmrg#include "xcbext.h"
43602e473dSmrg#include "xcbint.h"
44602e473dSmrg#include "bigreq.h"
45602e473dSmrg
4621298544Smrgstatic inline void send_request(xcb_connection_t *c, int isvoid, enum workarounds workaround, int flags, struct iovec *vector, int count)
47602e473dSmrg{
4821298544Smrg    if(c->has_error)
4921298544Smrg        return;
5021298544Smrg
5121298544Smrg    ++c->out.request;
5221298544Smrg    if(!isvoid)
5321298544Smrg        c->in.request_expected = c->out.request;
5421298544Smrg    if(workaround != WORKAROUND_NONE || flags != 0)
5521298544Smrg        _xcb_in_expect_reply(c, c->out.request, workaround, flags);
5621298544Smrg
57602e473dSmrg    while(count && c->out.queue_len + vector[0].iov_len <= sizeof(c->out.queue))
58602e473dSmrg    {
59602e473dSmrg        memcpy(c->out.queue + c->out.queue_len, vector[0].iov_base, vector[0].iov_len);
60602e473dSmrg        c->out.queue_len += vector[0].iov_len;
61602e473dSmrg        vector[0].iov_base = (char *) vector[0].iov_base + vector[0].iov_len;
62602e473dSmrg        vector[0].iov_len = 0;
63602e473dSmrg        ++vector, --count;
64602e473dSmrg    }
65602e473dSmrg    if(!count)
6621298544Smrg        return;
67602e473dSmrg
68602e473dSmrg    --vector, ++count;
69602e473dSmrg    vector[0].iov_base = c->out.queue;
70602e473dSmrg    vector[0].iov_len = c->out.queue_len;
71602e473dSmrg    c->out.queue_len = 0;
7221298544Smrg    _xcb_out_send(c, vector, count);
7321298544Smrg}
7421298544Smrg
7521298544Smrgstatic void send_sync(xcb_connection_t *c)
7621298544Smrg{
7721298544Smrg    static const union {
7821298544Smrg        struct {
7921298544Smrg            uint8_t major;
8021298544Smrg            uint8_t pad;
8121298544Smrg            uint16_t len;
8221298544Smrg        } fields;
8321298544Smrg        uint32_t packet;
8421298544Smrg    } sync_req = { { /* GetInputFocus */ 43, 0, 1 } };
8521298544Smrg    struct iovec vector[2];
8621298544Smrg    vector[1].iov_base = (char *) &sync_req;
8721298544Smrg    vector[1].iov_len = sizeof(sync_req);
8821298544Smrg    send_request(c, 0, WORKAROUND_NONE, XCB_REQUEST_DISCARD_REPLY, vector + 1, 1);
89602e473dSmrg}
90602e473dSmrg
91602e473dSmrgstatic void get_socket_back(xcb_connection_t *c)
92602e473dSmrg{
93602e473dSmrg    while(c->out.return_socket && c->out.socket_moving)
94602e473dSmrg        pthread_cond_wait(&c->out.socket_cond, &c->iolock);
95602e473dSmrg    if(!c->out.return_socket)
96602e473dSmrg        return;
97602e473dSmrg
98602e473dSmrg    c->out.socket_moving = 1;
99602e473dSmrg    pthread_mutex_unlock(&c->iolock);
100602e473dSmrg    c->out.return_socket(c->out.socket_closure);
101602e473dSmrg    pthread_mutex_lock(&c->iolock);
102602e473dSmrg    c->out.socket_moving = 0;
103602e473dSmrg
104602e473dSmrg    pthread_cond_broadcast(&c->out.socket_cond);
105602e473dSmrg    c->out.return_socket = 0;
106602e473dSmrg    c->out.socket_closure = 0;
107602e473dSmrg    _xcb_in_replies_done(c);
108602e473dSmrg}
109602e473dSmrg
1101c7386f4Smrgstatic void prepare_socket_request(xcb_connection_t *c)
1111c7386f4Smrg{
1121c7386f4Smrg    /* We're about to append data to out.queue, so we need to
1131c7386f4Smrg     * atomically test for an external socket owner *and* some other
1141c7386f4Smrg     * thread currently writing.
1151c7386f4Smrg     *
1161c7386f4Smrg     * If we have an external socket owner, we have to get the socket back
1171c7386f4Smrg     * before we can use it again.
1181c7386f4Smrg     *
1191c7386f4Smrg     * If some other thread is writing to the socket, we assume it's
1201c7386f4Smrg     * writing from out.queue, and so we can't stick data there.
1211c7386f4Smrg     *
1221c7386f4Smrg     * We satisfy this condition by first calling get_socket_back
1231c7386f4Smrg     * (which may drop the lock, but will return when XCB owns the
1241c7386f4Smrg     * socket again) and then checking for another writing thread and
1251c7386f4Smrg     * escaping the loop if we're ready to go.
1261c7386f4Smrg     */
1271c7386f4Smrg    for (;;) {
1281c7386f4Smrg        if(c->has_error)
1291c7386f4Smrg            return;
1301c7386f4Smrg        get_socket_back(c);
1311c7386f4Smrg        if (!c->out.writing)
1321c7386f4Smrg            break;
1331c7386f4Smrg        pthread_cond_wait(&c->out.cond, &c->iolock);
1341c7386f4Smrg    }
1351c7386f4Smrg}
1361c7386f4Smrg
137602e473dSmrg/* Public interface */
138602e473dSmrg
139602e473dSmrgvoid xcb_prefetch_maximum_request_length(xcb_connection_t *c)
140602e473dSmrg{
141602e473dSmrg    if(c->has_error)
142602e473dSmrg        return;
143602e473dSmrg    pthread_mutex_lock(&c->out.reqlenlock);
144602e473dSmrg    if(c->out.maximum_request_length_tag == LAZY_NONE)
145602e473dSmrg    {
146602e473dSmrg        const xcb_query_extension_reply_t *ext;
147602e473dSmrg        ext = xcb_get_extension_data(c, &xcb_big_requests_id);
148602e473dSmrg        if(ext && ext->present)
149602e473dSmrg        {
150602e473dSmrg            c->out.maximum_request_length_tag = LAZY_COOKIE;
151602e473dSmrg            c->out.maximum_request_length.cookie = xcb_big_requests_enable(c);
152602e473dSmrg        }
153602e473dSmrg        else
154602e473dSmrg        {
155602e473dSmrg            c->out.maximum_request_length_tag = LAZY_FORCED;
156602e473dSmrg            c->out.maximum_request_length.value = c->setup->maximum_request_length;
157602e473dSmrg        }
158602e473dSmrg    }
159602e473dSmrg    pthread_mutex_unlock(&c->out.reqlenlock);
160602e473dSmrg}
161602e473dSmrg
162602e473dSmrguint32_t xcb_get_maximum_request_length(xcb_connection_t *c)
163602e473dSmrg{
164602e473dSmrg    if(c->has_error)
165602e473dSmrg        return 0;
166602e473dSmrg    xcb_prefetch_maximum_request_length(c);
167602e473dSmrg    pthread_mutex_lock(&c->out.reqlenlock);
168602e473dSmrg    if(c->out.maximum_request_length_tag == LAZY_COOKIE)
169602e473dSmrg    {
170602e473dSmrg        xcb_big_requests_enable_reply_t *r = xcb_big_requests_enable_reply(c, c->out.maximum_request_length.cookie, 0);
171602e473dSmrg        c->out.maximum_request_length_tag = LAZY_FORCED;
172602e473dSmrg        if(r)
173602e473dSmrg        {
174602e473dSmrg            c->out.maximum_request_length.value = r->maximum_request_length;
175602e473dSmrg            free(r);
176602e473dSmrg        }
177602e473dSmrg        else
178602e473dSmrg            c->out.maximum_request_length.value = c->setup->maximum_request_length;
179602e473dSmrg    }
180602e473dSmrg    pthread_mutex_unlock(&c->out.reqlenlock);
181602e473dSmrg    return c->out.maximum_request_length.value;
182602e473dSmrg}
183602e473dSmrg
1847204935cSmrgstatic void close_fds(int *fds, unsigned int num_fds)
1857204935cSmrg{
1867204935cSmrg    for (unsigned int index = 0; index < num_fds; index++)
1877204935cSmrg        close(fds[index]);
1887204935cSmrg}
1897204935cSmrg
1907204935cSmrgstatic void send_fds(xcb_connection_t *c, int *fds, unsigned int num_fds)
1917204935cSmrg{
1927204935cSmrg#if HAVE_SENDMSG
1937204935cSmrg    /* Calling _xcb_out_flush_to() can drop the iolock and wait on a condition
1947204935cSmrg     * variable if another thread is currently writing (c->out.writing > 0).
1957204935cSmrg     * This call waits for writers to be done and thus _xcb_out_flush_to() will
1967204935cSmrg     * do the work itself (in which case we are a writer and
1977204935cSmrg     * prepare_socket_request() will wait for us to be done if another threads
1987204935cSmrg     * tries to send fds, too). Thanks to this, we can atomically write out FDs.
1997204935cSmrg     */
2007204935cSmrg    prepare_socket_request(c);
2017204935cSmrg
2027204935cSmrg    while (num_fds > 0) {
2037204935cSmrg        while (c->out.out_fd.nfd == XCB_MAX_PASS_FD && !c->has_error) {
2047204935cSmrg            /* XXX: if c->out.writing > 0, this releases the iolock and
2057204935cSmrg             * potentially allows other threads to interfere with their own fds.
2067204935cSmrg             */
2077204935cSmrg            _xcb_out_flush_to(c, c->out.request);
2087204935cSmrg
2097204935cSmrg            if (c->out.out_fd.nfd == XCB_MAX_PASS_FD) {
2107204935cSmrg                /* We need some request to send FDs with */
2117204935cSmrg                _xcb_out_send_sync(c);
2127204935cSmrg            }
2137204935cSmrg        }
2147204935cSmrg        if (c->has_error)
2157204935cSmrg            break;
2167204935cSmrg
2177204935cSmrg        c->out.out_fd.fd[c->out.out_fd.nfd++] = fds[0];
2187204935cSmrg        fds++;
2197204935cSmrg        num_fds--;
2207204935cSmrg    }
2217204935cSmrg#endif
2227204935cSmrg    close_fds(fds, num_fds);
2237204935cSmrg}
2247204935cSmrg
2257204935cSmrguint64_t xcb_send_request_with_fds64(xcb_connection_t *c, int flags, struct iovec *vector,
2267204935cSmrg                const xcb_protocol_request_t *req, unsigned int num_fds, int *fds)
227602e473dSmrg{
228602e473dSmrg    uint64_t request;
22921298544Smrg    uint32_t prefix[2];
230602e473dSmrg    int veclen = req->count;
231602e473dSmrg    enum workarounds workaround = WORKAROUND_NONE;
232602e473dSmrg
2337204935cSmrg    if(c->has_error) {
2347204935cSmrg        close_fds(fds, num_fds);
235602e473dSmrg        return 0;
2367204935cSmrg    }
237602e473dSmrg
238602e473dSmrg    assert(c != 0);
239602e473dSmrg    assert(vector != 0);
240602e473dSmrg    assert(req->count > 0);
241602e473dSmrg
242602e473dSmrg    if(!(flags & XCB_REQUEST_RAW))
243602e473dSmrg    {
244602e473dSmrg        static const char pad[3];
245602e473dSmrg        unsigned int i;
246602e473dSmrg        uint16_t shortlen = 0;
247602e473dSmrg        size_t longlen = 0;
248602e473dSmrg        assert(vector[0].iov_len >= 4);
249602e473dSmrg        /* set the major opcode, and the minor opcode for extensions */
250602e473dSmrg        if(req->ext)
251602e473dSmrg        {
252602e473dSmrg            const xcb_query_extension_reply_t *extension = xcb_get_extension_data(c, req->ext);
253602e473dSmrg            if(!(extension && extension->present))
254602e473dSmrg            {
2557204935cSmrg                close_fds(fds, num_fds);
25621298544Smrg                _xcb_conn_shutdown(c, XCB_CONN_CLOSED_EXT_NOTSUPPORTED);
257602e473dSmrg                return 0;
258602e473dSmrg            }
259602e473dSmrg            ((uint8_t *) vector[0].iov_base)[0] = extension->major_opcode;
260602e473dSmrg            ((uint8_t *) vector[0].iov_base)[1] = req->opcode;
261602e473dSmrg        }
262602e473dSmrg        else
263602e473dSmrg            ((uint8_t *) vector[0].iov_base)[0] = req->opcode;
264602e473dSmrg
265602e473dSmrg        /* put together the length field, possibly using BIGREQUESTS */
266602e473dSmrg        for(i = 0; i < req->count; ++i)
267602e473dSmrg        {
268602e473dSmrg            longlen += vector[i].iov_len;
269602e473dSmrg            if(!vector[i].iov_base)
270602e473dSmrg            {
271602e473dSmrg                vector[i].iov_base = (char *) pad;
272602e473dSmrg                assert(vector[i].iov_len <= sizeof(pad));
273602e473dSmrg            }
274602e473dSmrg        }
275602e473dSmrg        assert((longlen & 3) == 0);
276602e473dSmrg        longlen >>= 2;
277602e473dSmrg
278602e473dSmrg        if(longlen <= c->setup->maximum_request_length)
279602e473dSmrg        {
280602e473dSmrg            /* we don't need BIGREQUESTS. */
281602e473dSmrg            shortlen = longlen;
282602e473dSmrg            longlen = 0;
283602e473dSmrg        }
284602e473dSmrg        else if(longlen > xcb_get_maximum_request_length(c))
285602e473dSmrg        {
2867204935cSmrg            close_fds(fds, num_fds);
28721298544Smrg            _xcb_conn_shutdown(c, XCB_CONN_CLOSED_REQ_LEN_EXCEED);
288602e473dSmrg            return 0; /* server can't take this; maybe need BIGREQUESTS? */
289602e473dSmrg        }
290602e473dSmrg
291602e473dSmrg        /* set the length field. */
292602e473dSmrg        ((uint16_t *) vector[0].iov_base)[1] = shortlen;
293602e473dSmrg        if(!shortlen)
29421298544Smrg        {
29521298544Smrg            prefix[0] = ((uint32_t *) vector[0].iov_base)[0];
29621298544Smrg            prefix[1] = ++longlen;
29721298544Smrg            vector[0].iov_base = (uint32_t *) vector[0].iov_base + 1;
29821298544Smrg            vector[0].iov_len -= sizeof(uint32_t);
29921298544Smrg            --vector, ++veclen;
30021298544Smrg            vector[0].iov_base = prefix;
30121298544Smrg            vector[0].iov_len = sizeof(prefix);
30221298544Smrg        }
303602e473dSmrg    }
304602e473dSmrg    flags &= ~XCB_REQUEST_RAW;
305602e473dSmrg
306602e473dSmrg    /* do we need to work around the X server bug described in glx.xml? */
307602e473dSmrg    /* XXX: GetFBConfigs won't use BIG-REQUESTS in any sane
308602e473dSmrg     * configuration, but that should be handled here anyway. */
309602e473dSmrg    if(req->ext && !req->isvoid && !strcmp(req->ext->name, "GLX") &&
310602e473dSmrg            ((req->opcode == 17 && ((uint32_t *) vector[0].iov_base)[1] == 0x10004) ||
311602e473dSmrg             req->opcode == 21))
312602e473dSmrg        workaround = WORKAROUND_GLX_GET_FB_CONFIGS_BUG;
313602e473dSmrg
314602e473dSmrg    /* get a sequence number and arrange for delivery. */
315602e473dSmrg    pthread_mutex_lock(&c->iolock);
3161c7386f4Smrg
3177204935cSmrg    /* send FDs before establishing a good request number, because this might
3187204935cSmrg     * call send_sync(), too
3197204935cSmrg     */
3207204935cSmrg    send_fds(c, fds, num_fds);
3217204935cSmrg
3221c7386f4Smrg    prepare_socket_request(c);
323602e473dSmrg
324602e473dSmrg    /* send GetInputFocus (sync_req) when 64k-2 requests have been sent without
3251c7386f4Smrg     * a reply.
3261c7386f4Smrg     * Also send sync_req (could use NoOp) at 32-bit wrap to avoid having
327602e473dSmrg     * applications see sequence 0 as that is used to indicate
3281c7386f4Smrg     * an error in sending the request
3291c7386f4Smrg     */
3307204935cSmrg
3311c7386f4Smrg    while ((req->isvoid && c->out.request == c->in.request_expected + (1 << 16) - 2) ||
3321c7386f4Smrg           (unsigned int) (c->out.request + 1) == 0)
3331c7386f4Smrg    {
33421298544Smrg        send_sync(c);
3351c7386f4Smrg        prepare_socket_request(c);
3361c7386f4Smrg    }
33721298544Smrg
33821298544Smrg    send_request(c, req->isvoid, workaround, flags, vector, veclen);
33921298544Smrg    request = c->has_error ? 0 : c->out.request;
340602e473dSmrg    pthread_mutex_unlock(&c->iolock);
341602e473dSmrg    return request;
342602e473dSmrg}
343602e473dSmrg
3447204935cSmrg/* request number are actually uint64_t internally but keep API compat with unsigned int */
3457204935cSmrgunsigned int xcb_send_request_with_fds(xcb_connection_t *c, int flags, struct iovec *vector,
3467204935cSmrg        const xcb_protocol_request_t *req, unsigned int num_fds, int *fds)
3477204935cSmrg{
3487204935cSmrg    return xcb_send_request_with_fds64(c, flags, vector, req, num_fds, fds);
3497204935cSmrg}
3507204935cSmrg
3517204935cSmrguint64_t xcb_send_request64(xcb_connection_t *c, int flags, struct iovec *vector, const xcb_protocol_request_t *req)
3527204935cSmrg{
3537204935cSmrg    return xcb_send_request_with_fds64(c, flags, vector, req, 0, NULL);
3547204935cSmrg}
3557204935cSmrg
356709d36bbSmrg/* request number are actually uint64_t internally but keep API compat with unsigned int */
357709d36bbSmrgunsigned int xcb_send_request(xcb_connection_t *c, int flags, struct iovec *vector, const xcb_protocol_request_t *req)
358709d36bbSmrg{
359709d36bbSmrg    return xcb_send_request64(c, flags, vector, req);
360709d36bbSmrg}
361709d36bbSmrg
3621016ad83Smrgvoid
3631016ad83Smrgxcb_send_fd(xcb_connection_t *c, int fd)
3641016ad83Smrg{
3657204935cSmrg    int fds[1] = { fd };
3667204935cSmrg
3677204935cSmrg    if (c->has_error) {
3687204935cSmrg        close(fd);
3691016ad83Smrg        return;
3701016ad83Smrg    }
3717204935cSmrg    pthread_mutex_lock(&c->iolock);
3727204935cSmrg    send_fds(c, &fds[0], 1);
3731016ad83Smrg    pthread_mutex_unlock(&c->iolock);
3741016ad83Smrg}
3751016ad83Smrg
376602e473dSmrgint xcb_take_socket(xcb_connection_t *c, void (*return_socket)(void *closure), void *closure, int flags, uint64_t *sent)
377602e473dSmrg{
378602e473dSmrg    int ret;
379602e473dSmrg    if(c->has_error)
380602e473dSmrg        return 0;
381602e473dSmrg    pthread_mutex_lock(&c->iolock);
382602e473dSmrg    get_socket_back(c);
38321298544Smrg
38421298544Smrg    /* _xcb_out_flush may drop the iolock allowing other threads to
38521298544Smrg     * write requests, so keep flushing until we're done
38621298544Smrg     */
38721298544Smrg    do
38808c70cfbSmrg        ret = _xcb_out_flush_to(c, c->out.request);
38921298544Smrg    while (ret && c->out.request != c->out.request_written);
390602e473dSmrg    if(ret)
391602e473dSmrg    {
392602e473dSmrg        c->out.return_socket = return_socket;
393602e473dSmrg        c->out.socket_closure = closure;
394fe12f63cSmrg        if(flags) {
395fe12f63cSmrg            /* c->out.request + 1 will be the first request sent by the external
396fe12f63cSmrg             * socket owner. If the socket is returned before this request is sent
397fe12f63cSmrg             * it will be detected in _xcb_in_replies_done and this pending_reply
398fe12f63cSmrg             * will be discarded.
399fe12f63cSmrg             */
400fe12f63cSmrg            _xcb_in_expect_reply(c, c->out.request + 1, WORKAROUND_EXTERNAL_SOCKET_OWNER, flags);
401fe12f63cSmrg        }
402602e473dSmrg        assert(c->out.request == c->out.request_written);
403602e473dSmrg        *sent = c->out.request;
404602e473dSmrg    }
405602e473dSmrg    pthread_mutex_unlock(&c->iolock);
406602e473dSmrg    return ret;
407602e473dSmrg}
408602e473dSmrg
409602e473dSmrgint xcb_writev(xcb_connection_t *c, struct iovec *vector, int count, uint64_t requests)
410602e473dSmrg{
411602e473dSmrg    int ret;
412602e473dSmrg    if(c->has_error)
413602e473dSmrg        return 0;
414602e473dSmrg    pthread_mutex_lock(&c->iolock);
415602e473dSmrg    c->out.request += requests;
416b20a2039Smrg    ret = _xcb_out_send(c, vector, count);
417602e473dSmrg    pthread_mutex_unlock(&c->iolock);
418602e473dSmrg    return ret;
419602e473dSmrg}
420602e473dSmrg
421602e473dSmrgint xcb_flush(xcb_connection_t *c)
422602e473dSmrg{
423602e473dSmrg    int ret;
424602e473dSmrg    if(c->has_error)
425602e473dSmrg        return 0;
426602e473dSmrg    pthread_mutex_lock(&c->iolock);
427602e473dSmrg    ret = _xcb_out_flush_to(c, c->out.request);
428602e473dSmrg    pthread_mutex_unlock(&c->iolock);
429602e473dSmrg    return ret;
430602e473dSmrg}
431602e473dSmrg
432602e473dSmrg/* Private interface */
433602e473dSmrg
434602e473dSmrgint _xcb_out_init(_xcb_out *out)
435602e473dSmrg{
436602e473dSmrg    if(pthread_cond_init(&out->socket_cond, 0))
437602e473dSmrg        return 0;
438602e473dSmrg    out->return_socket = 0;
439602e473dSmrg    out->socket_closure = 0;
440602e473dSmrg    out->socket_moving = 0;
441602e473dSmrg
442602e473dSmrg    if(pthread_cond_init(&out->cond, 0))
443602e473dSmrg        return 0;
444602e473dSmrg    out->writing = 0;
445602e473dSmrg
446602e473dSmrg    out->queue_len = 0;
447602e473dSmrg
448602e473dSmrg    out->request = 0;
449602e473dSmrg    out->request_written = 0;
4508ffb90f1Smrg    out->request_expected_written = 0;
451602e473dSmrg
452602e473dSmrg    if(pthread_mutex_init(&out->reqlenlock, 0))
453602e473dSmrg        return 0;
454602e473dSmrg    out->maximum_request_length_tag = LAZY_NONE;
455602e473dSmrg
456602e473dSmrg    return 1;
457602e473dSmrg}
458602e473dSmrg
459602e473dSmrgvoid _xcb_out_destroy(_xcb_out *out)
460602e473dSmrg{
461602e473dSmrg    pthread_mutex_destroy(&out->reqlenlock);
4628ffb90f1Smrg    pthread_cond_destroy(&out->cond);
4638ffb90f1Smrg    pthread_cond_destroy(&out->socket_cond);
464602e473dSmrg}
465602e473dSmrg
466b20a2039Smrgint _xcb_out_send(xcb_connection_t *c, struct iovec *vector, int count)
467602e473dSmrg{
468602e473dSmrg    int ret = 1;
469b20a2039Smrg    while(ret && count)
470b20a2039Smrg        ret = _xcb_conn_wait(c, &c->out.cond, &vector, &count);
471602e473dSmrg    c->out.request_written = c->out.request;
4728ffb90f1Smrg    c->out.request_expected_written = c->in.request_expected;
473602e473dSmrg    pthread_cond_broadcast(&c->out.cond);
474b20a2039Smrg    _xcb_in_wake_up_next_reader(c);
475602e473dSmrg    return ret;
476602e473dSmrg}
477602e473dSmrg
47821298544Smrgvoid _xcb_out_send_sync(xcb_connection_t *c)
47921298544Smrg{
4801c7386f4Smrg    prepare_socket_request(c);
48121298544Smrg    send_sync(c);
48221298544Smrg}
48321298544Smrg
484602e473dSmrgint _xcb_out_flush_to(xcb_connection_t *c, uint64_t request)
485602e473dSmrg{
486602e473dSmrg    assert(XCB_SEQUENCE_COMPARE(request, <=, c->out.request));
487602e473dSmrg    if(XCB_SEQUENCE_COMPARE(c->out.request_written, >=, request))
488602e473dSmrg        return 1;
489602e473dSmrg    if(c->out.queue_len)
490602e473dSmrg    {
491b20a2039Smrg        struct iovec vec;
492602e473dSmrg        vec.iov_base = c->out.queue;
493602e473dSmrg        vec.iov_len = c->out.queue_len;
494602e473dSmrg        c->out.queue_len = 0;
495b20a2039Smrg        return _xcb_out_send(c, &vec, 1);
496602e473dSmrg    }
497602e473dSmrg    while(c->out.writing)
498602e473dSmrg        pthread_cond_wait(&c->out.cond, &c->iolock);
499602e473dSmrg    assert(XCB_SEQUENCE_COMPARE(c->out.request_written, >=, request));
500602e473dSmrg    return 1;
501602e473dSmrg}
502