diff options
| author | Benoit Germain <bnt.germain@gmail.com> | 2013-11-16 10:39:34 +0100 |
|---|---|---|
| committer | Benoit Germain <bnt.germain@gmail.com> | 2013-11-16 10:39:34 +0100 |
| commit | 3300bf6d0a7914d1e0914f90fdd5d968fcbfe010 (patch) | |
| tree | 73b45cad87783697d6b1353a070537588e0c8132 | |
| parent | 0860a7174bf376faa2f14803d720769b82ba1106 (diff) | |
| download | lanes-3300bf6d0a7914d1e0914f90fdd5d968fcbfe010.tar.gz lanes-3300bf6d0a7914d1e0914f90fdd5d968fcbfe010.tar.bz2 lanes-3300bf6d0a7914d1e0914f90fdd5d968fcbfe010.zip | |
lanes.genlock() improvements and fix
* Fixed function returned by lanes.genlock() not handling numeric keys properly when release lock
* Enable lanes.genlock() to attempt lock with an optional "try" mode
| -rw-r--r-- | src/lanes.lua | 54 |
1 files changed, 34 insertions, 20 deletions
diff --git a/src/lanes.lua b/src/lanes.lua index 0e27ea6..1a1c252 100644 --- a/src/lanes.lua +++ b/src/lanes.lua | |||
| @@ -614,28 +614,42 @@ end -- settings.with_timers | |||
| 614 | -- acquire (+M) and release (-M). For binary locks, use M==1. | 614 | -- acquire (+M) and release (-M). For binary locks, use M==1. |
| 615 | -- | 615 | -- |
| 616 | -- PUBLIC LANES API | 616 | -- PUBLIC LANES API |
| 617 | local function genlock( linda, key, N ) | 617 | local genlock = function( linda, key, N) |
| 618 | linda:limit(key,N) | 618 | linda:limit( key, N) |
| 619 | linda:set(key,nil) -- clears existing data | 619 | linda:set( key, nil) -- clears existing data |
| 620 | 620 | ||
| 621 | -- | 621 | -- |
| 622 | -- [true [, ...]= trues(uint) | 622 | -- [true [, ...]= trues(uint) |
| 623 | -- | 623 | -- |
| 624 | local function trues(n) | 624 | local function trues( n) |
| 625 | if n>0 then return true,trues(n-1) end | 625 | if n > 0 then |
| 626 | end | 626 | return true, trues( n - 1) |
| 627 | end | ||
| 628 | end | ||
| 627 | 629 | ||
| 628 | return | 630 | -- use an optimized version for case N == 1 |
| 629 | function(M) | 631 | return (N == 1) and |
| 630 | if M>0 then | 632 | function( M, mode_) |
| 631 | -- 'nil' timeout allows 'key' to be numeric | 633 | local timeout = (mode_ == "try") and 0 or nil |
| 632 | linda:send( nil, key, trues(M) ) -- suspends until been able to push them | 634 | if M > 0 then |
| 633 | else | 635 | -- 'nil' timeout allows 'key' to be numeric |
| 634 | for i=1,-M do | 636 | return linda:send( timeout, key, true) -- suspends until been able to push them |
| 635 | linda:receive( key ) | 637 | else |
| 636 | end | 638 | local k = linda:receive( nil, key) |
| 637 | end | 639 | return k and true or false |
| 638 | end | 640 | end |
| 641 | end | ||
| 642 | or | ||
| 643 | function( M, mode_) | ||
| 644 | local timeout = (mode_ == "try") and 0 or nil | ||
| 645 | if M > 0 then | ||
| 646 | -- 'nil' timeout allows 'key' to be numeric | ||
| 647 | return linda:send( timeout, key, trues(M)) -- suspends until been able to push them | ||
| 648 | else | ||
| 649 | local k = linda:receive( nil, linda.batched, key, -M) | ||
| 650 | return k and true or false | ||
| 651 | end | ||
| 652 | end | ||
| 639 | end | 653 | end |
| 640 | 654 | ||
| 641 | 655 | ||
