summaryrefslogtreecommitdiff
path: root/reply/reply.c
blob: 0cc75c6f64323fec9ed19a541545f542f5213660 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/*
 * Copyright © 2008 Julien Danjou <julien@danjou.info>
 *
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use, copy,
 * modify, merge, publish, distribute, sublicense, and/or sell copies
 * of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
 * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 *
 * Except as contained in this notice, the names of the authors or
 * their institutions shall not be used in advertising or otherwise to
 * promote the sale, use or other dealings in this Software without
 * prior written authorization from the authors.
 */

#include <stdlib.h>
#include <xcb/xcbext.h>

#include "xcb_reply.h"

void
xcb_reply_handlers_init(xcb_connection_t *c, xcb_reply_handlers_t *r)
{
    static const pthread_mutex_t proto_lock = PTHREAD_MUTEX_INITIALIZER;
    static const pthread_cond_t proto_cond = PTHREAD_COND_INITIALIZER;
    r->lock = proto_lock;
    r->cond = proto_cond;
    r->c = c;
    r->head = NULL;
}

xcb_connection_t *
xcb_reply_get_xcb_connection(xcb_reply_handlers_t *h)
{
    return h->c;
}

static void
insert_handler(xcb_reply_handlers_t *h, struct xcb_reply_node *cur)
{
    struct xcb_reply_node **prev = &h->head;
    while(*prev && (*prev)->request < cur->request)
        prev = &(*prev)->next;
    cur->next = *prev;
    *prev = cur;
}

static void
remove_handler(xcb_reply_handlers_t *h, struct xcb_reply_node *cur)
{
    struct xcb_reply_node **prev = &h->head;
    while(*prev && (*prev)->request < cur->request)
        prev = &(*prev)->next;
    if(!(*prev) || (*prev)->request != cur->request)
        return;
    *prev = cur->next;
    free(cur);
}

static int
process_replies(xcb_reply_handlers_t *h, int block)
{
    int handled = 0;
    pthread_mutex_lock(&h->lock);
    pthread_cleanup_push((void (*)(void *)) pthread_mutex_unlock, &h->lock);
    while(1)
    {
        struct xcb_reply_node *cur = h->head;
        xcb_generic_error_t *error;
        void *reply;
        pthread_mutex_unlock(&h->lock);
        pthread_cleanup_push((void (*)(void *)) pthread_mutex_lock, &h->lock);
        if(block)
            reply = xcb_wait_for_reply(h->c, cur->request, &error);
        else if(!xcb_poll_for_reply(h->c, cur->request, &reply, &error))
            return handled;
        if(reply || error)
        {
            cur->handler(cur->data, h->c, reply, error);
            cur->handled = 1;
            free(reply);
            free(error);
        }
        handled |= cur->handled;
        pthread_cleanup_pop(1);
        if(!reply)
            remove_handler(h, cur);
        if(!h->head)
        {
            if(block)
                pthread_cond_wait(&h->cond, &h->lock);
            else
                break;
        }
    }
    pthread_cleanup_pop(1);
    return handled;
}

static void *
reply_thread(void *h)
{
    process_replies(h, 1);
    return 0;
}

void
xcb_reply_start_thread(xcb_reply_handlers_t *h)
{
    pthread_create(&h->thread, 0, reply_thread, h);
}

void
xcb_reply_stop_thread(xcb_reply_handlers_t *h)
{
    pthread_cancel(h->thread);
    pthread_join(h->thread, 0);
}

void
xcb_reply_add_handler(xcb_reply_handlers_t *h, unsigned int request, xcb_generic_reply_handler_t handler, void *data)
{
    struct xcb_reply_node *cur = malloc(sizeof(struct xcb_reply_node));
    cur->request = request;
    cur->handler = handler;
    cur->data = data;
    cur->handled = 0;

    pthread_mutex_lock(&h->lock);
    insert_handler(h, cur);
    pthread_cond_broadcast(&h->cond);
    pthread_mutex_unlock(&h->lock);
}