X-Git-Url: http://git.demorecorder.com/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=src%2Fxcb_in.c;h=4191dc3ff07a510451cf4e6a38779dc62482c563;hb=36c9a985aaee655c118c9f7b8425d3ac9ce0f840;hp=26ab3581f96955f77f38fbdd9ea334bf6ed1aafa;hpb=f916062edf0e04cd4e0a78f6975892f59fae3b60;p=free-sw%2Fxcb%2Flibxcb diff --git a/src/xcb_in.c b/src/xcb_in.c index 26ab358..4191dc3 100644 --- a/src/xcb_in.c +++ b/src/xcb_in.c @@ -37,10 +37,14 @@ #include "xcbint.h" #if USE_POLL #include -#else +#elif !defined _WIN32 #include #endif +#ifdef _WIN32 +#include "xcb_windefs.h" +#endif /* _WIN32 */ + #define XCB_ERROR 0 #define XCB_REPLY 1 #define XCB_XGE_EVENT 35 @@ -267,12 +271,22 @@ static int read_block(const int fd, void *buf, const ssize_t len) int done = 0; while(done < len) { +#ifndef _WIN32 int ret = read(fd, ((char *) buf) + done, len - done); +#else + int ret = recv(fd, ((char *) buf) + done, len - done,0); +#endif /* !_WIN32 */ + if(ret > 0) done += ret; +#ifndef _WIN32 if(ret < 0 && errno == EAGAIN) +#else + if(ret == SOCKET_ERROR && WSAGetLastError() == WSAEWOULDBLOCK) +#endif /* !_Win32 */ { #if USE_POLL +#ifndef _WIN32 struct pollfd pfd; pfd.fd = fd; pfd.events = POLLIN; @@ -280,14 +294,21 @@ static int read_block(const int fd, void *buf, const ssize_t len) do { ret = poll(&pfd, 1, -1); } while (ret == -1 && errno == EINTR); +#endif /* !_WIN32 */ #else fd_set fds; FD_ZERO(&fds); FD_SET(fd, &fds); +#ifndef _WIN32 do { ret = select(fd + 1, &fds, 0, 0, 0); } while (ret == -1 && errno == EINTR); -#endif +#else + /* the do while loop used for the non-windows version isn't required*/ + /* for windows since there are no signals in Windows hence no EINTR*/ + ret = select(fd + 1, &fds, 0, 0, 0); +#endif /* !_WIN32 */ +#endif /* USE_POLL */ } if(ret <= 0) return ret; @@ -409,6 +430,116 @@ void *xcb_wait_for_reply(xcb_connection_t *c, unsigned int request, xcb_generic_ return ret; } +static void insert_pending_discard(xcb_connection_t *c, pending_reply **prev_next, uint64_t seq) +{ + pending_reply *pend; + pend = malloc(sizeof(*pend)); + if(!pend) + { + _xcb_conn_shutdown(c); + return; + } + + pend->first_request = seq; + pend->last_request = seq; + pend->workaround = 0; + pend->flags = XCB_REQUEST_DISCARD_REPLY; + pend->next = *prev_next; + *prev_next = pend; + + if(!pend->next) + c->in.pending_replies_tail = &pend->next; +} + +static void discard_reply(xcb_connection_t *c, unsigned int request) +{ + pending_reply *pend = 0; + pending_reply **prev_pend; + uint64_t widened_request; + + /* We've read requests past the one we want, so if it has replies we have + * them all and they're in the replies map. */ + if(XCB_SEQUENCE_COMPARE_32(request, <, c->in.request_read)) + { + struct reply_list *head; + head = _xcb_map_remove(c->in.replies, request); + while (head) + { + struct reply_list *next = head->next; + free(head->reply); + free(head); + head = next; + } + return; + } + + /* We're currently processing the responses to the request we want, and we + * have a reply ready to return. Free it, and mark the pend to free any further + * replies. */ + if(XCB_SEQUENCE_COMPARE_32(request, ==, c->in.request_read) && c->in.current_reply) + { + struct reply_list *head; + head = c->in.current_reply; + c->in.current_reply = NULL; + c->in.current_reply_tail = &c->in.current_reply; + while (head) + { + struct reply_list *next = head->next; + free(head->reply); + free(head); + head = next; + } + + pend = c->in.pending_replies; + if(pend && + !(XCB_SEQUENCE_COMPARE(pend->first_request, <=, c->in.request_read) && + (pend->workaround == WORKAROUND_EXTERNAL_SOCKET_OWNER || + XCB_SEQUENCE_COMPARE(c->in.request_read, <=, pend->last_request)))) + pend = 0; + if(pend) + pend->flags |= XCB_REQUEST_DISCARD_REPLY; + else + insert_pending_discard(c, &c->in.pending_replies, c->in.request_read); + + return; + } + + /* Walk the list of pending requests. Mark the first match for deletion. */ + for(prev_pend = &c->in.pending_replies; *prev_pend; prev_pend = &(*prev_pend)->next) + { + if(XCB_SEQUENCE_COMPARE_32((*prev_pend)->first_request, >, request)) + break; + + if(XCB_SEQUENCE_COMPARE_32((*prev_pend)->first_request, ==, request)) + { + /* Pending reply found. Mark for discard: */ + (*prev_pend)->flags |= XCB_REQUEST_DISCARD_REPLY; + return; + } + } + + /* Pending reply not found (likely due to _unchecked request). Create one: */ + widened_request = (c->out.request & UINT64_C(0xffffffff00000000)) | request; + if(widened_request > c->out.request) + widened_request -= UINT64_C(1) << 32; + + insert_pending_discard(c, prev_pend, widened_request); +} + +void xcb_discard_reply(xcb_connection_t *c, unsigned int sequence) +{ + if(c->has_error) + return; + + /* If an error occurred when issuing the request, fail immediately. */ + if(!sequence) + return; + + pthread_mutex_lock(&c->iolock); + discard_reply(c, sequence); + pthread_mutex_unlock(&c->iolock); +} + int xcb_poll_for_reply(xcb_connection_t *c, unsigned int request, void **reply, xcb_generic_error_t **error) { int ret; @@ -555,12 +686,20 @@ void _xcb_in_replies_done(xcb_connection_t *c) int _xcb_in_read(xcb_connection_t *c) { +#ifndef _WIN32 int n = read(c->fd, c->in.queue + c->in.queue_len, sizeof(c->in.queue) - c->in.queue_len); +#else + int n = recv(c->fd, c->in.queue + c->in.queue_len, sizeof(c->in.queue) - c->in.queue_len,0); +#endif /* !_WIN32 */ if(n > 0) c->in.queue_len += n; while(read_packet(c)) /* empty */; +#ifndef _WIN32 if((n > 0) || (n < 0 && errno == EAGAIN)) +#else + if((n > 0) || (n < 0 && WSAGetLastError() == WSAEWOULDBLOCK)) +#endif /* !_WIN32 */ return 1; _xcb_conn_shutdown(c); return 0;