Bugfix: move request_written update *before* _xcb_conn_wait in _xcb_out_flush. Otherw...
[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 <string.h>
31 #include <errno.h>
32
33 #include "xcb.h"
34 #include "xcbext.h"
35 #include "xcbint.h"
36 #include "extensions/bigreq.h"
37
38 static int force_sequence_wrap(XCBConnection *c)
39 {
40     int ret = 1;
41     if((c->out.request - c->in.request_read) > 65530)
42     {
43         pthread_mutex_unlock(&c->iolock);
44         ret = XCBSync(c, 0);
45         pthread_mutex_lock(&c->iolock);
46     }
47     return ret;
48 }
49
50 /* Public interface */
51
52 CARD32 XCBGetMaximumRequestLength(XCBConnection *c)
53 {
54     pthread_mutex_lock(&c->out.reqlenlock);
55     if(!c->out.maximum_request_length)
56     {
57         const XCBQueryExtensionRep *ext;
58         c->out.maximum_request_length = c->setup->maximum_request_length;
59         ext = XCBGetExtensionData(c, &XCBBigRequestsId);
60         if(ext && ext->present)
61         {
62             XCBBigRequestsEnableRep *r = XCBBigRequestsEnableReply(c, XCBBigRequestsEnable(c), 0);
63             c->out.maximum_request_length = r->maximum_request_length;
64             free(r);
65         }
66     }
67     pthread_mutex_unlock(&c->out.reqlenlock);
68     return c->out.maximum_request_length;
69 }
70
71 int XCBSendRequest(XCBConnection *c, unsigned int *request, struct iovec *vector, const XCBProtocolRequest *req)
72 {
73     int ret;
74     int i;
75     struct iovec prefix[2];
76     CARD16 shortlen = 0;
77     CARD32 longlen = 0;
78     enum workarounds workaround = WORKAROUND_NONE;
79
80     assert(c != 0);
81     assert(request != 0);
82     assert(vector != 0);
83     assert(req->count > 0);
84
85     /* put together the length field, possibly using BIGREQUESTS */
86     for(i = 0; i < req->count; ++i)
87         longlen += XCB_CEIL(vector[i].iov_len) >> 2;
88
89     if(longlen > c->setup->maximum_request_length)
90     {
91         if(longlen > XCBGetMaximumRequestLength(c))
92             return 0; /* server can't take this; maybe need BIGREQUESTS? */
93     }
94     else
95     {
96         /* we don't need BIGREQUESTS. */
97         shortlen = longlen;
98         longlen = 0;
99     }
100
101     /* set the length field. */
102     i = 0;
103     prefix[i].iov_base = vector[0].iov_base;
104     prefix[i].iov_len = sizeof(CARD32);
105     vector[0].iov_base = ((char *) vector[0].iov_base) + sizeof(CARD32);
106     vector[0].iov_len -= sizeof(CARD32);
107     ((CARD16 *) prefix[i].iov_base)[1] = shortlen;
108     ++i;
109     if(!shortlen)
110     {
111         ++longlen;
112         prefix[i].iov_base = &longlen;
113         prefix[i].iov_len = sizeof(CARD32);
114         ++i;
115     }
116
117     /* set the major opcode, and the minor opcode for extensions */
118     if(req->ext)
119     {
120         const XCBQueryExtensionRep *extension = XCBGetExtensionData(c, req->ext);
121         /* TODO: better error handling here, please! */
122         assert(extension && extension->present);
123         ((CARD8 *) prefix[0].iov_base)[0] = extension->major_opcode;
124         ((CARD8 *) prefix[0].iov_base)[1] = req->opcode;
125
126         /* do we need to work around the X server bug described in glx.xml? */
127         if(strcmp(req->ext->name, "GLX") &&
128                 ((req->opcode == 17 && ((CARD32 *) vector[0].iov_base)[0] == 0x10004) ||
129                  req->opcode == 21))
130             workaround = WORKAROUND_GLX_GET_FB_CONFIGS_BUG;
131     }
132     else
133         ((CARD8 *) prefix[0].iov_base)[0] = req->opcode;
134
135     /* get a sequence number and arrange for delivery. */
136     pthread_mutex_lock(&c->iolock);
137     if(req->isvoid && !force_sequence_wrap(c))
138     {
139         pthread_mutex_unlock(&c->iolock);
140         return -1;
141     }
142
143     *request = ++c->out.request;
144
145     if(!req->isvoid)
146         _xcb_in_expect_reply(c, *request, workaround);
147
148     ret = _xcb_out_write_block(c, prefix, i);
149     if(ret > 0)
150         ret = _xcb_out_write_block(c, vector, req->count);
151     pthread_mutex_unlock(&c->iolock);
152
153     return ret;
154 }
155
156 int XCBFlush(XCBConnection *c)
157 {
158     int ret;
159     pthread_mutex_lock(&c->iolock);
160     ret = _xcb_out_flush(c);
161     pthread_mutex_unlock(&c->iolock);
162     return ret;
163 }
164
165 /* Private interface */
166
167 int _xcb_out_init(_xcb_out *out)
168 {
169     if(pthread_cond_init(&out->cond, 0))
170         return 0;
171     out->writing = 0;
172
173     out->queue_len = 0;
174     out->vec = 0;
175     out->vec_len = 0;
176
177     out->request = 0;
178     out->request_written = 0;
179
180     if(pthread_mutex_init(&out->reqlenlock, 0))
181         return 0;
182     out->maximum_request_length = 0;
183
184     return 1;
185 }
186
187 void _xcb_out_destroy(_xcb_out *out)
188 {
189     pthread_cond_destroy(&out->cond);
190     pthread_mutex_destroy(&out->reqlenlock);
191     free(out->vec);
192 }
193
194 int _xcb_out_write(XCBConnection *c)
195 {
196     int n;
197     if(c->out.vec_len)
198         n = _xcb_writev(c->fd, c->out.vec, c->out.vec_len);
199     else
200         n = _xcb_write(c->fd, &c->out.queue, &c->out.queue_len);
201
202     if(n < 0 && errno == EAGAIN)
203         n = 1;
204
205     if(c->out.vec_len)
206     {
207         int i;
208         for(i = 0; i < c->out.vec_len; ++i)
209             if(c->out.vec[i].iov_len)
210                 return n;
211         c->out.vec_len = 0;
212     }
213     return n;
214 }
215
216 int _xcb_out_write_block(XCBConnection *c, struct iovec *vector, size_t count)
217 {
218     static const char pad[3];
219     int i;
220     int len = 0;
221
222     for(i = 0; i < count; ++i)
223         len += XCB_CEIL(vector[i].iov_len);
224
225     /* Is the queue about to overflow? */
226     if(c->out.queue_len + len < sizeof(c->out.queue))
227     {
228         /* No, this will fit. */
229         for(i = 0; i < count; ++i)
230         {
231             memcpy(c->out.queue + c->out.queue_len, vector[i].iov_base, vector[i].iov_len);
232             if(vector[i].iov_len & 3)
233                 memset(c->out.queue + c->out.queue_len + vector[i].iov_len, 0, XCB_PAD(vector[i].iov_len));
234             c->out.queue_len += XCB_CEIL(vector[i].iov_len);
235         }
236         return len;
237     }
238
239     assert(!c->out.vec_len);
240     assert(!c->out.vec);
241     c->out.vec = malloc(sizeof(struct iovec) * (1 + count * 2));
242     if(!c->out.vec)
243         return -1;
244     if(c->out.queue_len)
245     {
246         c->out.vec[c->out.vec_len].iov_base = c->out.queue;
247         c->out.vec[c->out.vec_len++].iov_len = c->out.queue_len;
248         c->out.queue_len = 0;
249     }
250     for(i = 0; i < count; ++i)
251     {
252         if(!vector[i].iov_len)
253             continue;
254         c->out.vec[c->out.vec_len].iov_base = vector[i].iov_base;
255         c->out.vec[c->out.vec_len++].iov_len = vector[i].iov_len;
256         if(!XCB_PAD(vector[i].iov_len))
257             continue;
258         c->out.vec[c->out.vec_len].iov_base = (void *) pad;
259         c->out.vec[c->out.vec_len++].iov_len = XCB_PAD(vector[i].iov_len);
260     }
261     if(_xcb_out_flush(c) <= 0)
262         len = -1;
263     free(c->out.vec);
264     c->out.vec = 0;
265
266     return len;
267 }
268
269 int _xcb_out_flush(XCBConnection *c)
270 {
271     int ret = 1;
272     c->out.request_written = c->out.request;
273     while(ret > 0 && (c->out.queue_len || c->out.vec_len))
274         ret = _xcb_conn_wait(c, /*should_write*/ 1, &c->out.cond);
275     pthread_cond_broadcast(&c->out.cond);
276     return ret;
277 }