Provide xcb_prefetch_maximum_request_length counterpart to xcb_get_maximum_request_le...
[free-sw/xcb/libxcb] / src / xcb_out.c
1 /* Copyright (C) 2001-2004 Bart Massey and Jamey Sharp.
2  *
3  * Permission is hereby granted, free of charge, to any person obtaining a
4  * copy of this software and associated documentation files (the "Software"),
5  * to deal in the Software without restriction, including without limitation
6  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
7  * and/or sell copies of the Software, and to permit persons to whom the
8  * Software is furnished to do so, subject to the following conditions:
9  * 
10  * The above copyright notice and this permission notice shall be included in
11  * all copies or substantial portions of the Software.
12  * 
13  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16  * AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
17  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
18  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
19  * 
20  * Except as contained in this notice, the names of the authors or their
21  * institutions shall not be used in advertising or otherwise to promote the
22  * sale, use or other dealings in this Software without prior written
23  * authorization from the authors.
24  */
25
26 /* Stuff that sends stuff to the server. */
27
28 #include <assert.h>
29 #include <stdlib.h>
30 #include <unistd.h>
31 #include <string.h>
32
33 #include "xcb.h"
34 #include "xcbext.h"
35 #include "xcbint.h"
36 #include "bigreq.h"
37
38 static int write_block(xcb_connection_t *c, struct iovec *vector, int count)
39 {
40     while(count && c->out.queue_len + vector[0].iov_len <= sizeof(c->out.queue))
41     {
42         memcpy(c->out.queue + c->out.queue_len, vector[0].iov_base, vector[0].iov_len);
43         c->out.queue_len += vector[0].iov_len;
44         vector[0].iov_base = (char *) vector[0].iov_base + vector[0].iov_len;
45         vector[0].iov_len = 0;
46         ++vector, --count;
47     }
48     if(!count)
49         return 1;
50
51     --vector, ++count;
52     vector[0].iov_base = c->out.queue;
53     vector[0].iov_len = c->out.queue_len;
54     c->out.queue_len = 0;
55     return _xcb_out_send(c, &vector, &count);
56 }
57
58 /* Public interface */
59
60 void xcb_prefetch_maximum_request_length(xcb_connection_t *c)
61 {
62     if(c->has_error)
63         return;
64     pthread_mutex_lock(&c->out.reqlenlock);
65     if(c->out.maximum_request_length_tag == LAZY_NONE)
66     {
67         const xcb_query_extension_reply_t *ext;
68         ext = xcb_get_extension_data(c, &xcb_big_requests_id);
69         if(ext && ext->present)
70         {
71             c->out.maximum_request_length_tag = LAZY_COOKIE;
72             c->out.maximum_request_length.cookie = xcb_big_requests_enable(c);
73         }
74         else
75         {
76             c->out.maximum_request_length_tag = LAZY_FORCED;
77             c->out.maximum_request_length.value = c->setup->maximum_request_length;
78         }
79     }
80     pthread_mutex_unlock(&c->out.reqlenlock);
81 }
82
83 uint32_t xcb_get_maximum_request_length(xcb_connection_t *c)
84 {
85     if(c->has_error)
86         return 0;
87     xcb_prefetch_maximum_request_length(c);
88     pthread_mutex_lock(&c->out.reqlenlock);
89     if(c->out.maximum_request_length_tag == LAZY_COOKIE)
90     {
91         xcb_big_requests_enable_reply_t *r = xcb_big_requests_enable_reply(c, c->out.maximum_request_length.cookie, 0);
92         c->out.maximum_request_length_tag = LAZY_FORCED;
93         if(r)
94         {
95             c->out.maximum_request_length.value = r->maximum_request_length;
96             free(r);
97         }
98         else
99             c->out.maximum_request_length.value = c->setup->maximum_request_length;
100     }
101     pthread_mutex_unlock(&c->out.reqlenlock);
102     return c->out.maximum_request_length.value;
103 }
104
105 unsigned int xcb_send_request(xcb_connection_t *c, int flags, struct iovec *vector, const xcb_protocol_request_t *req)
106 {
107     static const union {
108         struct {
109             uint8_t major;
110             uint8_t pad;
111             uint16_t len;
112         } fields;
113         uint32_t packet;
114     } sync = { { /* GetInputFocus */ 43, 0, 1 } };
115     unsigned int request;
116     uint32_t prefix[3] = { 0 };
117     int veclen = req->count;
118     enum workarounds workaround = WORKAROUND_NONE;
119
120     if(c->has_error)
121         return 0;
122
123     assert(c != 0);
124     assert(vector != 0);
125     assert(req->count > 0);
126
127     if(!(flags & XCB_REQUEST_RAW))
128     {
129         static const char pad[3];
130         int i;
131         uint16_t shortlen = 0;
132         size_t longlen = 0;
133         assert(vector[0].iov_len >= 4);
134         /* set the major opcode, and the minor opcode for extensions */
135         if(req->ext)
136         {
137             const xcb_query_extension_reply_t *extension = xcb_get_extension_data(c, req->ext);
138             if(!(extension && extension->present))
139             {
140                 _xcb_conn_shutdown(c);
141                 return 0;
142             }
143             ((uint8_t *) vector[0].iov_base)[0] = extension->major_opcode;
144             ((uint8_t *) vector[0].iov_base)[1] = req->opcode;
145         }
146         else
147             ((uint8_t *) vector[0].iov_base)[0] = req->opcode;
148
149         /* put together the length field, possibly using BIGREQUESTS */
150         for(i = 0; i < req->count; ++i)
151         {
152             longlen += vector[i].iov_len;
153             if(!vector[i].iov_base)
154             {
155                 vector[i].iov_base = (char *) pad;
156                 assert(vector[i].iov_len <= sizeof(pad));
157             }
158         }
159         assert((longlen & 3) == 0);
160         longlen >>= 2;
161
162         if(longlen <= c->setup->maximum_request_length)
163         {
164             /* we don't need BIGREQUESTS. */
165             shortlen = longlen;
166             longlen = 0;
167         }
168         else if(longlen > xcb_get_maximum_request_length(c))
169         {
170             _xcb_conn_shutdown(c);
171             return 0; /* server can't take this; maybe need BIGREQUESTS? */
172         }
173
174         /* set the length field. */
175         ((uint16_t *) vector[0].iov_base)[1] = shortlen;
176         if(!shortlen)
177             prefix[2] = ++longlen;
178     }
179     flags &= ~XCB_REQUEST_RAW;
180
181     /* do we need to work around the X server bug described in glx.xml? */
182     /* XXX: GetFBConfigs won't use BIG-REQUESTS in any sane
183      * configuration, but that should be handled here anyway. */
184     if(req->ext && !req->isvoid && !strcmp(req->ext->name, "GLX") &&
185             ((req->opcode == 17 && ((uint32_t *) vector[0].iov_base)[1] == 0x10004) ||
186              req->opcode == 21))
187         workaround = WORKAROUND_GLX_GET_FB_CONFIGS_BUG;
188
189     /* get a sequence number and arrange for delivery. */
190     _xcb_lock_io(c);
191     /* wait for other writing threads to get out of my way. */
192     while(c->out.writing)
193         pthread_cond_wait(&c->out.cond, &c->iolock);
194
195     request = ++c->out.request;
196     /* send GetInputFocus (sync) when 64k-2 requests have been sent without
197      * a reply.
198      * Also send sync (could use NoOp) at 32-bit wrap to avoid having
199      * applications see sequence 0 as that is used to indicate
200      * an error in sending the request */
201     while((req->isvoid &&
202         c->out.request == c->in.request_expected + (1 << 16) - 1) ||
203        request == 0)
204     {
205         prefix[0] = sync.packet;
206         _xcb_in_expect_reply(c, request, WORKAROUND_NONE, XCB_REQUEST_DISCARD_REPLY);
207         c->in.request_expected = c->out.request;
208         request = ++c->out.request;
209     }
210
211     if(workaround != WORKAROUND_NONE || flags != 0)
212         _xcb_in_expect_reply(c, request, workaround, flags);
213     if(!req->isvoid)
214         c->in.request_expected = c->out.request;
215
216     if(prefix[0] || prefix[2])
217     {
218         --vector, ++veclen;
219         if(prefix[2])
220         {
221             prefix[1] = ((uint32_t *) vector[1].iov_base)[0];
222             vector[1].iov_base = (uint32_t *) vector[1].iov_base + 1;
223             vector[1].iov_len -= sizeof(uint32_t);
224         }
225         vector[0].iov_len = sizeof(uint32_t) * (prefix[0] ? 1 : 0 | prefix[2] ? 2 : 0);
226         vector[0].iov_base = prefix + !prefix[0];
227     }
228
229     if(!write_block(c, vector, veclen))
230     {
231         _xcb_conn_shutdown(c);
232         request = 0;
233     }
234     _xcb_unlock_io(c);
235     return request;
236 }
237
238 int xcb_flush(xcb_connection_t *c)
239 {
240     int ret;
241     if(c->has_error)
242         return 0;
243     _xcb_lock_io(c);
244     ret = _xcb_out_flush_to(c, c->out.request);
245     _xcb_unlock_io(c);
246     return ret;
247 }
248
249 /* Private interface */
250
251 int _xcb_out_init(_xcb_out *out)
252 {
253     if(pthread_cond_init(&out->cond, 0))
254         return 0;
255     out->writing = 0;
256
257     out->queue_len = 0;
258
259     out->request = 0;
260     out->request_written = 0;
261
262     if(pthread_mutex_init(&out->reqlenlock, 0))
263         return 0;
264     out->maximum_request_length_tag = LAZY_NONE;
265
266     return 1;
267 }
268
269 void _xcb_out_destroy(_xcb_out *out)
270 {
271     pthread_cond_destroy(&out->cond);
272     pthread_mutex_destroy(&out->reqlenlock);
273 }
274
275 int _xcb_out_send(xcb_connection_t *c, struct iovec **vector, int *count)
276 {
277     int ret = 1;
278     while(ret && *count)
279         ret = _xcb_conn_wait(c, &c->out.cond, vector, count);
280     c->out.request_written = c->out.request;
281     pthread_cond_broadcast(&c->out.cond);
282     return ret;
283 }
284
285 int _xcb_out_flush_to(xcb_connection_t *c, unsigned int request)
286 {
287     assert(XCB_SEQUENCE_COMPARE(request, <=, c->out.request));
288     if(XCB_SEQUENCE_COMPARE(c->out.request_written, >=, request))
289         return 1;
290     if(c->out.queue_len)
291     {
292         struct iovec vec, *vec_ptr = &vec;
293         int count = 1;
294         vec.iov_base = c->out.queue;
295         vec.iov_len = c->out.queue_len;
296         c->out.queue_len = 0;
297         return _xcb_out_send(c, &vec_ptr, &count);
298     }
299     while(c->out.writing)
300         pthread_cond_wait(&c->out.cond, &c->iolock);
301     assert(XCB_SEQUENCE_COMPARE(c->out.request_written, >=, request));
302     return 1;
303 }