Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 170 lines (145 sloc) 4.102 kb
3ce7e07 @brimworks Complete refactoring (aka rewrite). Never compiled yet.
authored
1 /**
2 * Create a table for ev.Timer that gives access to the constructor for
3 * timer objects.
4 *
5 * [-0, +1, ?]
6 */
7 static int luaopen_ev_timer(lua_State *L) {
8 lua_pop(L, create_timer_mt(L));
9
10 lua_createtable(L, 0, 1);
11
12 lua_pushcfunction(L, timer_new);
13 lua_setfield(L, -2, "new");
14
15 return 1;
16 }
17
18 /**
19 * Create the timer metatable in the registry.
20 *
21 * [-0, +1, ?]
22 */
23 static int create_timer_mt(lua_State *L) {
24
5c4ac24 update for lua 5.2
Poggles authored
25 static luaL_Reg fns[] = {
3ce7e07 @brimworks Complete refactoring (aka rewrite). Never compiled yet.
authored
26 { "again", timer_again },
27 { "stop", timer_stop },
28 { "start", timer_start },
29 { "clear_pending", timer_clear_pending },
30 { NULL, NULL }
31 };
5f9bac2 @brimworks Fix compile errors and get tests to succeed.
authored
32 luaL_newmetatable(L, TIMER_MT);
9fb9107 Integrate Brian Downing's changes in, making the following
Brian Maher authored
33 add_watcher_mt(L);
5c4ac24 update for lua 5.2
Poggles authored
34 luaL_setfuncs(L, fns, 0);
9fb9107 Integrate Brian Downing's changes in, making the following
Brian Maher authored
35
3ce7e07 @brimworks Complete refactoring (aka rewrite). Never compiled yet.
authored
36 return 1;
37 }
38
39 /**
40 * Create a new timer object. Arguments:
41 * 1 - callback function.
42 * 2 - after (number of seconds until timer should trigger).
43 * 3 - repeat (number of seconds to wait for consecutive timeouts).
44 *
45 * @see watcher_new()
46 *
47 * [+1, -0, ?]
48 */
49 static int timer_new(lua_State* L) {
50 ev_tstamp after = luaL_checknumber(L, 2);
51 ev_tstamp repeat = luaL_optnumber(L, 3, 0);
52 ev_timer* timer;
53
54 if ( after <= 0.0 )
55 luaL_argerror(L, 2, "after must be greater than 0");
56 if ( repeat < 0.0 )
57 luaL_argerror(L, 3, "repeat must be greater than or equal to 0");
58
3c1ce7c @bdowning Use lua_State from loop:loop() call site, not watcher creation
bdowning authored
59 timer = watcher_new(L, sizeof(ev_timer), TIMER_MT);
3ce7e07 @brimworks Complete refactoring (aka rewrite). Never compiled yet.
authored
60 ev_timer_init(timer, &timer_cb, after, repeat);
61 return 1;
62 }
63
64 /**
65 * @see watcher_cb()
66 *
67 * [+0, -0, m]
68 */
69 static void timer_cb(struct ev_loop* loop, ev_timer* timer, int revents) {
3c1ce7c @bdowning Use lua_State from loop:loop() call site, not watcher creation
bdowning authored
70 watcher_cb(loop, timer, revents);
3ce7e07 @brimworks Complete refactoring (aka rewrite). Never compiled yet.
authored
71 }
72
73 /**
74 * Restart a timer with the specified repeat number of seconds. If no
75 * repeat was specified, the timer is simply stopped. May optionally
76 * specify a new value for repeat, otherwise uses the value set when
77 * the timer was created.
78 *
79 * 1 - timer object.
80 * 2 - loop object.
81 * 3 - repeat (number of seconds to wait for consecutive timeouts).
82 *
83 * Usage:
84 * timer:again(loop [, repeat_seconds])
85 *
86 * [+0, -0, e]
87 */
88 static int timer_again(lua_State *L) {
89 ev_timer* timer = check_timer(L, 1);
90 struct ev_loop* loop = *check_loop_and_init(L, 2);
91 ev_tstamp repeat = luaL_optnumber(L, 3, 0);
92
93 if ( repeat < 0.0 ) luaL_argerror(L, 3, "repeat must be greater than 0");
94
95 if ( repeat ) timer->repeat = repeat;
96
97 if ( timer->repeat ) {
98 ev_timer_again(loop, timer);
99 loop_start_watcher(L, 2, 1, -1);
100 } else {
101 /* Just calling stop instead of again in case the symantics
102 * change in libev */
103 loop_stop_watcher(L, 2, 1);
104 ev_timer_stop(loop, timer);
105 }
106
107 return 0;
108 }
109
110 /**
111 * Stops the timer so it won't be called by the specified event loop.
112 *
113 * Usage:
114 * timer:stop(loop)
115 *
116 * [+0, -0, e]
117 */
118 static int timer_stop(lua_State *L) {
119 ev_timer* timer = check_timer(L, 1);
120 struct ev_loop* loop = *check_loop_and_init(L, 2);
121
122 loop_stop_watcher(L, 2, 1);
123 ev_timer_stop(loop, timer);
124
125 return 0;
126 }
127
128 /**
129 * Starts the timer so it won't be called by the specified event loop.
130 *
131 * Usage:
132 * timer:start(loop [, is_daemon])
133 *
134 * [+0, -0, e]
135 */
136 static int timer_start(lua_State *L) {
137 ev_timer* timer = check_timer(L, 1);
138 struct ev_loop* loop = *check_loop_and_init(L, 2);
139 int is_daemon = lua_toboolean(L, 3);
140
141 ev_timer_start(loop, timer);
142 loop_start_watcher(L, 2, 1, is_daemon);
143
144 return 0;
145 }
146
147 /**
148 * If the timer is pending, return the revents and clear the pending
149 * status (so the timer callback won't be called).
150 *
151 * Usage:
152 * revents = timer:clear_pending(loop)
153 *
154 * [+1, -0, e]
155 */
156 static int timer_clear_pending(lua_State *L) {
157 ev_timer* timer = check_timer(L, 1);
158 struct ev_loop* loop = *check_loop_and_init(L, 2);
159
160 int revents = ev_clear_pending(loop, timer);
5f9bac2 @brimworks Fix compile errors and get tests to succeed.
authored
161 if ( ! timer->repeat &&
3ce7e07 @brimworks Complete refactoring (aka rewrite). Never compiled yet.
authored
162 ( revents & EV_TIMEOUT ) )
163 {
164 loop_stop_watcher(L, 2, 1);
165 }
777ae87 @agladysh trimmed whitespaces
agladysh authored
166
3ce7e07 @brimworks Complete refactoring (aka rewrite). Never compiled yet.
authored
167 lua_pushnumber(L, revents);
168 return 1;
169 }
Something went wrong with that request. Please try again.