Permalink
Browse files

Imported from 5.1/lalarm.tar.gz.

  • Loading branch information...
1 parent 8bc390d commit 80dbedaf842d3b88e103feac3fd1e717f2b9059f Luiz Henrique de Figueiredo committed with davidm May 1, 2009
Showing with 87 additions and 56 deletions.
  1. +22 −15 Makefile
  2. +9 −8 README
  3. +0 −10 alarm.lua
  4. +18 −15 lalarm.c
  5. +38 −8 test.lua
View
@@ -1,52 +1,59 @@
# 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:"
@fgrep '/**' $(MYLIB).c | cut -f2 -d/ | tr -d '*' | sort | column
# 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)
View
17 README
@@ -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 lhf@tecgraf.puc-rio.br .
-------------------------------------------------------------------------------
alarm library:
- alarm(secs,[func])
+ alarm([secs,[func]])
-------------------------------------------------------------------------------
View
@@ -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"
View
@@ -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 <lhf@tecgraf.puc-rio.br>
-* 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;
}
View
@@ -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)

0 comments on commit 80dbeda

Please sign in to comment.