X-Git-Url: http://git.demorecorder.com/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=src%2Fxcb_in.c;h=973a0d2fe097da5e2cdeca301492c672d9bd7bc2;hb=29f9fe0fc805a1ec6860f167a45664cc1cf0c769;hp=92bc0bf5ca4b9ed598bb0fb606808c82a62c202f;hpb=ff665b57266b9e7e9b9a366272c2115bbd516173;p=free-sw%2Fxcb%2Flibxcb diff --git a/src/xcb_in.c b/src/xcb_in.c index 92bc0bf..973a0d2 100644 --- a/src/xcb_in.c +++ b/src/xcb_in.c @@ -53,22 +53,17 @@ typedef struct pending_reply { struct pending_reply *next; } pending_reply; -typedef struct XCBReplyData { +typedef struct reader_list { unsigned int request; pthread_cond_t *data; -} XCBReplyData; - -static int match_reply(const void *request, const void *data) -{ - return ((XCBReplyData *) data)->request == *(unsigned int *) request; -} + struct reader_list *next; +} reader_list; static void wake_up_next_reader(XCBConnection *c) { - XCBReplyData *cur = _xcb_list_peek_head(c->in.readers); int pthreadret; - if(cur) - pthreadret = pthread_cond_signal(cur->data); + if(c->in.readers) + pthreadret = pthread_cond_signal(c->in.readers->data); else pthreadret = pthread_cond_signal(&c->in.event_cond); assert(pthreadret == 0); @@ -94,25 +89,30 @@ static int read_packet(XCBConnection *c) { int lastread = c->in.request_read; c->in.request_read = (lastread & 0xffff0000) | genrep.sequence; + if(c->in.request_read < lastread) + c->in.request_read += 0x10000; + if(c->in.request_read != lastread) { - pending_reply *oldpend = c->in.pending_replies; - if(oldpend && oldpend->request == lastread) - { - c->in.pending_replies = oldpend->next; - if(!oldpend->next) - c->in.pending_replies_tail = &c->in.pending_replies; - free(oldpend); - } if(c->in.current_reply) { _xcb_map_put(c->in.replies, lastread, c->in.current_reply); c->in.current_reply = 0; c->in.current_reply_tail = &c->in.current_reply; } + c->in.request_completed = c->in.request_read - 1; + } + if(genrep.response_type != 1) /* not reply: error or event */ + c->in.request_completed = c->in.request_read; /* XXX: does event/error imply no more replies? */ + + while(c->in.pending_replies && c->in.pending_replies->request <= c->in.request_completed) + { + pending_reply *oldpend = c->in.pending_replies; + c->in.pending_replies = oldpend->next; + if(!oldpend->next) + c->in.pending_replies_tail = &c->in.pending_replies; + free(oldpend); } - if(c->in.request_read < lastread) - c->in.request_read += 0x10000; } if(genrep.response_type == 0 || genrep.response_type == 1) @@ -133,7 +133,7 @@ static int read_packet(XCBConnection *c) length += genrep.length * 4; } - buf = malloc(length); + buf = malloc(length + (genrep.response_type == 1 ? 0 : sizeof(CARD32))); if(!buf) return 0; if(_xcb_in_read_block(c, buf, length) <= 0) @@ -142,10 +142,13 @@ static int read_packet(XCBConnection *c) return 0; } + if(genrep.response_type != 1) + ((XCBGenericEvent *) buf)->full_sequence = c->in.request_read; + /* reply, or checked error */ if(genrep.response_type == 1 || (genrep.response_type == 0 && pend && (pend->flags & XCB_REQUEST_CHECKED))) { - XCBReplyData *reader = _xcb_list_find(c->in.readers, match_reply, &c->in.request_read); + reader_list *reader; struct reply_list *cur = malloc(sizeof(struct reply_list)); if(!cur) return 0; @@ -153,8 +156,12 @@ static int read_packet(XCBConnection *c) cur->next = 0; *c->in.current_reply_tail = cur; c->in.current_reply_tail = &cur->next; - if(reader) - pthread_cond_signal(reader->data); + for(reader = c->in.readers; reader && reader->request <= c->in.request_read; reader = reader->next) + if(reader->request == c->in.request_read) + { + pthread_cond_signal(reader->data); + break; + } return 1; } @@ -224,12 +231,17 @@ static int read_block(const int fd, void *buf, const size_t len) void *XCBWaitForReply(XCBConnection *c, unsigned int request, XCBGenericError **e) { pthread_cond_t cond = PTHREAD_COND_INITIALIZER; - XCBReplyData reader; + reader_list reader; + reader_list **prev_reader; struct reply_list *head; void *ret = 0; if(e) *e = 0; + /* If an error occurred when issuing the request, fail immediately. */ + if(!request) + return 0; + pthread_mutex_lock(&c->iolock); /* If this request has not been written yet, write it. */ @@ -237,16 +249,19 @@ void *XCBWaitForReply(XCBConnection *c, unsigned int request, XCBGenericError ** if(!_xcb_out_flush(c)) goto done; /* error */ - if(_xcb_list_find(c->in.readers, match_reply, &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) + goto done; /* error */ reader.request = request; reader.data = &cond; - _xcb_list_append(c->in.readers, &reader); + reader.next = *prev_reader; + *prev_reader = &reader; - /* If this request has not been read yet, wait for it. */ - while(((signed int) (c->in.request_read - request) < 0 || - (c->in.request_read == request && !c->in.current_reply))) + /* If this request has not completed yet and has no reply waiting, + * 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)) goto done; @@ -283,7 +298,12 @@ void *XCBWaitForReply(XCBConnection *c, unsigned int request, XCBGenericError ** } done: - _xcb_list_remove(c->in.readers, match_reply, &request); + for(prev_reader = &c->in.readers; *prev_reader && (*prev_reader)->request <= request; prev_reader = &(*prev_reader)->next) + if(*prev_reader == &reader) + { + *prev_reader = (*prev_reader)->next; + break; + } pthread_cond_destroy(&cond); wake_up_next_reader(c); @@ -352,10 +372,10 @@ int _xcb_in_init(_xcb_in *in) in->queue_len = 0; in->request_read = 0; + in->request_completed = 0; in->replies = _xcb_map_new(); - in->readers = _xcb_list_new(); - if(!in->replies || !in->readers) + if(!in->replies) return 0; in->current_reply_tail = &in->current_reply; @@ -370,7 +390,6 @@ void _xcb_in_destroy(_xcb_in *in) pthread_cond_destroy(&in->event_cond); free_reply_list(in->current_reply); _xcb_map_delete(in->replies, (void (*)(void *)) free_reply_list); - _xcb_list_delete(in->readers, 0); while(in->events) { struct event_list *e = in->events;