[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Adding poll() to lposix
- From: "steve donovan" <steve.j.donovan@...>
- Date: Fri, 16 Nov 2007 15:39:34 +0200
Hi guys,
Here's a little hack that may be useful; I've added a simplified
binding to poll() to lposix. It takes an input file, and a timeout
(which may be -1 for 'infinite' as usual). It will return either 0
(timeout), 1 (file ready for reading) or nil, we had a problem.
It's useful for a situation where you have a little daemon doing
something occaisionally, which you also want to control using a named
pipe. Obviously this can be done with other methods, and obviously
there are other interesting uses, but that's what I did it for. A
particularly cool combination is posix.rpoll() and posix.fork() for
old-style multi-threading.
require 'posix'
f = io.open('one')
while true do
ret = posix.rpoll(f,500)
if ret == 0 then
print 'timeout'
elseif ret == 1 then
line = f:read()
print(line)
else
print 'finis!'
return
end
end
This contains the updated lposix.c and testpoll.lua; look for the
function Ppoll().
Note that this is an out-of-the-box Lua 5.1 compliant lposix.c, so
compile it to 'posix.so' and require() will find it _without_
posix.lua
cc -shared -o posix.so -I<lua-include> lposix.c
http://mysite.mweb.co.za/residents/sdonovan/lua/posix.zip
steve d.