X-Git-Url: http://git.demorecorder.com/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=src%2Fxcb_out.c;h=762efbd20eb41b448eb0515db1dac11a828a9d08;hb=731c85762d8994119f4eaf572cc59c9adbb7abd9;hp=cecf7cb19398251d221d9b6b7fdad181be255fba;hpb=7f0bc778c88ab2f565cc05d5d3d5ee4c8d1388a1;p=free-sw%2Fxcb%2Flibxcb diff --git a/src/xcb_out.c b/src/xcb_out.c index cecf7cb..762efbd 100644 --- a/src/xcb_out.c +++ b/src/xcb_out.c @@ -58,40 +58,6 @@ static int force_sequence_wrap(XCBConnection *c) return ret; } -static int _xcb_write(const int fd, char (*buf)[], int *count) -{ - int n = write(fd, *buf, *count); - if(n > 0) - { - *count -= n; - if(*count) - memmove(*buf, *buf + n, *count); - } - return n; -} - -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) @@ -113,7 +79,7 @@ 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; @@ -139,7 +105,7 @@ int XCBSendRequest(XCBConnection *c, unsigned int *request, struct iovec *vector ((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") && + if(!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; @@ -149,19 +115,16 @@ int XCBSendRequest(XCBConnection *c, unsigned int *request, struct iovec *vector /* put together the length field, possibly using BIGREQUESTS */ for(i = 0; i < req->count; ++i) - longlen += XCB_CEIL(vector[i].iov_len) >> 2; + longlen += (vector[i].iov_len + 3) >> 2; - if(longlen > c->setup->maximum_request_length) - { - if(longlen > XCBGetMaximumRequestLength(c)) - return 0; /* server can't take this; maybe need BIGREQUESTS? */ - } - else + 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? */ padded = #ifdef HAVE_ALLOCA @@ -207,10 +170,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); @@ -256,35 +222,39 @@ void _xcb_out_destroy(_xcb_out *out) { pthread_cond_destroy(&out->cond); pthread_mutex_destroy(&out->reqlenlock); - free(out->vec); } +/* precondition: there must be something for us to write. */ int _xcb_out_write(XCBConnection *c) { int n; - if(c->out.vec_len) - n = _xcb_writev(c->fd, c->out.vec, c->out.vec_len); - else - n = _xcb_write(c->fd, &c->out.queue, &c->out.queue_len); - - /* XXX: should "nothing was written" be considered failure or - * success for this function? it's not an I/O error, but... */ - n = (n > 0) || (n < 0 && errno == EAGAIN); + assert(!c->out.queue_len); + n = writev(c->fd, c->out.vec, c->out.vec_len); + if(n < 0 && errno == EAGAIN) + return 1; + if(n <= 0) + return 0; - if(c->out.vec_len) + for(; c->out.vec_len; --c->out.vec_len, ++c->out.vec) { - int i; - for(i = 0; i < c->out.vec_len; ++i) - if(c->out.vec[i].iov_len) - return n; - c->out.vec = 0; - c->out.vec_len = 0; + 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; } - return n; + if(!c->out.vec_len) + c->out.vec = 0; + 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); @@ -299,8 +269,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); @@ -309,7 +277,17 @@ int _xcb_out_write_block(XCBConnection *c, struct iovec *vector, size_t count) int _xcb_out_flush(XCBConnection *c) { int ret = 1; - while(ret && (c->out.queue_len || c->out.vec_len)) + struct iovec vec; + if(c->out.queue_len) + { + 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; + c->out.vec_len = 1; + c->out.queue_len = 0; + } + while(ret && c->out.vec_len) ret = _xcb_conn_wait(c, /*should_write*/ 1, &c->out.cond); c->out.request_written = c->out.request; pthread_cond_broadcast(&c->out.cond);