-
Notifications
You must be signed in to change notification settings - Fork 171
/
timers.c
86 lines (75 loc) · 2.83 KB
/
timers.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include "moar.h"
/* Info we convey about a timer. */
typedef struct {
int timeout;
int repeat;
uv_timer_t handle;
MVMThreadContext *tc;
int work_idx;
} TimerInfo;
/* Timer callback; dispatches schedulee to the queue. */
static void timer_cb(uv_timer_t *handle) {
TimerInfo *ti = (TimerInfo *)handle->data;
MVMThreadContext *tc = ti->tc;
MVMAsyncTask *t = (MVMAsyncTask *)MVM_repr_at_pos_o(tc,
tc->instance->event_loop_active, ti->work_idx);
MVM_repr_push_o(tc, t->body.queue, t->body.schedulee);
}
/* Sets the timer up on the event loop. */
static void setup(MVMThreadContext *tc, uv_loop_t *loop, MVMObject *async_task, void *data) {
TimerInfo *ti = (TimerInfo *)data;
uv_timer_init(loop, &ti->handle);
ti->work_idx = MVM_repr_elems(tc, tc->instance->event_loop_active);
ti->tc = tc;
ti->handle.data = ti;
MVM_repr_push_o(tc, tc->instance->event_loop_active, async_task);
uv_timer_start(&ti->handle, timer_cb, ti->timeout, ti->repeat);
}
/* Stops the timer. */
static void cancel(MVMThreadContext *tc, uv_loop_t *loop, MVMObject *async_task, void *data) {
TimerInfo *ti = (TimerInfo *)data;
uv_timer_stop(&ti->handle);
}
/* Frees data associated with a timer async task. */
static void gc_free(MVMThreadContext *tc, MVMObject *t, void *data) {
if (data)
MVM_free(data);
}
/* Operations table for async timer task. */
static const MVMAsyncTaskOps op_table = {
setup,
cancel,
NULL,
gc_free
};
/* Creates a new timer. */
MVMObject * MVM_io_timer_create(MVMThreadContext *tc, MVMObject *queue,
MVMObject *schedulee, MVMint64 timeout,
MVMint64 repeat, MVMObject *async_type) {
MVMAsyncTask *task;
TimerInfo *timer_info;
/* Validate REPRs. */
if (REPR(queue)->ID != MVM_REPR_ID_ConcBlockingQueue)
MVM_exception_throw_adhoc(tc,
"timer target queue must have ConcBlockingQueue REPR");
if (REPR(async_type)->ID != MVM_REPR_ID_MVMAsyncTask)
MVM_exception_throw_adhoc(tc,
"timer result type must have REPR AsyncTask");
/* Create async task handle. */
MVMROOT(tc, queue, {
MVMROOT(tc, schedulee, {
task = (MVMAsyncTask *)MVM_repr_alloc_init(tc, async_type);
});
});
MVM_ASSIGN_REF(tc, &(task->common.header), task->body.queue, queue);
MVM_ASSIGN_REF(tc, &(task->common.header), task->body.schedulee, schedulee);
task->body.ops = &op_table;
timer_info = MVM_malloc(sizeof(TimerInfo));
timer_info->timeout = timeout;
timer_info->repeat = repeat;
task->body.data = timer_info;
/* Hand the task off to the event loop, which will set up the timer on the
* event loop. */
MVM_io_eventloop_queue_work(tc, (MVMObject *)task);
return (MVMObject *)task;
}