-
Notifications
You must be signed in to change notification settings - Fork 55
/
timer_lua_ev.c
167 lines (143 loc) · 3.92 KB
/
timer_lua_ev.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
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
/**
* Create a table for ev.Timer that gives access to the constructor for
* timer objects.
*
* [-0, +1, ?]
*/
static int luaopen_ev_timer(lua_State *L) {
lua_pop(L, create_timer_mt(L));
lua_createtable(L, 0, 1);
lua_pushcfunction(L, timer_new);
lua_setfield(L, -2, "new");
return 1;
}
/**
* Create the timer metatable in the registry.
*
* [-0, +1, ?]
*/
static int create_timer_mt(lua_State *L) {
static luaL_Reg fns[] = {
{ "again", timer_again },
{ "stop", timer_stop },
{ "start", timer_start },
{ "clear_pending", timer_clear_pending },
{ NULL, NULL }
};
luaL_newmetatable(L, TIMER_MT);
add_watcher_mt(L);
luaL_setfuncs(L, fns, 0);
return 1;
}
/**
* Create a new timer object. Arguments:
* 1 - callback function.
* 2 - after (number of seconds until timer should trigger).
* 3 - repeat (number of seconds to wait for consecutive timeouts).
*
* @see watcher_new()
*
* [+1, -0, ?]
*/
static int timer_new(lua_State* L) {
ev_tstamp after = luaL_checknumber(L, 2);
ev_tstamp repeat = luaL_optnumber(L, 3, 0);
ev_timer* timer;
if ( repeat < 0.0 )
luaL_argerror(L, 3, "repeat must be greater than or equal to 0");
timer = watcher_new(L, sizeof(ev_timer), TIMER_MT);
ev_timer_init(timer, &timer_cb, after, repeat);
return 1;
}
/**
* @see watcher_cb()
*
* [+0, -0, m]
*/
static void timer_cb(struct ev_loop* loop, ev_timer* timer, int revents) {
watcher_cb(loop, timer, revents);
}
/**
* Restart a timer with the specified repeat number of seconds. If no
* repeat was specified, the timer is simply stopped. May optionally
* specify a new value for repeat, otherwise uses the value set when
* the timer was created.
*
* 1 - timer object.
* 2 - loop object.
* 3 - repeat (number of seconds to wait for consecutive timeouts).
*
* Usage:
* timer:again(loop [, repeat_seconds])
*
* [+0, -0, e]
*/
static int timer_again(lua_State *L) {
ev_timer* timer = check_timer(L, 1);
struct ev_loop* loop = *check_loop_and_init(L, 2);
ev_tstamp repeat = luaL_optnumber(L, 3, 0);
if ( repeat < 0.0 ) luaL_argerror(L, 3, "repeat must be greater than 0");
if ( repeat ) timer->repeat = repeat;
if ( timer->repeat ) {
ev_timer_again(loop, timer);
loop_start_watcher(L, 2, 1, -1);
} else {
/* Just calling stop instead of again in case the symantics
* change in libev */
loop_stop_watcher(L, 2, 1);
ev_timer_stop(loop, timer);
}
return 0;
}
/**
* Stops the timer so it won't be called by the specified event loop.
*
* Usage:
* timer:stop(loop)
*
* [+0, -0, e]
*/
static int timer_stop(lua_State *L) {
ev_timer* timer = check_timer(L, 1);
struct ev_loop* loop = *check_loop_and_init(L, 2);
loop_stop_watcher(L, 2, 1);
ev_timer_stop(loop, timer);
return 0;
}
/**
* Starts the timer so it won't be called by the specified event loop.
*
* Usage:
* timer:start(loop [, is_daemon])
*
* [+0, -0, e]
*/
static int timer_start(lua_State *L) {
ev_timer* timer = check_timer(L, 1);
struct ev_loop* loop = *check_loop_and_init(L, 2);
int is_daemon = lua_toboolean(L, 3);
ev_timer_start(loop, timer);
loop_start_watcher(L, 2, 1, is_daemon);
return 0;
}
/**
* If the timer is pending, return the revents and clear the pending
* status (so the timer callback won't be called).
*
* Usage:
* revents = timer:clear_pending(loop)
*
* [+1, -0, e]
*/
static int timer_clear_pending(lua_State *L) {
ev_timer* timer = check_timer(L, 1);
struct ev_loop* loop = *check_loop_and_init(L, 2);
int revents = ev_clear_pending(loop, timer);
if ( ! timer->repeat &&
( revents & EV_TIMEOUT ) )
{
loop_stop_watcher(L, 2, 1);
}
lua_pushnumber(L, revents);
return 1;
}