-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathtime.c
57 lines (44 loc) · 1.26 KB
/
time.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
/* SPDX-License-Identifier: MIT */
/*
* Author: Jianhui Zhao <[email protected]>
*/
#include "eco.h"
/* returns the Unix time, the number of seconds elapsed since January 1, 1970 UTC */
static int lua_ev_now(lua_State *L)
{
struct eco_context *ctx = eco_get_context(L);
ev_now_update(ctx->loop);
lua_pushnumber(L, ev_now(ctx->loop));
return 1;
}
/*
* pauses the current coroutine for at least the delay seconds.
* A negative or zero delay causes sleep to return immediately.
*/
static int lua_sleep(lua_State *L)
{
struct eco_context *ctx = eco_get_context(L);
double delay = luaL_checknumber(L, 1);
struct ev_timer *tmr;
eco_push_context_env(L);
lua_rawgetp(L, LUA_REGISTRYINDEX, eco_get_obj_registry());
lua_pushlightuserdata(L, L);
lua_rawget(L, -2); /* ctx_env, objs, co */
lua_remove(L, -2); /* ctx_env, co */
lua_rawget(L, -2); /* ctx_env, tmr_ptr */
tmr = (struct ev_timer *)lua_topointer(L, -1);
lua_pop(L, 2);
ev_timer_set(tmr, delay, 0.0);
ev_timer_start(ctx->loop, tmr);
return lua_yield(L, 0);
}
static const luaL_Reg funcs[] = {
{"now", lua_ev_now},
{"sleep", lua_sleep},
{NULL, NULL}
};
int luaopen_eco_core_time(lua_State *L)
{
luaL_newlib(L, funcs);
return 1;
}