diff options
author | Benoit Germain <bnt.germain@gmail.com> | 2012-06-24 15:12:32 +0200 |
---|---|---|
committer | Benoit Germain <bnt.germain@gmail.com> | 2012-06-24 15:12:32 +0200 |
commit | 14acdd61de1e3679cb731713c6e5aba274fa8869 (patch) | |
tree | bce1234e0a956abbd8fb5b1f383fe7e10b2881ed /src | |
parent | fc20e54ac64569ad53118084a2b0615f84eed5b1 (diff) | |
download | lanes-14acdd61de1e3679cb731713c6e5aba274fa8869.tar.gz lanes-14acdd61de1e3679cb731713c6e5aba274fa8869.tar.bz2 lanes-14acdd61de1e3679cb731713c6e5aba274fa8869.zip |
fix issue #25
* lanes.timer() accepts a first_secs=nil to stop a timer
* timer lane catches errors and prints them
* fixed some typos in manual
Diffstat (limited to 'src')
-rw-r--r-- | src/lanes.c | 2 | ||||
-rw-r--r-- | src/lanes.lua | 67 |
2 files changed, 38 insertions, 31 deletions
diff --git a/src/lanes.c b/src/lanes.c index 931af98..b1c4018 100644 --- a/src/lanes.c +++ b/src/lanes.c | |||
@@ -51,7 +51,7 @@ | |||
51 | * ... | 51 | * ... |
52 | */ | 52 | */ |
53 | 53 | ||
54 | char const* VERSION = "3.1.3"; | 54 | char const* VERSION = "3.1.4"; |
55 | 55 | ||
56 | /* | 56 | /* |
57 | =============================================================================== | 57 | =============================================================================== |
diff --git a/src/lanes.lua b/src/lanes.lua index dffd017..5aeeaf4 100644 --- a/src/lanes.lua +++ b/src/lanes.lua | |||
@@ -92,10 +92,10 @@ local error= assert( error ) | |||
92 | 92 | ||
93 | lanes.ABOUT= | 93 | lanes.ABOUT= |
94 | { | 94 | { |
95 | author= "Asko Kauppi <akauppi@gmail.com>", | 95 | author= "Asko Kauppi <akauppi@gmail.com>, Benoit Germain <bnt.germain@gmail.com>", |
96 | description= "Running multiple Lua states in parallel", | 96 | description= "Running multiple Lua states in parallel", |
97 | license= "MIT/X11", | 97 | license= "MIT/X11", |
98 | copyright= "Copyright (c) 2007-10, Asko Kauppi", | 98 | copyright= "Copyright (c) 2007-10, Asko Kauppi; (c) 2011-12, Benoit Germain", |
99 | version= _version, | 99 | version= _version, |
100 | } | 100 | } |
101 | 101 | ||
@@ -315,7 +315,6 @@ if first_time then | |||
315 | -- set_timer( linda_h, key [,wakeup_at_secs [,period_secs]] ) | 315 | -- set_timer( linda_h, key [,wakeup_at_secs [,period_secs]] ) |
316 | -- | 316 | -- |
317 | local function set_timer( linda, key, wakeup_at, period ) | 317 | local function set_timer( linda, key, wakeup_at, period ) |
318 | |||
319 | assert( wakeup_at==nil or wakeup_at>0.0 ) | 318 | assert( wakeup_at==nil or wakeup_at>0.0 ) |
320 | assert( period==nil or period>0.0 ) | 319 | assert( period==nil or period>0.0 ) |
321 | 320 | ||
@@ -429,32 +428,40 @@ if first_time then | |||
429 | -- We let the timer lane be a "free running" thread; no handle to it | 428 | -- We let the timer lane be a "free running" thread; no handle to it |
430 | -- remains. | 429 | -- remains. |
431 | -- | 430 | -- |
432 | gen( "*", { priority=max_prio}, function() -- "*" instead of "io,package" for LuaJIT compatibility... | 431 | local timer_body = function() |
433 | set_debug_threadname( "LanesTimer") | 432 | local timer_gateway_batched = timer_gateway.batched |
434 | while true do | 433 | set_debug_threadname( "LanesTimer") |
435 | local next_wakeup= check_timers() | 434 | set_finalizer( function( err, stk) |
436 | 435 | if err and type( err) ~= "userdata" then | |
437 | -- Sleep until next timer to wake up, or a set/clear command | 436 | WR( "LanesTimer error: "..tostring(err)) |
438 | -- | 437 | --elseif type( err) == "userdata" then |
439 | local secs | 438 | -- WR( "LanesTimer after cancel" ) |
440 | if next_wakeup then | 439 | --else |
441 | secs = next_wakeup - now_secs() | 440 | -- WR("LanesTimer finalized") |
442 | if secs < 0 then secs = 0 end | 441 | end |
443 | end | 442 | end) |
444 | local linda= timer_gateway:receive( secs, TGW_KEY ) | 443 | while true do |
445 | 444 | local next_wakeup= check_timers() | |
446 | if linda then | 445 | |
447 | local key= timer_gateway:receive( 0.0, TGW_KEY ) | 446 | -- Sleep until next timer to wake up, or a set/clear command |
448 | local wakeup_at= timer_gateway:receive( 0.0, TGW_KEY ) | 447 | -- |
449 | local period= timer_gateway:receive( 0.0, TGW_KEY ) | 448 | local secs |
450 | assert( key and wakeup_at and period ) | 449 | if next_wakeup then |
451 | 450 | secs = next_wakeup - now_secs() | |
452 | set_timer( linda, key, wakeup_at, period>0 and period or nil ) | 451 | if secs < 0 then secs = 0 end |
453 | --elseif secs == nil then -- got no value while block-waiting? | 452 | end |
454 | -- WR( "timer lane: no linda, aborted?") | 453 | local linda = timer_gateway:receive( secs, TGW_KEY) |
455 | end | 454 | |
456 | end | 455 | if linda then |
457 | end )() | 456 | local key, wakeup_at, period = timer_gateway:receive( 0, timer_gateway_batched, TGW_KEY, 3) |
457 | assert( key) | ||
458 | set_timer( linda, key, wakeup_at, period and period > 0 and period or nil) | ||
459 | --elseif secs == nil then -- got no value while block-waiting? | ||
460 | -- WR( "timer lane: no linda, aborted?") | ||
461 | end | ||
462 | end | ||
463 | end | ||
464 | gen( "*", { priority=max_prio}, timer_body)() -- "*" instead of "io,package" for LuaJIT compatibility... | ||
458 | end | 465 | end |
459 | 466 | ||
460 | ----- | 467 | ----- |
@@ -479,7 +486,7 @@ timer = function( linda, key, a, period ) | |||
479 | end | 486 | end |
480 | 487 | ||
481 | local wakeup_at= type(a)=="table" and wakeup_conv(a) -- given point of time | 488 | local wakeup_at= type(a)=="table" and wakeup_conv(a) -- given point of time |
482 | or now_secs()+a | 489 | or (a and now_secs()+a or nil) |
483 | -- queue to timer | 490 | -- queue to timer |
484 | -- | 491 | -- |
485 | timer_gateway:send( TGW_KEY, linda, key, wakeup_at, period ) | 492 | timer_gateway:send( TGW_KEY, linda, key, wakeup_at, period ) |