This repository has been archived by the owner on Nov 20, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
87 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,24 @@ | ||
This is an alarm library for Lua 5.0. It depends on signal and SIGALRM, | ||
which should be available in any POSIX system. | ||
This is an alarm library for Lua 5.1. It depends on alarm, signal, and SIGALRM, | ||
which should be available in any POSIX system. See for instance` | ||
http://www.opengroup.org/onlinepubs/009695399/functions/alarm.html | ||
|
||
To try the library, just edit Makefile to reflect your installation of Lua. | ||
Then run make. This will build the library and run a simple test. | ||
For detailed installation instructions, see | ||
|
||
http://www.tecgraf.puc-rio.br/~lhf/ftp/lua/install.html | ||
|
||
The library exports a single function: alarm(s,[f]). It tells Lua to call f | ||
after s seconds have elapsed. This is done only once. If you want f to be | ||
called every s seconds, call alarm(s) inside f. See test.lua, which shows | ||
the library in action. | ||
The library exports a single function, alarm([s,[f]]), which tells Lua to | ||
call f after s seconds have elapsed. This is done only once. If you want f | ||
to be called every s seconds, call alarm(s) inside f. Call alarm() without | ||
arguments or with s=0 to cancel any pending alarm. See test.lua, which | ||
shows the library in action. | ||
|
||
This code is hereby placed in the public domain. | ||
Please send comments, suggestions, and bug reports to [email protected] . | ||
|
||
------------------------------------------------------------------------------- | ||
|
||
alarm library: | ||
alarm(secs,[func]) | ||
alarm([secs,[func]]) | ||
|
||
------------------------------------------------------------------------------- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,8 @@ | ||
/* | ||
* lalarm.c | ||
* an alarm library for Lua 5.0 based on signal | ||
* an alarm library for Lua 5.1 based on signal | ||
* Luiz Henrique de Figueiredo <[email protected]> | ||
* 11 Oct 2006 12:39:59 | ||
* 01 May 2009 10:40:56 | ||
* This code is hereby placed in the public domain. | ||
*/ | ||
|
||
|
@@ -37,26 +37,29 @@ static void l_signal(int i) | |
lua_sethook(LL,l_handler,LUA_MASKCALL | LUA_MASKRET | LUA_MASKCOUNT,1); | ||
} | ||
|
||
static int l_alarm(lua_State *L) /** alarm(secs,[func]) */ | ||
static int l_alarm(lua_State *L) /** alarm([secs,[func]]) */ | ||
{ | ||
LL=L; | ||
if (lua_gettop(L)==1) | ||
switch (lua_gettop(L)) | ||
{ | ||
lua_pushliteral(L,NAME); | ||
lua_gettable(L,LUA_REGISTRYINDEX); | ||
if (lua_isnil(L,-1)) luaL_error(L,"no alarm handler set"); | ||
} | ||
else | ||
{ | ||
luaL_checktype(L,2,LUA_TFUNCTION); | ||
lua_pushliteral(L,NAME); | ||
lua_pushvalue(L,2); | ||
lua_settable(L,LUA_REGISTRYINDEX); | ||
case 0: | ||
break; | ||
case 1: | ||
lua_pushliteral(L,NAME); | ||
lua_gettable(L,LUA_REGISTRYINDEX); | ||
if (lua_isnil(L,-1)) luaL_error(L,"no alarm handler set"); | ||
break; | ||
default: | ||
luaL_checktype(L,2,LUA_TFUNCTION); | ||
lua_pushliteral(L,NAME); | ||
lua_pushvalue(L,2); | ||
lua_settable(L,LUA_REGISTRYINDEX); | ||
break; | ||
} | ||
if (signal(SIGALRM,l_signal)==SIG_ERR) | ||
lua_pushnil(L); | ||
else | ||
lua_pushnumber(L,alarm(lua_tonumber(L,1))); | ||
lua_pushinteger(L,alarm(luaL_optinteger(L,1,0))); | ||
return 1; | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,48 @@ | ||
-- test alarm | ||
|
||
function myalarm() | ||
print("in alarm!",os.date"%T",a,math.floor(100*a/N).."%") | ||
alarm(1) | ||
require"alarm" | ||
|
||
version="alarm library for ".. _VERSION.." / May 2009" | ||
|
||
------------------------------------------------------------------------------ | ||
print(version) | ||
|
||
------------------------------------------------------------------------------ | ||
print"" | ||
print"timeout..." | ||
|
||
function timeout(t,f,...) | ||
alarm(t,function () error"timeout!" end) | ||
print(pcall(f,...)) | ||
alarm() | ||
end | ||
|
||
timeout(1,function (N) for i=1,N do end return "ok",N end,1e6) | ||
timeout(1,function (N) for i=1,N do end return "ok",N end,1e8) | ||
|
||
------------------------------------------------------------------------------ | ||
print"" | ||
print"timer..." | ||
|
||
function timer(s,f) | ||
local a=function () f() alarm(s) end | ||
alarm(s,a) | ||
end | ||
|
||
N=4000000 | ||
function myalarm() | ||
print("in alarm!",os.date"%T",a,math.floor(100*a/N).."%") | ||
end | ||
|
||
print"hello" | ||
alarm(1,myalarm) | ||
N=2e7 | ||
timer(1,myalarm) | ||
a=0 | ||
print("start ",os.date"%T",a,math.floor(100*a/N).."%") | ||
for i=1,N do | ||
a=a+1 | ||
math.sin(a) -- waste some time... | ||
end | ||
print(a) | ||
print"bye" | ||
print("finish ",os.date"%T",a,math.floor(100*a/N).."%") | ||
|
||
------------------------------------------------------------------------------ | ||
print"" | ||
print(version) |