xcb_out.c revision fe12f63c
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>
34602e473dSmrg#include <unistd.h>
35602e473dSmrg#include <string.h>
36602e473dSmrg
37602e473dSmrg#include "xcb.h"
38602e473dSmrg#include "xcbext.h"
39602e473dSmrg#include "xcbint.h"
40602e473dSmrg#include "bigreq.h"
41602e473dSmrg
4221298544Smrgstatic inline void send_request(xcb_connection_t *c, int isvoid, enum workarounds workaround, int flags, struct iovec *vector, int count)
43602e473dSmrg{
4421298544Smrg    if(c->has_error)
4521298544Smrg        return;
4621298544Smrg
4721298544Smrg    ++c->out.request;
4821298544Smrg    if(!isvoid)
4921298544Smrg        c->in.request_expected = c->out.request;
5021298544Smrg    if(workaround != WORKAROUND_NONE || flags != 0)
5121298544Smrg        _xcb_in_expect_reply(c, c->out.request, workaround, flags);
5221298544Smrg
53602e473dSmrg    while(count && c->out.queue_len + vector[0].iov_len <= sizeof(c->out.queue))
54602e473dSmrg    {
55602e473dSmrg        memcpy(c->out.queue + c->out.queue_len, vector[0].iov_base, vector[0].iov_len);
56602e473dSmrg        c->out.queue_len += vector[0].iov_len;
57602e473dSmrg        vector[0].iov_base = (char *) vector[0].iov_base + vector[0].iov_len;
58602e473dSmrg        vector[0].iov_len = 0;
59602e473dSmrg        ++vector, --count;
60602e473dSmrg    }
61602e473dSmrg    if(!count)
6221298544Smrg        return;
63602e473dSmrg
64602e473dSmrg    --vector, ++count;
65602e473dSmrg    vector[0].iov_base = c->out.queue;
66602e473dSmrg    vector[0].iov_len = c->out.queue_len;
67602e473dSmrg    c->out.queue_len = 0;
6821298544Smrg    _xcb_out_send(c, vector, count);
6921298544Smrg}
7021298544Smrg
7121298544Smrgstatic void send_sync(xcb_connection_t *c)
7221298544Smrg{
7321298544Smrg    static const union {
7421298544Smrg        struct {
7521298544Smrg            uint8_t major;
7621298544Smrg            uint8_t pad;
7721298544Smrg            uint16_t len;
7821298544Smrg        } fields;
7921298544Smrg        uint32_t packet;
8021298544Smrg    } sync_req = { { /* GetInputFocus */ 43, 0, 1 } };
8121298544Smrg    struct iovec vector[2];
8221298544Smrg    vector[1].iov_base = (char *) &sync_req;
8321298544Smrg    vector[1].iov_len = sizeof(sync_req);
8421298544Smrg    send_request(c, 0, WORKAROUND_NONE, XCB_REQUEST_DISCARD_REPLY, vector + 1, 1);
85602e473dSmrg}
86602e473dSmrg
87602e473dSmrgstatic void get_socket_back(xcb_connection_t *c)
88602e473dSmrg{
89602e473dSmrg    while(c->out.return_socket && c->out.socket_moving)
90602e473dSmrg        pthread_cond_wait(&c->out.socket_cond, &c->iolock);
91602e473dSmrg    if(!c->out.return_socket)
92602e473dSmrg        return;
93602e473dSmrg
94602e473dSmrg    c->out.socket_moving = 1;
95602e473dSmrg    pthread_mutex_unlock(&c->iolock);
96602e473dSmrg    c->out.return_socket(c->out.socket_closure);
97602e473dSmrg    pthread_mutex_lock(&c->iolock);
98602e473dSmrg    c->out.socket_moving = 0;
99602e473dSmrg
100602e473dSmrg    pthread_cond_broadcast(&c->out.socket_cond);
101602e473dSmrg    c->out.return_socket = 0;
102602e473dSmrg    c->out.socket_closure = 0;
103602e473dSmrg    _xcb_in_replies_done(c);
104602e473dSmrg}
105602e473dSmrg
1061c7386f4Smrgstatic void prepare_socket_request(xcb_connection_t *c)
1071c7386f4Smrg{
1081c7386f4Smrg    /* We're about to append data to out.queue, so we need to
1091c7386f4Smrg     * atomically test for an external socket owner *and* some other
1101c7386f4Smrg     * thread currently writing.
1111c7386f4Smrg     *
1121c7386f4Smrg     * If we have an external socket owner, we have to get the socket back
1131c7386f4Smrg     * before we can use it again.
1141c7386f4Smrg     *
1151c7386f4Smrg     * If some other thread is writing to the socket, we assume it's
1161c7386f4Smrg     * writing from out.queue, and so we can't stick data there.
1171c7386f4Smrg     *
1181c7386f4Smrg     * We satisfy this condition by first calling get_socket_back
1191c7386f4Smrg     * (which may drop the lock, but will return when XCB owns the
1201c7386f4Smrg     * socket again) and then checking for another writing thread and
1211c7386f4Smrg     * escaping the loop if we're ready to go.
1221c7386f4Smrg     */
1231c7386f4Smrg    for (;;) {
1241c7386f4Smrg        if(c->has_error)
1251c7386f4Smrg            return;
1261c7386f4Smrg        get_socket_back(c);
1271c7386f4Smrg        if (!c->out.writing)
1281c7386f4Smrg            break;
1291c7386f4Smrg        pthread_cond_wait(&c->out.cond, &c->iolock);
1301c7386f4Smrg    }
1311c7386f4Smrg}
1321c7386f4Smrg
133602e473dSmrg/* Public interface */
134602e473dSmrg
135602e473dSmrgvoid xcb_prefetch_maximum_request_length(xcb_connection_t *c)
136602e473dSmrg{
137602e473dSmrg    if(c->has_error)
138602e473dSmrg        return;
139602e473dSmrg    pthread_mutex_lock(&c->out.reqlenlock);
140602e473dSmrg    if(c->out.maximum_request_length_tag == LAZY_NONE)
141602e473dSmrg    {
142602e473dSmrg        const xcb_query_extension_reply_t *ext;
143602e473dSmrg        ext = xcb_get_extension_data(c, &xcb_big_requests_id);
144602e473dSmrg        if(ext && ext->present)
145602e473dSmrg        {
146602e473dSmrg            c->out.maximum_request_length_tag = LAZY_COOKIE;
147602e473dSmrg            c->out.maximum_request_length.cookie = xcb_big_requests_enable(c);
148602e473dSmrg        }
149602e473dSmrg        else
150602e473dSmrg        {
151602e473dSmrg            c->out.maximum_request_length_tag = LAZY_FORCED;
152602e473dSmrg            c->out.maximum_request_length.value = c->setup->maximum_request_length;
153602e473dSmrg        }
154602e473dSmrg    }
155602e473dSmrg    pthread_mutex_unlock(&c->out.reqlenlock);
156602e473dSmrg}
157602e473dSmrg
158602e473dSmrguint32_t xcb_get_maximum_request_length(xcb_connection_t *c)
159602e473dSmrg{
160602e473dSmrg    if(c->has_error)
161602e473dSmrg        return 0;
162602e473dSmrg    xcb_prefetch_maximum_request_length(c);
163602e473dSmrg    pthread_mutex_lock(&c->out.reqlenlock);
164602e473dSmrg    if(c->out.maximum_request_length_tag == LAZY_COOKIE)
165602e473dSmrg    {
166602e473dSmrg        xcb_big_requests_enable_reply_t *r = xcb_big_requests_enable_reply(c, c->out.maximum_request_length.cookie, 0);
167602e473dSmrg        c->out.maximum_request_length_tag = LAZY_FORCED;
168602e473dSmrg        if(r)
169602e473dSmrg        {
170602e473dSmrg            c->out.maximum_request_length.value = r->maximum_request_length;
171602e473dSmrg            free(r);
172602e473dSmrg        }
173602e473dSmrg        else
174602e473dSmrg            c->out.maximum_request_length.value = c->setup->maximum_request_length;
175602e473dSmrg    }
176602e473dSmrg    pthread_mutex_unlock(&c->out.reqlenlock);
177602e473dSmrg    return c->out.maximum_request_length.value;
178602e473dSmrg}
179602e473dSmrg
1807204935cSmrgstatic void close_fds(int *fds, unsigned int num_fds)
1817204935cSmrg{
1827204935cSmrg    for (unsigned int index = 0; index < num_fds; index++)
1837204935cSmrg        close(fds[index]);
1847204935cSmrg}
1857204935cSmrg
1867204935cSmrgstatic void send_fds(xcb_connection_t *c, int *fds, unsigned int num_fds)
1877204935cSmrg{
1887204935cSmrg#if HAVE_SENDMSG
1897204935cSmrg    /* Calling _xcb_out_flush_to() can drop the iolock and wait on a condition
1907204935cSmrg     * variable if another thread is currently writing (c->out.writing > 0).
1917204935cSmrg     * This call waits for writers to be done and thus _xcb_out_flush_to() will
1927204935cSmrg     * do the work itself (in which case we are a writer and
1937204935cSmrg     * prepare_socket_request() will wait for us to be done if another threads
1947204935cSmrg     * tries to send fds, too). Thanks to this, we can atomically write out FDs.
1957204935cSmrg     */
1967204935cSmrg    prepare_socket_request(c);
1977204935cSmrg
1987204935cSmrg    while (num_fds > 0) {
1997204935cSmrg        while (c->out.out_fd.nfd == XCB_MAX_PASS_FD && !c->has_error) {
2007204935cSmrg            /* XXX: if c->out.writing > 0, this releases the iolock and
2017204935cSmrg             * potentially allows other threads to interfere with their own fds.
2027204935cSmrg             */
2037204935cSmrg            _xcb_out_flush_to(c, c->out.request);
2047204935cSmrg
2057204935cSmrg            if (c->out.out_fd.nfd == XCB_MAX_PASS_FD) {
2067204935cSmrg                /* We need some request to send FDs with */
2077204935cSmrg                _xcb_out_send_sync(c);
2087204935cSmrg            }
2097204935cSmrg        }
2107204935cSmrg        if (c->has_error)
2117204935cSmrg            break;
2127204935cSmrg
2137204935cSmrg        c->out.out_fd.fd[c->out.out_fd.nfd++] = fds[0];
2147204935cSmrg        fds++;
2157204935cSmrg        num_fds--;
2167204935cSmrg    }
2177204935cSmrg#endif
2187204935cSmrg    close_fds(fds, num_fds);
2197204935cSmrg}
2207204935cSmrg
2217204935cSmrguint64_t xcb_send_request_with_fds64(xcb_connection_t *c, int flags, struct iovec *vector,
2227204935cSmrg                const xcb_protocol_request_t *req, unsigned int num_fds, int *fds)
223602e473dSmrg{
224602e473dSmrg    uint64_t request;
22521298544Smrg    uint32_t prefix[2];
226602e473dSmrg    int veclen = req->count;
227602e473dSmrg    enum workarounds workaround = WORKAROUND_NONE;
228602e473dSmrg
2297204935cSmrg    if(c->has_error) {
2307204935cSmrg        close_fds(fds, num_fds);
231602e473dSmrg        return 0;
2327204935cSmrg    }
233602e473dSmrg
234602e473dSmrg    assert(c != 0);
235602e473dSmrg    assert(vector != 0);
236602e473dSmrg    assert(req->count > 0);
237602e473dSmrg
238602e473dSmrg    if(!(flags & XCB_REQUEST_RAW))
239602e473dSmrg    {
240602e473dSmrg        static const char pad[3];
241602e473dSmrg        unsigned int i;
242602e473dSmrg        uint16_t shortlen = 0;
243602e473dSmrg        size_t longlen = 0;
244602e473dSmrg        assert(vector[0].iov_len >= 4);
245602e473dSmrg        /* set the major opcode, and the minor opcode for extensions */
246602e473dSmrg        if(req->ext)
247602e473dSmrg        {
248602e473dSmrg            const xcb_query_extension_reply_t *extension = xcb_get_extension_data(c, req->ext);
249602e473dSmrg            if(!(extension && extension->present))
250602e473dSmrg            {
2517204935cSmrg                close_fds(fds, num_fds);
25221298544Smrg                _xcb_conn_shutdown(c, XCB_CONN_CLOSED_EXT_NOTSUPPORTED);
253602e473dSmrg                return 0;
254602e473dSmrg            }
255602e473dSmrg            ((uint8_t *) vector[0].iov_base)[0] = extension->major_opcode;
256602e473dSmrg            ((uint8_t *) vector[0].iov_base)[1] = req->opcode;
257602e473dSmrg        }
258602e473dSmrg        else
259602e473dSmrg            ((uint8_t *) vector[0].iov_base)[0] = req->opcode;
260602e473dSmrg
261602e473dSmrg        /* put together the length field, possibly using BIGREQUESTS */
262602e473dSmrg        for(i = 0; i < req->count; ++i)
263602e473dSmrg        {
264602e473dSmrg            longlen += vector[i].iov_len;
265602e473dSmrg            if(!vector[i].iov_base)
266602e473dSmrg            {
267602e473dSmrg                vector[i].iov_base = (char *) pad;
268602e473dSmrg                assert(vector[i].iov_len <= sizeof(pad));
269602e473dSmrg            }
270602e473dSmrg        }
271602e473dSmrg        assert((longlen & 3) == 0);
272602e473dSmrg        longlen >>= 2;
273602e473dSmrg
274602e473dSmrg        if(longlen <= c->setup->maximum_request_length)
275602e473dSmrg        {
276602e473dSmrg            /* we don't need BIGREQUESTS. */
277602e473dSmrg            shortlen = longlen;
278602e473dSmrg            longlen = 0;
279602e473dSmrg        }
280602e473dSmrg        else if(longlen > xcb_get_maximum_request_length(c))
281602e473dSmrg        {
2827204935cSmrg            close_fds(fds, num_fds);
28321298544Smrg            _xcb_conn_shutdown(c, XCB_CONN_CLOSED_REQ_LEN_EXCEED);
284602e473dSmrg            return 0; /* server can't take this; maybe need BIGREQUESTS? */
285602e473dSmrg        }
286602e473dSmrg
287602e473dSmrg        /* set the length field. */
288602e473dSmrg        ((uint16_t *) vector[0].iov_base)[1] = shortlen;
289602e473dSmrg        if(!shortlen)
29021298544Smrg        {
29121298544Smrg            prefix[0] = ((uint32_t *) vector[0].iov_base)[0];
29221298544Smrg            prefix[1] = ++longlen;
29321298544Smrg            vector[0].iov_base = (uint32_t *) vector[0].iov_base + 1;
29421298544Smrg            vector[0].iov_len -= sizeof(uint32_t);
29521298544Smrg            --vector, ++veclen;
29621298544Smrg            vector[0].iov_base = prefix;
29721298544Smrg            vector[0].iov_len = sizeof(prefix);
29821298544Smrg        }
299602e473dSmrg    }
300602e473dSmrg    flags &= ~XCB_REQUEST_RAW;
301602e473dSmrg
302602e473dSmrg    /* do we need to work around the X server bug described in glx.xml? */
303602e473dSmrg    /* XXX: GetFBConfigs won't use BIG-REQUESTS in any sane
304602e473dSmrg     * configuration, but that should be handled here anyway. */
305602e473dSmrg    if(req->ext && !req->isvoid && !strcmp(req->ext->name, "GLX") &&
306602e473dSmrg            ((req->opcode == 17 && ((uint32_t *) vector[0].iov_base)[1] == 0x10004) ||
307602e473dSmrg             req->opcode == 21))
308602e473dSmrg        workaround = WORKAROUND_GLX_GET_FB_CONFIGS_BUG;
309602e473dSmrg
310602e473dSmrg    /* get a sequence number and arrange for delivery. */
311602e473dSmrg    pthread_mutex_lock(&c->iolock);
3121c7386f4Smrg
3137204935cSmrg    /* send FDs before establishing a good request number, because this might
3147204935cSmrg     * call send_sync(), too
3157204935cSmrg     */
3167204935cSmrg    send_fds(c, fds, num_fds);
3177204935cSmrg
3181c7386f4Smrg    prepare_socket_request(c);
319602e473dSmrg
320602e473dSmrg    /* send GetInputFocus (sync_req) when 64k-2 requests have been sent without
3211c7386f4Smrg     * a reply.
3221c7386f4Smrg     * Also send sync_req (could use NoOp) at 32-bit wrap to avoid having
323602e473dSmrg     * applications see sequence 0 as that is used to indicate
3241c7386f4Smrg     * an error in sending the request
3251c7386f4Smrg     */
3267204935cSmrg
3271c7386f4Smrg    while ((req->isvoid && c->out.request == c->in.request_expected + (1 << 16) - 2) ||
3281c7386f4Smrg           (unsigned int) (c->out.request + 1) == 0)
3291c7386f4Smrg    {
33021298544Smrg        send_sync(c);
3311c7386f4Smrg        prepare_socket_request(c);
3321c7386f4Smrg    }
33321298544Smrg
33421298544Smrg    send_request(c, req->isvoid, workaround, flags, vector, veclen);
33521298544Smrg    request = c->has_error ? 0 : c->out.request;
336602e473dSmrg    pthread_mutex_unlock(&c->iolock);
337602e473dSmrg    return request;
338602e473dSmrg}
339602e473dSmrg
3407204935cSmrg/* request number are actually uint64_t internally but keep API compat with unsigned int */
3417204935cSmrgunsigned int xcb_send_request_with_fds(xcb_connection_t *c, int flags, struct iovec *vector,
3427204935cSmrg        const xcb_protocol_request_t *req, unsigned int num_fds, int *fds)
3437204935cSmrg{
3447204935cSmrg    return xcb_send_request_with_fds64(c, flags, vector, req, num_fds, fds);
3457204935cSmrg}
3467204935cSmrg
3477204935cSmrguint64_t xcb_send_request64(xcb_connection_t *c, int flags, struct iovec *vector, const xcb_protocol_request_t *req)
3487204935cSmrg{
3497204935cSmrg    return xcb_send_request_with_fds64(c, flags, vector, req, 0, NULL);
3507204935cSmrg}
3517204935cSmrg
352709d36bbSmrg/* request number are actually uint64_t internally but keep API compat with unsigned int */
353709d36bbSmrgunsigned int xcb_send_request(xcb_connection_t *c, int flags, struct iovec *vector, const xcb_protocol_request_t *req)
354709d36bbSmrg{
355709d36bbSmrg    return xcb_send_request64(c, flags, vector, req);
356709d36bbSmrg}
357709d36bbSmrg
3581016ad83Smrgvoid
3591016ad83Smrgxcb_send_fd(xcb_connection_t *c, int fd)
3601016ad83Smrg{
3617204935cSmrg    int fds[1] = { fd };
3627204935cSmrg
3637204935cSmrg    if (c->has_error) {
3647204935cSmrg        close(fd);
3651016ad83Smrg        return;
3661016ad83Smrg    }
3677204935cSmrg    pthread_mutex_lock(&c->iolock);
3687204935cSmrg    send_fds(c, &fds[0], 1);
3691016ad83Smrg    pthread_mutex_unlock(&c->iolock);
3701016ad83Smrg}
3711016ad83Smrg
372602e473dSmrgint xcb_take_socket(xcb_connection_t *c, void (*return_socket)(void *closure), void *closure, int flags, uint64_t *sent)
373602e473dSmrg{
374602e473dSmrg    int ret;
375602e473dSmrg    if(c->has_error)
376602e473dSmrg        return 0;
377602e473dSmrg    pthread_mutex_lock(&c->iolock);
378602e473dSmrg    get_socket_back(c);
37921298544Smrg
38021298544Smrg    /* _xcb_out_flush may drop the iolock allowing other threads to
38121298544Smrg     * write requests, so keep flushing until we're done
38221298544Smrg     */
38321298544Smrg    do
38408c70cfbSmrg        ret = _xcb_out_flush_to(c, c->out.request);
38521298544Smrg    while (ret && c->out.request != c->out.request_written);
386602e473dSmrg    if(ret)
387602e473dSmrg    {
388602e473dSmrg        c->out.return_socket = return_socket;
389602e473dSmrg        c->out.socket_closure = closure;
390fe12f63cSmrg        if(flags) {
391fe12f63cSmrg            /* c->out.request + 1 will be the first request sent by the external
392fe12f63cSmrg             * socket owner. If the socket is returned before this request is sent
393fe12f63cSmrg             * it will be detected in _xcb_in_replies_done and this pending_reply
394fe12f63cSmrg             * will be discarded.
395fe12f63cSmrg             */
396fe12f63cSmrg            _xcb_in_expect_reply(c, c->out.request + 1, WORKAROUND_EXTERNAL_SOCKET_OWNER, flags);
397fe12f63cSmrg        }
398602e473dSmrg        assert(c->out.request == c->out.request_written);
399602e473dSmrg        *sent = c->out.request;
400602e473dSmrg    }
401602e473dSmrg    pthread_mutex_unlock(&c->iolock);
402602e473dSmrg    return ret;
403602e473dSmrg}
404602e473dSmrg
405602e473dSmrgint xcb_writev(xcb_connection_t *c, struct iovec *vector, int count, uint64_t requests)
406602e473dSmrg{
407602e473dSmrg    int ret;
408602e473dSmrg    if(c->has_error)
409602e473dSmrg        return 0;
410602e473dSmrg    pthread_mutex_lock(&c->iolock);
411602e473dSmrg    c->out.request += requests;
412b20a2039Smrg    ret = _xcb_out_send(c, vector, count);
413602e473dSmrg    pthread_mutex_unlock(&c->iolock);
414602e473dSmrg    return ret;
415602e473dSmrg}
416602e473dSmrg
417602e473dSmrgint xcb_flush(xcb_connection_t *c)
418602e473dSmrg{
419602e473dSmrg    int ret;
420602e473dSmrg    if(c->has_error)
421602e473dSmrg        return 0;
422602e473dSmrg    pthread_mutex_lock(&c->iolock);
423602e473dSmrg    ret = _xcb_out_flush_to(c, c->out.request);
424602e473dSmrg    pthread_mutex_unlock(&c->iolock);
425602e473dSmrg    return ret;
426602e473dSmrg}
427602e473dSmrg
428602e473dSmrg/* Private interface */
429602e473dSmrg
430602e473dSmrgint _xcb_out_init(_xcb_out *out)
431602e473dSmrg{
432602e473dSmrg    if(pthread_cond_init(&out->socket_cond, 0))
433602e473dSmrg        return 0;
434602e473dSmrg    out->return_socket = 0;
435602e473dSmrg    out->socket_closure = 0;
436602e473dSmrg    out->socket_moving = 0;
437602e473dSmrg
438602e473dSmrg    if(pthread_cond_init(&out->cond, 0))
439602e473dSmrg        return 0;
440602e473dSmrg    out->writing = 0;
441602e473dSmrg
442602e473dSmrg    out->queue_len = 0;
443602e473dSmrg
444602e473dSmrg    out->request = 0;
445602e473dSmrg    out->request_written = 0;
446602e473dSmrg
447602e473dSmrg    if(pthread_mutex_init(&out->reqlenlock, 0))
448602e473dSmrg        return 0;
449602e473dSmrg    out->maximum_request_length_tag = LAZY_NONE;
450602e473dSmrg
451602e473dSmrg    return 1;
452602e473dSmrg}
453602e473dSmrg
454602e473dSmrgvoid _xcb_out_destroy(_xcb_out *out)
455602e473dSmrg{
456602e473dSmrg    pthread_cond_destroy(&out->cond);
457602e473dSmrg    pthread_mutex_destroy(&out->reqlenlock);
458602e473dSmrg}
459602e473dSmrg
460b20a2039Smrgint _xcb_out_send(xcb_connection_t *c, struct iovec *vector, int count)
461602e473dSmrg{
462602e473dSmrg    int ret = 1;
463b20a2039Smrg    while(ret && count)
464b20a2039Smrg        ret = _xcb_conn_wait(c, &c->out.cond, &vector, &count);
465602e473dSmrg    c->out.request_written = c->out.request;
466602e473dSmrg    pthread_cond_broadcast(&c->out.cond);
467b20a2039Smrg    _xcb_in_wake_up_next_reader(c);
468602e473dSmrg    return ret;
469602e473dSmrg}
470602e473dSmrg
47121298544Smrgvoid _xcb_out_send_sync(xcb_connection_t *c)
47221298544Smrg{
4731c7386f4Smrg    prepare_socket_request(c);
47421298544Smrg    send_sync(c);
47521298544Smrg}
47621298544Smrg
477602e473dSmrgint _xcb_out_flush_to(xcb_connection_t *c, uint64_t request)
478602e473dSmrg{
479602e473dSmrg    assert(XCB_SEQUENCE_COMPARE(request, <=, c->out.request));
480602e473dSmrg    if(XCB_SEQUENCE_COMPARE(c->out.request_written, >=, request))
481602e473dSmrg        return 1;
482602e473dSmrg    if(c->out.queue_len)
483602e473dSmrg    {
484b20a2039Smrg        struct iovec vec;
485602e473dSmrg        vec.iov_base = c->out.queue;
486602e473dSmrg        vec.iov_len = c->out.queue_len;
487602e473dSmrg        c->out.queue_len = 0;
488b20a2039Smrg        return _xcb_out_send(c, &vec, 1);
489602e473dSmrg    }
490602e473dSmrg    while(c->out.writing)
491602e473dSmrg        pthread_cond_wait(&c->out.cond, &c->iolock);
492602e473dSmrg    assert(XCB_SEQUENCE_COMPARE(c->out.request_written, >=, request));
493602e473dSmrg    return 1;
494602e473dSmrg}
495