summaryrefslogtreecommitdiffstats
path: root/lem/utils.c
blob: 18e9c67a2af8b0959e99e74dab902c9382c28468 (plain) (blame)
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
/*
 * This file is part of LEM, a Lua Event Machine.
 * Copyright 2011-2012 Emil Renner Berthing
 *
 * LEM is free software: you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation, either version 3 of
 * the License, or (at your option) any later version.
 *
 * LEM is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with LEM.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <sys/time.h>
#include <lem.h>

static int
sleeper_wakeup(lua_State *T)
{
	struct ev_timer *w;
	lua_State *S;
	int nargs;

	luaL_checktype(T, 1, LUA_TUSERDATA);
	w = lua_touserdata(T, 1);
	S = w->data;
	if (S == NULL) {
		lua_pushnil(T);
		lua_pushliteral(T, "not sleeping");
		return 2;
	}

	ev_timer_stop(LEM_ w);

	nargs = lua_gettop(T) - 1;
	lua_settop(S, 0);
	lua_xmove(T, S, nargs);
	lem_queue(S, nargs);
	w->data = NULL;

	/* return true */
	lua_pushboolean(T, 1);
	return 1;
}

static void
sleep_handler(EV_P_ struct ev_timer *w, int revents)
{
	lua_State *T = w->data;

	(void)revents;

	/* return nil, "timeout" */
	lem_queue(T, 2);
	w->data = NULL;
}

static int
sleeper_sleep(lua_State *T)
{
	struct ev_timer *w;

	luaL_checktype(T, 1, LUA_TUSERDATA);
	w = lua_touserdata(T, 1);
	if (w->data != NULL) {
		lua_pushnil(T);
		lua_pushliteral(T, "busy");
		return 2;
	}

	if (lua_gettop(T) > 1 && !lua_isnil(T, 2)) {
		ev_tstamp delay = (ev_tstamp)luaL_checknumber(T, 2);

		if (delay <= 0) {
			/* return nil, "timeout"
			 * ..but yield the thread */
			lua_pushnil(T);
			lua_pushvalue(T, lua_upvalueindex(1));
			lem_queue(T, 2);

			return lua_yield(T, 2);
		}

		ev_timer_set(w, delay, 0);
		ev_timer_start(LEM_ w);
	}

	w->data = T;

	/* yield sleeper, nil, "timeout" */
	lua_settop(T, 1);
	lua_pushnil(T);
	lua_pushvalue(T, lua_upvalueindex(1));
	return lua_yield(T, 3);
}

static int
sleeper_new(lua_State *T)
{
	struct ev_timer *w;

	/* create new sleeper object and set metatable */
	w = lua_newuserdata(T, sizeof(struct ev_timer));
	lua_pushvalue(T, lua_upvalueindex(1));
	lua_setmetatable(T, -2);

	ev_init(w, sleep_handler);
	w->data = NULL;

	return 1;
}

static int
utils_spawn(lua_State *T)
{
	lua_State *S;
	int nargs;

	luaL_checktype(T, 1, LUA_TFUNCTION);

	S = lem_newthread();
	nargs = lua_gettop(T);

	lua_xmove(T, S, nargs);

	lem_queue(S, nargs - 1);

	lua_pushboolean(T, 1);
	return 1;
}

static int
utils_yield(lua_State *T)
{
	lem_queue(T, 0);
	return lua_yield(T, 0);
}

static int
utils_exit(lua_State *T)
{
	int status = (int)luaL_checknumber(T, 1);
	lem_exit(status);
	return lua_yield(T, 0);
}

static int
utils_thisthread(lua_State *T)
{
	lua_pushthread(T);
	return 1;
}

static int
utils_suspend(lua_State *T)
{
	return lua_yield(T, 0);
}

static int
utils_resume(lua_State *T)
{
	int args;
	lua_State *S;

	luaL_checktype(T, 1, LUA_TTHREAD);
	S = lua_tothread(T, 1);

	args = lua_gettop(T) - 1;
	lua_xmove(T, S, args);
	lem_queue(S, args);

	return 0;
}

static int
utils_now(lua_State *T)
{
	lua_pushnumber(T, (lua_Number)ev_now());
	return 1;
}

static int
utils_updatenow(lua_State *T)
{
	ev_now_update(LEM);
	lua_pushnumber(T, (lua_Number)ev_now());
	return 1;
}

static int
utils_poolconfig(lua_State *T)
{
	lua_Number n;
	int delay;
	int min;
	int max;

	n = luaL_checknumber(T, 1);
	delay = (int)n;
	luaL_argcheck(T, (lua_Number)delay == n && delay >= 0,
			1, "not an integer in proper range");
	n = luaL_checknumber(T, 2);
	min = (int)n;
	luaL_argcheck(T, (lua_Number)min == n && min >= 0,
			2, "not an integer in proper range");
	n = luaL_checknumber(T, 3);
	max = (int)n;
	luaL_argcheck(T, (lua_Number)max == n && max > 0 && max >= min,
			3, "not an integer in proper range");

	lem_async_config(delay, min, max);
	return 0;
}

int
luaopen_lem_utils(lua_State *L)
{
	/* create module table */
	lua_newtable(L);

	/* create new sleeper metatable */
	lua_newtable(L);
	/* mt.__index = mt */
	lua_pushvalue(L, -1);
	lua_setfield(L, -2, "__index");
	/* mt.wakeup = <sleeper_wakeup> */
	lua_pushcfunction(L, sleeper_wakeup);
	lua_setfield(L, -2, "wakeup");
	/* mt.sleep = <sleeper_sleep> */
	lua_pushliteral(L, "timeout"); /* upvalue 1: "timeout" */
	lua_pushcclosure(L, sleeper_sleep, 1);
	lua_setfield(L, -2, "sleep");
	/* set sleeper function */
	lua_pushcclosure(L, sleeper_new, 1);
	lua_setfield(L, -2, "newsleeper");

	/* set spawn function */
	lua_pushcfunction(L, utils_spawn);
	lua_setfield(L, -2, "spawn");

	/* set yield function */
	lua_pushcfunction(L, utils_yield);
	lua_setfield(L, -2, "yield");

	/* set exit function */
	lua_pushcfunction(L, utils_exit);
	lua_setfield(L, -2, "exit");

	/* set thisthread function */
	lua_pushcfunction(L, utils_thisthread);
	lua_setfield(L, -2, "thisthread");
	/* set suspend function */
	lua_pushcfunction(L, utils_suspend);
	lua_setfield(L, -2, "suspend");
	/* set resume function */
	lua_pushcfunction(L, utils_resume);
	lua_setfield(L, -2, "resume");

	/* set now function */
	lua_pushcfunction(L, utils_now);
	lua_setfield(L, -2, "now");
	/* set updatenow function */
	lua_pushcfunction(L, utils_updatenow);
	lua_setfield(L, -2, "updatenow");

	/* set poolconfig function */
	lua_pushcfunction(L, utils_poolconfig);
	lua_setfield(L, -2, "poolconfig");

	return 1;
}