Add lextra.c and extra library, with sleep() now part of it instead of in os
This commit is contained in:
parent
2d78ec849e
commit
4fb243af7b
5 changed files with 91 additions and 64 deletions
37
los.c
37
los.c
|
@ -40,46 +40,9 @@ os_hostname(lua_State *L)
|
|||
|
||||
return 1;
|
||||
}
|
||||
/***
|
||||
* Waits the specified amount of seconds.
|
||||
*
|
||||
* @function sleep
|
||||
* @usage
|
||||
local minutes = 5
|
||||
os.sleep(minutes * 60) -- 5 minutes
|
||||
* @tparam number seconds The amount of seconds to wait.
|
||||
*/
|
||||
static int
|
||||
os_sleep(lua_State *L)
|
||||
{
|
||||
/* Implementation from luasocket */
|
||||
double n;
|
||||
struct timespec t, r;
|
||||
|
||||
n = luaL_checknumber(L, 1);
|
||||
|
||||
if (n < 0.0)
|
||||
n = 0.0;
|
||||
if (n > INT_MAX)
|
||||
n = INT_MAX;
|
||||
|
||||
t.tv_sec = (int)n;
|
||||
n -= t.tv_sec;
|
||||
t.tv_nsec = (int)(n * 1000000000);
|
||||
|
||||
if (t.tv_nsec >= 1000000000)
|
||||
t.tv_nsec = 999999999;
|
||||
|
||||
while (nanosleep(&t, &r) != 0) {
|
||||
t.tv_sec = r.tv_sec;
|
||||
t.tv_nsec = r.tv_nsec;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static const luaL_Reg oslib[] = {
|
||||
{"hostname", os_hostname},
|
||||
{"sleep", os_sleep},
|
||||
{NULL, NULL}
|
||||
};
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue