aboutsummaryrefslogtreecommitdiff
path: root/src/lanes.lua
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--src/lanes.lua41
1 files changed, 26 insertions, 15 deletions
diff --git a/src/lanes.lua b/src/lanes.lua
index d890adf..c860ba0 100644
--- a/src/lanes.lua
+++ b/src/lanes.lua
@@ -400,7 +400,8 @@ local configure_timers = function()
400 400
401 -- Timer lane; initialize only on the first 'require "lanes"' instance (which naturally has 'table' always declared) 401 -- Timer lane; initialize only on the first 'require "lanes"' instance (which naturally has 'table' always declared)
402 local first_time_key = "first time" 402 local first_time_key = "first time"
403 local first_time = timerLinda:get(first_time_key) == nil 403 local _, _first_time_val = timerLinda:get(first_time_key)
404 local first_time = (_first_time_val == nil)
404 timerLinda:set(first_time_key, true) 405 timerLinda:set(first_time_key, true)
405 if first_time then 406 if first_time then
406 407
@@ -490,8 +491,8 @@ local configure_timers = function()
490 -- 491 --
491 local t2 = t1[key_] 492 local t2 = t1[key_]
492 if not t2 then 493 if not t2 then
493 t2= {} 494 t2 = {}
494 t1[key_]= t2 495 t1[key_] = t2
495 end 496 end
496 497
497 t2[1] = wakeup_at_ 498 t2[1] = wakeup_at_
@@ -507,10 +508,10 @@ local configure_timers = function()
507 local now = now_secs() 508 local now = now_secs()
508 local next_wakeup 509 local next_wakeup
509 510
510 for linda_deep,t1 in pairs(collection) do 511 for linda_deep, t1 in pairs(collection) do
511 for key,t2 in pairs(t1) do 512 for key, t2 in pairs(t1) do
512 -- 513 --
513 if key==linda_deep then 514 if key == linda_deep then
514 -- no 'continue' in Lua :/ 515 -- no 'continue' in Lua :/
515 else 516 else
516 -- 't2': { wakeup_at_secs [,period_secs] } 517 -- 't2': { wakeup_at_secs [,period_secs] }
@@ -519,10 +520,10 @@ local configure_timers = function()
519 local period= t2[2] -- may be 'nil' 520 local period= t2[2] -- may be 'nil'
520 521
521 if wakeup_at <= now then 522 if wakeup_at <= now then
522 local linda= t1[linda_deep] 523 local linda = t1[linda_deep]
523 assert(linda) 524 assert(linda)
524 525
525 linda:set(key, now ) 526 linda:set(key, now)
526 527
527 -- 'pairs()' allows the values to be modified (and even 528 -- 'pairs()' allows the values to be modified (and even
528 -- removed) as far as keys are not touched 529 -- removed) as far as keys are not touched
@@ -530,13 +531,13 @@ local configure_timers = function()
530 if not period then 531 if not period then
531 -- one-time timer; gone 532 -- one-time timer; gone
532 -- 533 --
533 t1[key]= nil 534 t1[key] = nil
534 wakeup_at= nil -- no 'continue' in Lua :/ 535 wakeup_at = nil -- no 'continue' in Lua :/
535 else 536 else
536 -- repeating timer; find next wakeup (may jump multiple repeats) 537 -- repeating timer; find next wakeup (may jump multiple repeats)
537 -- 538 --
538 repeat 539 repeat
539 wakeup_at= wakeup_at+period 540 wakeup_at= wakeup_at+period
540 until wakeup_at > now 541 until wakeup_at > now
541 542
542 t2[1]= wakeup_at 543 t2[1]= wakeup_at
@@ -544,7 +545,7 @@ local configure_timers = function()
544 end 545 end
545 546
546 if wakeup_at and ((not next_wakeup) or (wakeup_at < next_wakeup)) then 547 if wakeup_at and ((not next_wakeup) or (wakeup_at < next_wakeup)) then
547 next_wakeup= wakeup_at 548 next_wakeup = wakeup_at
548 end 549 end
549 end 550 end
550 end -- t2 loop 551 end -- t2 loop
@@ -675,7 +676,12 @@ local cancel_error
675local genlock = function(linda_, key_, N) 676local genlock = function(linda_, key_, N)
676 -- clear existing data and set the limit 677 -- clear existing data and set the limit
677 N = N or 1 678 N = N or 1
678 if linda_:set(key_) == cancel_error or linda_:limit(key_, N) == cancel_error then 679 local _status, _err = linda_:set(key_)
680 if _err == cancel_error then
681 return cancel_error
682 end
683 local _status, _err = linda_:limit(key_, N)
684 if _err == cancel_error then
679 return cancel_error 685 return cancel_error
680 end 686 end
681 687
@@ -723,7 +729,12 @@ end -- genlock
723-- 729--
724local genatomic = function(linda_, key_, initial_val_) 730local genatomic = function(linda_, key_, initial_val_)
725 -- clears existing data (also queue). the slot may contain the stored value, and an additional boolean value 731 -- clears existing data (also queue). the slot may contain the stored value, and an additional boolean value
726 if linda_:limit(key_, 2) == cancel_error or linda_:set(key_, initial_val_ or 0.0) == cancel_error then 732 local _status, _err = linda_:limit(key_, 2)
733 if _err == cancel_error then
734 return cancel_error
735 end
736 local _status, _err = linda_:set(key_, initial_val_ or 0.0)
737 if _err == cancel_error then
727 return cancel_error 738 return cancel_error
728 end 739 end
729 740
@@ -734,7 +745,7 @@ local genatomic = function(linda_, key_, initial_val_)
734 if _err == cancel_error then 745 if _err == cancel_error then
735 return cancel_error 746 return cancel_error
736 end 747 end
737 local val = linda_:get(key_) 748 local _, val = linda_:get(key_)
738 if val ~= cancel_error then 749 if val ~= cancel_error then
739 val = val + (diff_ or 1.0) 750 val = val + (diff_ or 1.0)
740 -- set() releases the lock by emptying queue 751 -- set() releases the lock by emptying queue