#include <netinet/in.h>
#include <sys/select.h>
#include <sys/fcntl.h>
+#include <errno.h>
#include "xcb.h"
#include "xcbint.h"
assert(count <= sizeof(parts) / sizeof(*parts));
pthread_mutex_lock(&c->iolock);
- _xcb_out_write_block(c, parts, count);
- ret = _xcb_out_flush(c);
+ {
+ struct iovec *parts_ptr = parts;
+ ret = _xcb_out_send(c, &parts_ptr, &count);
+ }
pthread_mutex_unlock(&c->iolock);
return ret;
}
return 1;
}
+/* precondition: there must be something for us to write. */
+static int write_vec(XCBConnection *c, struct iovec **vector, int *count)
+{
+ int n;
+ assert(!c->out.queue_len);
+ n = writev(c->fd, *vector, *count);
+ if(n < 0 && errno == EAGAIN)
+ return 1;
+ if(n <= 0)
+ return 0;
+
+ for(; *count; --*count, ++*vector)
+ {
+ int cur = (*vector)->iov_len;
+ if(cur > n)
+ cur = n;
+ (*vector)->iov_len -= cur;
+ (*vector)->iov_base = (char *) (*vector)->iov_base + cur;
+ n -= cur;
+ if((*vector)->iov_len)
+ break;
+ }
+ if(!*count)
+ *vector = 0;
+ assert(n == 0);
+ return 1;
+}
+
/* Public interface */
XCBConnSetupSuccessRep *XCBGetSetup(XCBConnection *c)
/* Private interface */
-int _xcb_conn_wait(XCBConnection *c, const int should_write, pthread_cond_t *cond)
+int _xcb_conn_wait(XCBConnection *c, pthread_cond_t *cond, struct iovec **vector, int *count)
{
int ret;
fd_set rfds, wfds;
/* If the thing I should be doing is already being done, wait for it. */
- if(should_write ? c->out.writing : c->in.reading)
+ if(count ? c->out.writing : c->in.reading)
{
pthread_cond_wait(cond, &c->iolock);
return 1;
++c->in.reading;
FD_ZERO(&wfds);
- if(should_write)
+ if(count)
{
FD_SET(c->fd, &wfds);
++c->out.writing;
ret = ret && _xcb_in_read(c);
if(FD_ISSET(c->fd, &wfds))
- ret = ret && _xcb_out_write(c);
+ ret = ret && write_vec(c, vector, count);
}
- if(should_write)
+ if(count)
--c->out.writing;
--c->in.reading;
pthread_mutex_lock(&c->iolock);
/* If this request has not been written yet, write it. */
- if((signed int) (c->out.request_written - request) < 0)
- if(!_xcb_out_flush(c))
- goto done; /* error */
+ if(!_xcb_out_flush_to(c, request))
+ goto done; /* error */
for(prev_reader = &c->in.readers; *prev_reader && (*prev_reader)->request <= request; prev_reader = &(*prev_reader)->next)
if((*prev_reader)->request == request)
* wait for one. */
while(c->in.request_completed < request &&
!(c->in.request_read == request && c->in.current_reply))
- if(!_xcb_conn_wait(c, /*should_write*/ 0, &cond))
+ if(!_xcb_conn_wait(c, &cond, 0, 0))
goto done;
if(c->in.request_read != request)
pthread_mutex_lock(&c->iolock);
/* get_event returns 0 on empty list. */
while(!(ret = get_event(c)))
- if(!_xcb_conn_wait(c, /*should_write*/ 0, &c->in.event_cond))
+ if(!_xcb_conn_wait(c, &c->in.event_cond, 0, 0))
break;
wake_up_next_reader(c);
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
-#include <errno.h>
#include "xcb.h"
#include "xcbext.h"
#include "xcbint.h"
#include "extensions/bigreq.h"
+static int write_block(XCBConnection *c, struct iovec *vector, int count)
+{
+ 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);
+ c->out.queue_len += vector[0].iov_len;
+ vector[0].iov_base = (char *) vector[0].iov_base + vector[0].iov_len;
+ vector[0].iov_len = 0;
+ ++vector, --count;
+ }
+ if(!count)
+ return 1;
+
+ --vector, ++count;
+ vector[0].iov_base = c->out.queue;
+ vector[0].iov_len = c->out.queue_len;
+ c->out.queue_len = 0;
+ return _xcb_out_send(c, &vector, &count);
+}
+
/* Public interface */
CARD32 XCBGetMaximumRequestLength(XCBConnection *c)
vector[0].iov_base = prefix + !prefix[0];
}
- if(!_xcb_out_write_block(c, vector, veclen))
+ if(!write_block(c, vector, veclen))
request = 0;
pthread_mutex_unlock(&c->iolock);
return request;
{
int ret;
pthread_mutex_lock(&c->iolock);
- ret = _xcb_out_flush(c);
+ ret = _xcb_out_flush_to(c, c->out.request);
pthread_mutex_unlock(&c->iolock);
return ret;
}
out->writing = 0;
out->queue_len = 0;
- out->vec = 0;
- out->vec_len = 0;
out->request = 0;
out->request_written = 0;
pthread_mutex_destroy(&out->reqlenlock);
}
-/* precondition: there must be something for us to write. */
-int _xcb_out_write(XCBConnection *c)
+int _xcb_out_send(XCBConnection *c, struct iovec **vector, int *count)
{
- int n;
- 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;
-
- 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;
- assert(n == 0);
- return 1;
+ int ret = 1;
+ while(ret && *count)
+ ret = _xcb_conn_wait(c, &c->out.cond, vector, count);
+ c->out.request_written = c->out.request;
+ pthread_cond_broadcast(&c->out.cond);
+ return ret;
}
-int _xcb_out_write_block(XCBConnection *c, struct iovec *vector, size_t count)
+int _xcb_out_flush_to(XCBConnection *c, unsigned int request)
{
- 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);
- c->out.queue_len += vector[0].iov_len;
- vector[0].iov_base = (char *) vector[0].iov_base + vector[0].iov_len;
- vector[0].iov_len = 0;
- ++vector, --count;
- }
- if(!count)
+ assert(request <= c->out.request);
+ if(c->out.request_written >= request)
return 1;
-
- --vector, ++count;
- vector[0].iov_base = c->out.queue;
- vector[0].iov_len = c->out.queue_len;
- c->out.queue_len = 0;
-
- c->out.vec_len = count;
- c->out.vec = vector;
- return _xcb_out_flush(c);
-}
-
-int _xcb_out_flush(XCBConnection *c)
-{
- int ret = 1;
- struct iovec vec;
if(c->out.queue_len)
{
- assert(!c->out.vec && !c->out.vec_len);
+ struct iovec vec, *vec_ptr = &vec;
+ int count = 1;
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;
+ return _xcb_out_send(c, &vec_ptr, &count);
}
- 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);
- return ret;
+ while(c->out.writing)
+ pthread_cond_wait(&c->out.cond, &c->iolock);
+ assert(c->out.request_written >= request);
+ return 1;
}
char queue[4096];
int queue_len;
- struct iovec *vec;
- int vec_len;
unsigned int request;
unsigned int request_written;
int _xcb_out_init(_xcb_out *out);
void _xcb_out_destroy(_xcb_out *out);
-int _xcb_out_write(XCBConnection *c);
-int _xcb_out_write_block(XCBConnection *c, struct iovec *vector, size_t count);
-int _xcb_out_flush(XCBConnection *c);
+int _xcb_out_send(XCBConnection *c, struct iovec **vector, int *count);
+int _xcb_out_flush_to(XCBConnection *c, unsigned int request);
/* xcb_in.c */
_xcb_xid xid;
};
-int _xcb_conn_wait(XCBConnection *c, const int should_write, pthread_cond_t *cond);
+int _xcb_conn_wait(XCBConnection *c, pthread_cond_t *cond, struct iovec **vector, int *count);
#ifdef GCC_HAS_VISIBILITY
#pragma GCC visibility pop