X-Git-Url: http://git.demorecorder.com/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=src%2Fxcb_in.c;h=cacb22010e1b23f7d59df89ebc7064abd3d5a254;hb=622b599c8fcf5d677f76ca03f3241a23dba58712;hp=76990f35909a84892332a1368a8dbe67b2a01059;hpb=86ce18c22c8daebe8796d3c38e8a1d496ab6e8de;p=free-sw%2Fxcb%2Flibxcb diff --git a/src/xcb_in.c b/src/xcb_in.c index 76990f3..cacb220 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; - void *data; -} XCBReplyData; - -static int match_reply(const void *request, const void *data) -{ - return ((XCBReplyData *) data)->request == *(unsigned int *) request; -} + pthread_cond_t *data; + 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) @@ -145,7 +145,7 @@ static int read_packet(XCBConnection *c) /* 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 +153,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,7 +228,8 @@ 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) @@ -237,16 +242,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 +291,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 +365,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 +383,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;