summaryrefslogtreecommitdiffstats
path: root/src/mainloop.c
blob: d043ce90aac882eca18cca6a311f16d6f2885b08 (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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
#include <sys/poll.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>

#include "mainloop.h"

struct mainloop_source {
    struct mainloop_source *next;
    struct mainloop *mainloop;
    enum mainloop_source_type type;

    int enabled;
    int dead;
    void *userdata;

    struct {
        int fd;
        enum mainloop_io_event events;
        void (*callback)(struct mainloop_source*s, int fd, enum mainloop_io_event event, void *userdata);
        struct pollfd pollfd;
    } io;
    
    struct  {
        void (*callback)(struct mainloop_source*s, void *userdata);
    } prepare;
    
    struct  {
        void (*callback)(struct mainloop_source*s, void *userdata);
    } idle;
};

struct mainloop_source_list {
    struct mainloop_source *sources;
    int n_sources;
    int dead_sources;
};

struct mainloop {
    struct mainloop_source_list io_sources, prepare_sources, idle_sources;
    
    struct pollfd *pollfds;
    int max_pollfds, n_pollfds;
    int rebuild_pollfds;

    int quit;
    int running;
};

struct mainloop *mainloop_new(void) {
    struct mainloop *m;

    m = malloc(sizeof(struct mainloop));
    assert(m);
    memset(m, 0, sizeof(struct mainloop));
    
    return m;
}

static void free_sources(struct mainloop_source_list *l, int all) {
    struct mainloop_source *s, *p;
    assert(l);

    if (!l->dead_sources)
        return;

    p = NULL;
    s = l->sources;
    while (s) {
        if (all || s->dead) {
            struct mainloop_source *t = s;
            s = s->next;

            if (p)
                p->next = s;
            else
                l->sources = s;
            
            free(t);
        } else {
            p = s;
            s = s->next;
        }
    }

    l->dead_sources = 0;

    if (all) {
        assert(l->sources);
        l->n_sources = 0;
    }
}

void mainloop_free(struct mainloop* m) {
    assert(m);
    free_sources(&m->io_sources, 1);
    free_sources(&m->prepare_sources, 1);
    free_sources(&m->idle_sources, 1);
    free(m->pollfds);
}

static void rebuild_pollfds(struct mainloop *m) {
    struct mainloop_source*s;
    struct pollfd *p;
    
    if (m->max_pollfds < m->io_sources.n_sources) {
        m->max_pollfds = m->io_sources.n_sources*2;
        m->pollfds = realloc(m->pollfds, sizeof(struct pollfd)*m->max_pollfds);
    }

    m->n_pollfds = 0;
    p = m->pollfds;
    for (s = m->io_sources.sources; s; s = s->next) {
        assert(s->type == MAINLOOP_SOURCE_TYPE_IO);
        if (!s->dead && s->enabled && s->io.events != MAINLOOP_IO_EVENT_NULL) {
            *(p++) = s->io.pollfd;
            m->n_pollfds++;
        }
    }
}

static void dispatch_pollfds(struct mainloop *m) {
    int i;
    struct pollfd *p;
    struct mainloop_source *s;
    /* This loop assumes that m->sources and m->pollfds have the same
     * order and that m->pollfds is a subset of m->sources! */

    s = m->io_sources.sources;
    for (p = m->pollfds, i = 0; i < m->n_pollfds; p++, i++) {
        for (;;) {
            assert(s && s->type == MAINLOOP_SOURCE_TYPE_IO);
            
            if (p->fd == s->io.fd) {
                if (!s->dead && s->enabled) {
                    enum mainloop_io_event e = (p->revents & POLLIN ? MAINLOOP_IO_EVENT_IN : 0) | (p->revents & POLLOUT ? MAINLOOP_IO_EVENT_OUT : 0);
                    if (e) {
                        assert(s->io.callback);
                        s->io.callback(s, s->io.fd, e, s->userdata);
                    }
                }

                break;
            }
            s = s->next;
        }
    }
}

int mainloop_iterate(struct mainloop *m, int block) {
    struct mainloop_source *s;
    int c;
    assert(m && !m->running);
    
    if(m->quit)
        return m->quit;

    free_sources(&m->io_sources, 0);
    free_sources(&m->prepare_sources, 0);
    free_sources(&m->idle_sources, 0);

    for (s = m->prepare_sources.sources; s; s = s->next) {
        assert(!s->dead && s->type == MAINLOOP_SOURCE_TYPE_PREPARE);
        if (s->enabled) {
            assert(s->prepare.callback);
            s->prepare.callback(s, s->userdata);
        }   
    }

    if (m->rebuild_pollfds)
        rebuild_pollfds(m);

    m->running = 1;

    if ((c = poll(m->pollfds, m->n_pollfds, (block && !m->idle_sources.n_sources) ? -1 : 0)) > 0)
        dispatch_pollfds(m);
    else if (c == 0) {
        for (s = m->idle_sources.sources; s; s = s->next) {
            assert(!s->dead && s->type == MAINLOOP_SOURCE_TYPE_IDLE);
            if (s->enabled) {
                assert(s->idle.callback);
                s->idle.callback(s, s->userdata);
            }
        }
    }
    
    m->running = 0;
    return c < 0 ? -1 : 0;
}

int mainloop_run(struct mainloop *m) {
    int r;
    while (!(r = mainloop_iterate(m, 1)));
    return r;
}

void mainloop_quit(struct mainloop *m, int r) {
    assert(m);
    m->quit = r;
}

static struct mainloop_source_list* get_source_list(struct mainloop *m, enum mainloop_source_type type) {
    struct mainloop_source_list *l;
    
    switch(type) {
        case MAINLOOP_SOURCE_TYPE_IO:
            l = &m->io_sources;
            break;
        case MAINLOOP_SOURCE_TYPE_PREPARE:
            l = &m->prepare_sources;
            break;
        case MAINLOOP_SOURCE_TYPE_IDLE:
            l = &m->idle_sources;
            break;
        default:
            l = NULL;
            break;
    }
    
    return l;
}

static struct mainloop_source *source_new(struct mainloop*m, enum mainloop_source_type type) {
    struct mainloop_source_list *l;
    struct mainloop_source* s;
    assert(m);

    s = malloc(sizeof(struct mainloop_source));
    assert(s);
    memset(s, 0, sizeof(struct mainloop_source));

    s->type = type;
    s->mainloop = m;

    l = get_source_list(m, type);
    assert(l);
            
    s->next = l->sources;
    l->sources = s;
    l->n_sources++;
    return s;
}

struct mainloop_source* mainloop_source_new_io(struct mainloop*m, int fd, enum mainloop_io_event event, void (*callback)(struct mainloop_source*s, int fd, enum mainloop_io_event event, void *userdata), void *userdata) {
    struct mainloop_source* s;
    assert(m && fd>=0 && callback);

    s = source_new(m, MAINLOOP_SOURCE_TYPE_IO);

    s->io.fd = fd;
    s->io.events = event;
    s->io.callback = callback;
    s->userdata = userdata;
    s->io.pollfd.fd = fd;
    s->io.pollfd.events = (event & MAINLOOP_IO_EVENT_IN ? POLLIN : 0) | (event & MAINLOOP_IO_EVENT_OUT ? POLLOUT : 0);
    s->io.pollfd.revents = 0;

    s->enabled = 1;

    m->rebuild_pollfds = 1;
    return s;
}

struct mainloop_source* mainloop_source_new_prepare(struct mainloop*m, void (*callback)(struct mainloop_source *s, void*userdata), void*userdata) {
    struct mainloop_source* s;
    assert(m && callback);

    s = source_new(m, MAINLOOP_SOURCE_TYPE_PREPARE);

    s->prepare.callback = callback;
    s->userdata = userdata;
    s->enabled = 1;
    return s;
}

struct mainloop_source* mainloop_source_new_idle(struct mainloop*m, void (*callback)(struct mainloop_source *s, void*userdata), void*userdata) {
    struct mainloop_source* s;
    assert(m && callback);

    s = source_new(m, MAINLOOP_SOURCE_TYPE_IDLE);

    s->prepare.callback = callback;
    s->userdata = userdata;
    s->enabled = 1;
    return s;
}

void mainloop_source_free(struct mainloop_source*s) {
    struct mainloop_source_list *l;
    assert(s && !s->dead);
    s->dead = 1;

    assert(s->mainloop);
    l = get_source_list(s->mainloop, s->type);
    assert(l);

    l->n_sources--;
    l->dead_sources = 1;

    if (s->type == MAINLOOP_SOURCE_TYPE_IO)
        s->mainloop->rebuild_pollfds = 1;
}

void mainloop_source_enable(struct mainloop_source*s, int b) {
    assert(s && !s->dead);

    if (s->type == MAINLOOP_SOURCE_TYPE_IO && ((s->enabled && !b) || (!s->enabled && b))) {
        assert(s->mainloop);
        s->mainloop->rebuild_pollfds = 1;
    }

    s->enabled = b;
}

void mainloop_source_io_set_events(struct mainloop_source*s, enum mainloop_io_event events) {
    assert(s && !s->dead && s->type == MAINLOOP_SOURCE_TYPE_IO);

    if ((s->io.events && !events) || (!s->io.events && events)) {
        assert(s->mainloop);
        s->mainloop->rebuild_pollfds = 1;
    }

    s->io.events = events;
    s->io.pollfd.events = ((events & MAINLOOP_IO_EVENT_IN) ? POLLIN : 0) | ((events & MAINLOOP_IO_EVENT_OUT) ? POLLOUT : 0);
}

struct mainloop *mainloop_source_get_mainloop(struct mainloop_source *s) {
    assert(s);

    return s->mainloop;
}