-
Notifications
You must be signed in to change notification settings - Fork 1
/
loop_defer.c
48 lines (39 loc) · 896 Bytes
/
loop_defer.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
#include <stdlib.h>
#include "loop_internal.h"
#include "loop.h"
struct loop_defer_s {
wuy_list_node_t list_node;
loop_defer_f *func;
void *data;
float rank;
};
void loop_defer_init(loop_t *loop)
{
wuy_list_init(&loop->defer_head);
}
bool loop_defer_add4(loop_t *loop, loop_defer_f *func, void *data, float rank)
{
loop_defer_t *defer = malloc(sizeof(loop_defer_t));
if (defer == NULL) {
return false;
}
defer->func = func;
defer->data = data;
defer->rank = rank;
loop_defer_t *i;
wuy_list_iter_reverse_type(&loop->defer_head, i, list_node) {
if (i->rank <= rank) {
wuy_list_add_after(&i->list_node, &defer->list_node);
return true;
}
}
wuy_list_insert(&loop->defer_head, &defer->list_node);
return true;
}
void loop_defer_run(loop_t *loop)
{
loop_defer_t *defer;
wuy_list_iter_type(&loop->defer_head, defer, list_node) {
defer->func(defer->data);
}
}