xcb_out.c revision 1c7386f4
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
180602e473dSmrgunsigned int xcb_send_request(xcb_connection_t *c, int flags, struct iovec *vector, const xcb_protocol_request_t *req)
181602e473dSmrg{
182602e473dSmrg    uint64_t request;
18321298544Smrg    uint32_t prefix[2];
184602e473dSmrg    int veclen = req->count;
185602e473dSmrg    enum workarounds workaround = WORKAROUND_NONE;
186602e473dSmrg
187602e473dSmrg    if(c->has_error)
188602e473dSmrg        return 0;
189602e473dSmrg
190602e473dSmrg    assert(c != 0);
191602e473dSmrg    assert(vector != 0);
192602e473dSmrg    assert(req->count > 0);
193602e473dSmrg
194602e473dSmrg    if(!(flags & XCB_REQUEST_RAW))
195602e473dSmrg    {
196602e473dSmrg        static const char pad[3];
197602e473dSmrg        unsigned int i;
198602e473dSmrg        uint16_t shortlen = 0;
199602e473dSmrg        size_t longlen = 0;
200602e473dSmrg        assert(vector[0].iov_len >= 4);
201602e473dSmrg        /* set the major opcode, and the minor opcode for extensions */
202602e473dSmrg        if(req->ext)
203602e473dSmrg        {
204602e473dSmrg            const xcb_query_extension_reply_t *extension = xcb_get_extension_data(c, req->ext);
205602e473dSmrg            if(!(extension && extension->present))
206602e473dSmrg            {
20721298544Smrg                _xcb_conn_shutdown(c, XCB_CONN_CLOSED_EXT_NOTSUPPORTED);
208602e473dSmrg                return 0;
209602e473dSmrg            }
210602e473dSmrg            ((uint8_t *) vector[0].iov_base)[0] = extension->major_opcode;
211602e473dSmrg            ((uint8_t *) vector[0].iov_base)[1] = req->opcode;
212602e473dSmrg        }
213602e473dSmrg        else
214602e473dSmrg            ((uint8_t *) vector[0].iov_base)[0] = req->opcode;
215602e473dSmrg
216602e473dSmrg        /* put together the length field, possibly using BIGREQUESTS */
217602e473dSmrg        for(i = 0; i < req->count; ++i)
218602e473dSmrg        {
219602e473dSmrg            longlen += vector[i].iov_len;
220602e473dSmrg            if(!vector[i].iov_base)
221602e473dSmrg            {
222602e473dSmrg                vector[i].iov_base = (char *) pad;
223602e473dSmrg                assert(vector[i].iov_len <= sizeof(pad));
224602e473dSmrg            }
225602e473dSmrg        }
226602e473dSmrg        assert((longlen & 3) == 0);
227602e473dSmrg        longlen >>= 2;
228602e473dSmrg
229602e473dSmrg        if(longlen <= c->setup->maximum_request_length)
230602e473dSmrg        {
231602e473dSmrg            /* we don't need BIGREQUESTS. */
232602e473dSmrg            shortlen = longlen;
233602e473dSmrg            longlen = 0;
234602e473dSmrg        }
235602e473dSmrg        else if(longlen > xcb_get_maximum_request_length(c))
236602e473dSmrg        {
23721298544Smrg            _xcb_conn_shutdown(c, XCB_CONN_CLOSED_REQ_LEN_EXCEED);
238602e473dSmrg            return 0; /* server can't take this; maybe need BIGREQUESTS? */
239602e473dSmrg        }
240602e473dSmrg
241602e473dSmrg        /* set the length field. */
242602e473dSmrg        ((uint16_t *) vector[0].iov_base)[1] = shortlen;
243602e473dSmrg        if(!shortlen)
24421298544Smrg        {
24521298544Smrg            prefix[0] = ((uint32_t *) vector[0].iov_base)[0];
24621298544Smrg            prefix[1] = ++longlen;
24721298544Smrg            vector[0].iov_base = (uint32_t *) vector[0].iov_base + 1;
24821298544Smrg            vector[0].iov_len -= sizeof(uint32_t);
24921298544Smrg            --vector, ++veclen;
25021298544Smrg            vector[0].iov_base = prefix;
25121298544Smrg            vector[0].iov_len = sizeof(prefix);
25221298544Smrg        }
253602e473dSmrg    }
254602e473dSmrg    flags &= ~XCB_REQUEST_RAW;
255602e473dSmrg
256602e473dSmrg    /* do we need to work around the X server bug described in glx.xml? */
257602e473dSmrg    /* XXX: GetFBConfigs won't use BIG-REQUESTS in any sane
258602e473dSmrg     * configuration, but that should be handled here anyway. */
259602e473dSmrg    if(req->ext && !req->isvoid && !strcmp(req->ext->name, "GLX") &&
260602e473dSmrg            ((req->opcode == 17 && ((uint32_t *) vector[0].iov_base)[1] == 0x10004) ||
261602e473dSmrg             req->opcode == 21))
262602e473dSmrg        workaround = WORKAROUND_GLX_GET_FB_CONFIGS_BUG;
263602e473dSmrg
264602e473dSmrg    /* get a sequence number and arrange for delivery. */
265602e473dSmrg    pthread_mutex_lock(&c->iolock);
2661c7386f4Smrg
2671c7386f4Smrg    prepare_socket_request(c);
268602e473dSmrg
269602e473dSmrg    /* send GetInputFocus (sync_req) when 64k-2 requests have been sent without
2701c7386f4Smrg     * a reply.
2711c7386f4Smrg     * Also send sync_req (could use NoOp) at 32-bit wrap to avoid having
272602e473dSmrg     * applications see sequence 0 as that is used to indicate
2731c7386f4Smrg     * an error in sending the request
2741c7386f4Smrg     */
2751c7386f4Smrg
2761c7386f4Smrg    while ((req->isvoid && c->out.request == c->in.request_expected + (1 << 16) - 2) ||
2771c7386f4Smrg           (unsigned int) (c->out.request + 1) == 0)
2781c7386f4Smrg    {
27921298544Smrg        send_sync(c);
2801c7386f4Smrg        prepare_socket_request(c);
2811c7386f4Smrg    }
28221298544Smrg
28321298544Smrg    send_request(c, req->isvoid, workaround, flags, vector, veclen);
28421298544Smrg    request = c->has_error ? 0 : c->out.request;
285602e473dSmrg    pthread_mutex_unlock(&c->iolock);
286602e473dSmrg    return request;
287602e473dSmrg}
288602e473dSmrg
2891016ad83Smrgvoid
2901016ad83Smrgxcb_send_fd(xcb_connection_t *c, int fd)
2911016ad83Smrg{
2921016ad83Smrg#if HAVE_SENDMSG
2931016ad83Smrg    if (c->has_error)
2941016ad83Smrg        return;
2951016ad83Smrg    pthread_mutex_lock(&c->iolock);
2961016ad83Smrg    while (c->out.out_fd.nfd == XCB_MAX_PASS_FD) {
2971016ad83Smrg        _xcb_out_flush_to(c, c->out.request);
2981016ad83Smrg        if (c->has_error)
2991016ad83Smrg            break;
3001016ad83Smrg    }
3011016ad83Smrg    if (!c->has_error)
3021016ad83Smrg        c->out.out_fd.fd[c->out.out_fd.nfd++] = fd;
3031016ad83Smrg    pthread_mutex_unlock(&c->iolock);
3041016ad83Smrg#endif
3051016ad83Smrg}
3061016ad83Smrg
307602e473dSmrgint xcb_take_socket(xcb_connection_t *c, void (*return_socket)(void *closure), void *closure, int flags, uint64_t *sent)
308602e473dSmrg{
309602e473dSmrg    int ret;
310602e473dSmrg    if(c->has_error)
311602e473dSmrg        return 0;
312602e473dSmrg    pthread_mutex_lock(&c->iolock);
313602e473dSmrg    get_socket_back(c);
31421298544Smrg
31521298544Smrg    /* _xcb_out_flush may drop the iolock allowing other threads to
31621298544Smrg     * write requests, so keep flushing until we're done
31721298544Smrg     */
31821298544Smrg    do
31908c70cfbSmrg        ret = _xcb_out_flush_to(c, c->out.request);
32021298544Smrg    while (ret && c->out.request != c->out.request_written);
321602e473dSmrg    if(ret)
322602e473dSmrg    {
323602e473dSmrg        c->out.return_socket = return_socket;
324602e473dSmrg        c->out.socket_closure = closure;
325602e473dSmrg        if(flags)
326602e473dSmrg            _xcb_in_expect_reply(c, c->out.request, WORKAROUND_EXTERNAL_SOCKET_OWNER, flags);
327602e473dSmrg        assert(c->out.request == c->out.request_written);
328602e473dSmrg        *sent = c->out.request;
329602e473dSmrg    }
330602e473dSmrg    pthread_mutex_unlock(&c->iolock);
331602e473dSmrg    return ret;
332602e473dSmrg}
333602e473dSmrg
334602e473dSmrgint xcb_writev(xcb_connection_t *c, struct iovec *vector, int count, uint64_t requests)
335602e473dSmrg{
336602e473dSmrg    int ret;
337602e473dSmrg    if(c->has_error)
338602e473dSmrg        return 0;
339602e473dSmrg    pthread_mutex_lock(&c->iolock);
340602e473dSmrg    c->out.request += requests;
341b20a2039Smrg    ret = _xcb_out_send(c, vector, count);
342602e473dSmrg    pthread_mutex_unlock(&c->iolock);
343602e473dSmrg    return ret;
344602e473dSmrg}
345602e473dSmrg
346602e473dSmrgint xcb_flush(xcb_connection_t *c)
347602e473dSmrg{
348602e473dSmrg    int ret;
349602e473dSmrg    if(c->has_error)
350602e473dSmrg        return 0;
351602e473dSmrg    pthread_mutex_lock(&c->iolock);
352602e473dSmrg    ret = _xcb_out_flush_to(c, c->out.request);
353602e473dSmrg    pthread_mutex_unlock(&c->iolock);
354602e473dSmrg    return ret;
355602e473dSmrg}
356602e473dSmrg
357602e473dSmrg/* Private interface */
358602e473dSmrg
359602e473dSmrgint _xcb_out_init(_xcb_out *out)
360602e473dSmrg{
361602e473dSmrg    if(pthread_cond_init(&out->socket_cond, 0))
362602e473dSmrg        return 0;
363602e473dSmrg    out->return_socket = 0;
364602e473dSmrg    out->socket_closure = 0;
365602e473dSmrg    out->socket_moving = 0;
366602e473dSmrg
367602e473dSmrg    if(pthread_cond_init(&out->cond, 0))
368602e473dSmrg        return 0;
369602e473dSmrg    out->writing = 0;
370602e473dSmrg
371602e473dSmrg    out->queue_len = 0;
372602e473dSmrg
373602e473dSmrg    out->request = 0;
374602e473dSmrg    out->request_written = 0;
375602e473dSmrg
376602e473dSmrg    if(pthread_mutex_init(&out->reqlenlock, 0))
377602e473dSmrg        return 0;
378602e473dSmrg    out->maximum_request_length_tag = LAZY_NONE;
379602e473dSmrg
380602e473dSmrg    return 1;
381602e473dSmrg}
382602e473dSmrg
383602e473dSmrgvoid _xcb_out_destroy(_xcb_out *out)
384602e473dSmrg{
385602e473dSmrg    pthread_cond_destroy(&out->cond);
386602e473dSmrg    pthread_mutex_destroy(&out->reqlenlock);
387602e473dSmrg}
388602e473dSmrg
389b20a2039Smrgint _xcb_out_send(xcb_connection_t *c, struct iovec *vector, int count)
390602e473dSmrg{
391602e473dSmrg    int ret = 1;
392b20a2039Smrg    while(ret && count)
393b20a2039Smrg        ret = _xcb_conn_wait(c, &c->out.cond, &vector, &count);
394602e473dSmrg    c->out.request_written = c->out.request;
395602e473dSmrg    pthread_cond_broadcast(&c->out.cond);
396b20a2039Smrg    _xcb_in_wake_up_next_reader(c);
397602e473dSmrg    return ret;
398602e473dSmrg}
399602e473dSmrg
40021298544Smrgvoid _xcb_out_send_sync(xcb_connection_t *c)
40121298544Smrg{
4021c7386f4Smrg    prepare_socket_request(c);
40321298544Smrg    send_sync(c);
40421298544Smrg}
40521298544Smrg
406602e473dSmrgint _xcb_out_flush_to(xcb_connection_t *c, uint64_t request)
407602e473dSmrg{
408602e473dSmrg    assert(XCB_SEQUENCE_COMPARE(request, <=, c->out.request));
409602e473dSmrg    if(XCB_SEQUENCE_COMPARE(c->out.request_written, >=, request))
410602e473dSmrg        return 1;
411602e473dSmrg    if(c->out.queue_len)
412602e473dSmrg    {
413b20a2039Smrg        struct iovec vec;
414602e473dSmrg        vec.iov_base = c->out.queue;
415602e473dSmrg        vec.iov_len = c->out.queue_len;
416602e473dSmrg        c->out.queue_len = 0;
417b20a2039Smrg        return _xcb_out_send(c, &vec, 1);
418602e473dSmrg    }
419602e473dSmrg    while(c->out.writing)
420602e473dSmrg        pthread_cond_wait(&c->out.cond, &c->iolock);
421602e473dSmrg    assert(XCB_SEQUENCE_COMPARE(c->out.request_written, >=, request));
422602e473dSmrg    return 1;
423602e473dSmrg}
424