Skip to content
This repository has been archived by the owner on Nov 20, 2020. It is now read-only.

Commit

Permalink
Imported from 5.1/lalarm.tar.gz.
Browse files Browse the repository at this point in the history
  • Loading branch information
Luiz Henrique de Figueiredo authored and davidm committed Mar 24, 2011
1 parent 8bc390d commit 80dbeda
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 56 deletions.
37 changes: 22 additions & 15 deletions Makefile
Original file line number Diff line number Diff line change
@@ -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)

Expand Down
17 changes: 9 additions & 8 deletions README
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]])

-------------------------------------------------------------------------------
10 changes: 0 additions & 10 deletions alarm.lua

This file was deleted.

33 changes: 18 additions & 15 deletions lalarm.c
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.
*/

Expand Down Expand Up @@ -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;
}

Expand Down
46 changes: 38 additions & 8 deletions test.lua
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)

0 comments on commit 80dbeda

Please sign in to comment.