-
Notifications
You must be signed in to change notification settings - Fork 1
/
loop.c
65 lines (56 loc) · 1.25 KB
/
loop.c
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
#include <assert.h>
#include <stdio.h>
#include <strings.h>
#include <stdlib.h>
#include "wuy_event.h"
#include "wuy_list.h"
#include "loop_internal.h"
#include "loop.h"
static void loop_event_handler(void *data, bool readable, bool writable)
{
int type = *(int *)data;
switch (type) {
case LOOP_TYPE_TCP_LISTEN:
loop_tcp_listen_acceptable(data);
break;
case LOOP_TYPE_STREAM:
loop_stream_event_handler(data, readable, writable);
break;
default:
abort();
}
}
loop_t *loop_new_noev(void)
{
loop_t *loop = calloc(1, sizeof(loop_t));
assert(loop != NULL);
loop_defer_init(loop);
loop_timer_init(loop);
loop_stream_init(loop);
return loop;
}
void loop_new_event(loop_t *loop)
{
loop->event_ctx = wuy_event_ctx_new(loop_event_handler);
}
loop_t *loop_new(void)
{
loop_t *loop = loop_new_noev();
loop_new_event(loop);
return loop;
}
void loop_run(loop_t *loop)
{
while (!loop->quit) {
/* call loop_event_handler() to handle events */
wuy_event_run(loop->event_ctx, loop_timer_next(loop->timer_ctx));
/* call loop_timer_expire() before loop_defer_run(), because
* timers may make something that need the defers to cleanup. */
loop_timer_expire(loop->timer_ctx);
loop_defer_run(loop);
}
}
void loop_kill(loop_t *loop)
{
loop->quit = 1;
}