X-Git-Url: http://git.demorecorder.com/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=src%2Fxcb_out.c;h=42050f5552e91adb3a1c1781e0cb9d0a2568315f;hb=c05ae15b661bff6b95deb3abc7f48abe16892ac8;hp=e7d5323aca503d2f6c09742bd9aa99307abc18ab;hpb=4e91ae275e46e76de64ab089d6f13c1e9b6ddee6;p=free-sw%2Fxcb%2Flibxcb diff --git a/src/xcb_out.c b/src/xcb_out.c index e7d5323..42050f5 100644 --- a/src/xcb_out.c +++ b/src/xcb_out.c @@ -58,28 +58,6 @@ static int force_sequence_wrap(XCBConnection *c) return ret; } -static int _xcb_writev(const int fd, struct iovec *vec, int count) -{ - int n = writev(fd, vec, count); - if(n > 0) - { - int rem = n; - for(; count; --count, ++vec) - { - int cur = vec->iov_len; - if(cur > rem) - cur = rem; - vec->iov_len -= cur; - vec->iov_base = (char *) vec->iov_base + cur; - rem -= cur; - if(vec->iov_len) - break; - } - assert(rem == 0); - } - return n; -} - /* Public interface */ CARD32 XCBGetMaximumRequestLength(XCBConnection *c) @@ -101,15 +79,14 @@ CARD32 XCBGetMaximumRequestLength(XCBConnection *c) return c->out.maximum_request_length; } -int XCBSendRequest(XCBConnection *c, unsigned int *request, struct iovec *vector, const XCBProtocolRequest *req) +int XCBSendRequest(XCBConnection *c, unsigned int *request, int flags, struct iovec *vector, const XCBProtocolRequest *req) { static const char pad[3]; int ret; int i; + CARD32 prefix[2]; struct iovec *padded; int padlen = 0; - CARD16 shortlen = 0; - CARD32 longlen = 0; enum workarounds workaround = WORKAROUND_NONE; assert(c != 0); @@ -117,60 +94,57 @@ int XCBSendRequest(XCBConnection *c, unsigned int *request, struct iovec *vector assert(vector != 0); assert(req->count > 0); - /* set the major opcode, and the minor opcode for extensions */ - if(req->ext) - { - const XCBQueryExtensionRep *extension = XCBGetExtensionData(c, req->ext); - /* TODO: better error handling here, please! */ - assert(extension && extension->present); - ((CARD8 *) vector[0].iov_base)[0] = extension->major_opcode; - ((CARD8 *) vector[0].iov_base)[1] = req->opcode; - - /* do we need to work around the X server bug described in glx.xml? */ - if(strcmp(req->ext->name, "GLX") && - ((req->opcode == 17 && ((CARD32 *) vector[0].iov_base)[0] == 0x10004) || - req->opcode == 21)) - workaround = WORKAROUND_GLX_GET_FB_CONFIGS_BUG; - } - else - ((CARD8 *) vector[0].iov_base)[0] = req->opcode; - - /* put together the length field, possibly using BIGREQUESTS */ - for(i = 0; i < req->count; ++i) - longlen += XCB_CEIL(vector[i].iov_len) >> 2; - - if(longlen > c->setup->maximum_request_length) - { - if(longlen > XCBGetMaximumRequestLength(c)) - return 0; /* server can't take this; maybe need BIGREQUESTS? */ - } - else - { - /* we don't need BIGREQUESTS. */ - shortlen = longlen; - longlen = 0; - } - padded = #ifdef HAVE_ALLOCA alloca #else malloc #endif - ((req->count * 2 + 3) * sizeof(struct iovec)); - /* set the length field. */ - ((CARD16 *) vector[0].iov_base)[1] = shortlen; - if(!shortlen) + ((req->count * 2 + 2) * sizeof(struct iovec)); + + if(!(flags & XCB_REQUEST_RAW)) { - padded[0].iov_base = vector[0].iov_base; - padded[0].iov_len = sizeof(CARD32); - vector[0].iov_base = ((char *) vector[0].iov_base) + sizeof(CARD32); - vector[0].iov_len -= sizeof(CARD32); - ++longlen; - padded[1].iov_base = &longlen; - padded[1].iov_len = sizeof(CARD32); - padlen = 2; + CARD16 shortlen = 0; + CARD32 longlen = 0; + /* set the major opcode, and the minor opcode for extensions */ + if(req->ext) + { + const XCBQueryExtensionRep *extension = XCBGetExtensionData(c, req->ext); + /* TODO: better error handling here, please! */ + assert(extension && extension->present); + ((CARD8 *) vector[0].iov_base)[0] = extension->major_opcode; + ((CARD8 *) vector[0].iov_base)[1] = req->opcode; + } + else + ((CARD8 *) vector[0].iov_base)[0] = req->opcode; + + /* put together the length field, possibly using BIGREQUESTS */ + for(i = 0; i < req->count; ++i) + longlen += (vector[i].iov_len + 3) >> 2; + + if(longlen <= c->setup->maximum_request_length) + { + /* we don't need BIGREQUESTS. */ + shortlen = longlen; + longlen = 0; + } + else if(longlen > XCBGetMaximumRequestLength(c)) + return 0; /* server can't take this; maybe need BIGREQUESTS? */ + + /* set the length field. */ + ((CARD16 *) vector[0].iov_base)[1] = shortlen; + if(!shortlen) + { + padded[0].iov_base = prefix; + padded[0].iov_len = sizeof(prefix); + prefix[0] = ((CARD32 *) vector[0].iov_base)[0]; + prefix[1] = ++longlen; + vector[0].iov_base = ((char *) vector[0].iov_base) + sizeof(CARD32); + vector[0].iov_len -= sizeof(CARD32); + padlen = 1; + } } + flags &= ~XCB_REQUEST_RAW; for(i = 0; i < req->count; ++i) { @@ -184,6 +158,12 @@ int XCBSendRequest(XCBConnection *c, unsigned int *request, struct iovec *vector padded[padlen++].iov_len = XCB_PAD(vector[i].iov_len); } + /* do we need to work around the X server bug described in glx.xml? */ + if(req->ext && !req->isvoid && strcmp(req->ext->name, "GLX") && + ((req->opcode == 17 && ((CARD32 *) vector[0].iov_base)[0] == 0x10004) || + req->opcode == 21)) + workaround = WORKAROUND_GLX_GET_FB_CONFIGS_BUG; + /* get a sequence number and arrange for delivery. */ pthread_mutex_lock(&c->iolock); if(req->isvoid && !force_sequence_wrap(c)) @@ -195,10 +175,13 @@ int XCBSendRequest(XCBConnection *c, unsigned int *request, struct iovec *vector return -1; } + /* wait for other writing threads to get out of my way. */ + while(c->out.writing) + pthread_cond_wait(&c->out.cond, &c->iolock); + *request = ++c->out.request; - if(!req->isvoid) - _xcb_in_expect_reply(c, *request, workaround); + _xcb_in_expect_reply(c, *request, workaround, flags); ret = _xcb_out_write_block(c, padded, padlen); pthread_mutex_unlock(&c->iolock); @@ -251,16 +234,32 @@ int _xcb_out_write(XCBConnection *c) { int n; assert(!c->out.queue_len); - n = _xcb_writev(c->fd, c->out.vec, c->out.vec_len); - while(c->out.vec_len && !c->out.vec[0].iov_len) - ++c->out.vec, --c->out.vec_len; + n = writev(c->fd, c->out.vec, c->out.vec_len); + if(n < 0 && errno == EAGAIN) + return 1; + if(n <= 0) + return 0; + + for(; c->out.vec_len; --c->out.vec_len, ++c->out.vec) + { + int cur = c->out.vec->iov_len; + if(cur > n) + cur = n; + c->out.vec->iov_len -= cur; + c->out.vec->iov_base = (char *) c->out.vec->iov_base + cur; + n -= cur; + if(c->out.vec->iov_len) + break; + } if(!c->out.vec_len) c->out.vec = 0; - return (n > 0) || (n < 0 && errno == EAGAIN); + assert(n == 0); + return 1; } int _xcb_out_write_block(XCBConnection *c, struct iovec *vector, size_t count) { + assert(!c->out.vec && !c->out.vec_len); while(count && c->out.queue_len + vector[0].iov_len < sizeof(c->out.queue)) { memcpy(c->out.queue + c->out.queue_len, vector[0].iov_base, vector[0].iov_len); @@ -275,8 +274,6 @@ int _xcb_out_write_block(XCBConnection *c, struct iovec *vector, size_t count) vector[0].iov_len = c->out.queue_len; c->out.queue_len = 0; - assert(!c->out.vec_len); - assert(!c->out.vec); c->out.vec_len = count; c->out.vec = vector; return _xcb_out_flush(c); @@ -288,8 +285,7 @@ int _xcb_out_flush(XCBConnection *c) struct iovec vec; if(c->out.queue_len) { - assert(!c->out.vec_len); - assert(!c->out.vec); + assert(!c->out.vec && !c->out.vec_len); vec.iov_base = c->out.queue; vec.iov_len = c->out.queue_len; c->out.vec = &vec;