[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Timers and Futures in Lua?
- From: Jaco van der Merwe <jjn_vdm@...>
- Date: Sat, 31 Jan 2004 07:16:22 -0800 (PST)
Hi All
We want to implement a real-time system in Lua that
has the following requirements:
1. It must be possible to create timers (one-shot or
repeating) that call Lua functions when the timers
expire. There may be several timers running at
different frequencies, e.g., ten times per second,
once per second, once per minute, etc. Here's an
example of how I envisage the code for this:
t1 = timer.new( 100, my_func1 )
// t1 fires every 100ms
// t1 calls my_func1 when it fires
t2 = timer.new( 1000, my_func2 )
// t2 fires every 1s
// t2 calls my_func2 when it fires
2. It must be possible to wait for an event with a
timeout mechanism. For example:
future = send_cmd("xyz")
// send a command to another system over network
// returns a future object
result = future:result(1000)
// future.result() returns the result
// immediately if it is available, otherwise it
// blocks until it is available. If it times out
// after 1s then it returns nil.
3. It must be possible to put delays/sleeps in the
code. For example:
do_something1()
sleep(3000) // sleep for 3 seconds
do_something2()
...
Is anybody aware of a library that satisfies the above
requirements?
I've thought about two possible implementation
approaches.
Approach A:
-----------
Build an extension to the coroutine library. It'll
work as follows. There is a main event loop that
handles all the timers. Each timer has it's own
coroutine. The user function called when a timer fires
will be called from within the timer's coroutine. The
sleep() function is like a yield, but goes back to the
main event loop that resumes the thread once the sleep
period has expired. The benefit of this approach is
that other timers can fire while a thread is sleeping.
Similarly, the future.result() function is also like a
yield that is resumed after either an event (like a
packet received over network) or a timeout.
With this approach only one execution thread is active
at any point in time. It also means that a timer
function that takes very long to execute may hold up
other higher frequency timers that have fired (unless
the time is spent in sleep() or future.result())
Approach B:
-----------
Use a fully preemptive threading solution. Each timer
runs in its own thread. When it fires it calls the lua
user function in its own thread. The sleep() and
future.result() implementations just hook into the
preemptive scheduling mechanism and should be quite
easy to do.
With this approach timer functions may execute in
parallel and a event loop is not required. Thus, a
main function can run in parallel with the timers.
This is not possible with the coroutine solution.
Is there an implementation of the preemptive threading
for Lua? Are there any disadvantages in using such an
approach that I'm not seeing?
Regards
Jaco
__________________________________
Do you Yahoo!?
Yahoo! SiteBuilder - Free web site building tool. Try it!
http://webhosting.yahoo.com/ps/sb/