diff --git a/Makefile b/Makefile index 13094de..aea7561 100644 --- a/Makefile +++ b/Makefile @@ -1,36 +1,45 @@ # makefile for alarm library for Lua # change these to reflect your Lua installation -LUA= /tmp/lhf/lua-5.0 -LUAINC= $(LUA)/include -LUALIB= $(LUA)/lib -LUABIN= $(LUA)/bin - -# no need to change anything below here +LUA= /tmp/lhf/lua-5.1.4 +LUAINC= $(LUA)/src +LUALIB= $(LUA)/src +LUABIN= $(LUA)/src + +# these will probably work if Lua has been installed globally +#LUA= /usr/local +#LUAINC= $(LUA)/include +#LUALIB= $(LUA)/lib +#LUABIN= $(LUA)/bin + +# probably no need to change anything below here +CC= gcc CFLAGS= $(INCS) $(WARN) -O2 $G WARN= -pedantic -Wall INCS= -I$(LUAINC) +MAKESO= $(CC) -shared +#MAKESO= env MACOSX_DEPLOYMENT_TARGET=10.3 $(CC) -bundle -undefined dynamic_lookup MYNAME= alarm MYLIB= l$(MYNAME) -T= $(MYLIB).so +T= $(MYNAME).so OBJS= $(MYLIB).o TEST= test.lua all: test test: $T - $(LUABIN)/lua -l$(MYNAME) $(TEST) + $(LUABIN)/lua $(TEST) o: $(MYLIB).o so: $T $T: $(OBJS) - $(CC) -o $@ -shared $(OBJS) + $(MAKESO) -o $@ $(OBJS) clean: - rm -f $(OBJS) $T core core.* a.out + rm -f $(OBJS) $T core core.* doc: @echo "$(MYNAME) library:" @@ -38,15 +47,13 @@ doc: # distribution -FTP= $(HOME)/public/ftp/lua/5.0 +FTP= $(HOME)/public/ftp/lua/5.1 D= $(MYNAME) A= $(MYLIB).tar.gz -TOTAR= Makefile,README,$(MYLIB).c,$(MYNAME).lua,test.lua +TOTAR= Makefile,README,$(MYLIB).c,test.lua -tar: clean +distr: clean tar zcvf $A -C .. $D/{$(TOTAR)} - -distr: tar touch -r $A .stamp mv $A $(FTP) diff --git a/README b/README index 8ec30db..047e01b 100644 --- a/README +++ b/README @@ -1,16 +1,17 @@ -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 lhf@tecgraf.puc-rio.br . @@ -18,6 +19,6 @@ Please send comments, suggestions, and bug reports to lhf@tecgraf.puc-rio.br . ------------------------------------------------------------------------------- alarm library: - alarm(secs,[func]) + alarm([secs,[func]]) ------------------------------------------------------------------------------- diff --git a/alarm.lua b/alarm.lua deleted file mode 100644 index 9c9a711..0000000 --- a/alarm.lua +++ /dev/null @@ -1,10 +0,0 @@ --- alarm.lua --- support code for alarm library --- usage lua -lalarm ... - -local function so(x) - local SOPATH= os.getenv"LUA_SOPATH" or "./" - assert(loadlib(SOPATH.."l"..x..".so","luaopen_"..x))() -end - -so"alarm" diff --git a/lalarm.c b/lalarm.c index 836bcfc..17d8d17 100644 --- a/lalarm.c +++ b/lalarm.c @@ -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 -* 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; } diff --git a/test.lua b/test.lua index 2078ddd..73f2cfb 100644 --- a/test.lua +++ b/test.lua @@ -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)