diff options
| author | Benoit Germain <benoit.germain@ubisoft.com> | 2024-05-07 09:23:12 +0200 |
|---|---|---|
| committer | Benoit Germain <benoit.germain@ubisoft.com> | 2024-05-13 18:15:46 +0200 |
| commit | ebb0837588336e32fc1258a3838673afdd31ee71 (patch) | |
| tree | f7590e7d8d22fe62f9022bf6cb1ae442dccc5861 | |
| parent | 2c69c99da47b1d708606da713433608c73ecdb88 (diff) | |
| download | lanes-ebb0837588336e32fc1258a3838673afdd31ee71.tar.gz lanes-ebb0837588336e32fc1258a3838673afdd31ee71.tar.bz2 lanes-ebb0837588336e32fc1258a3838673afdd31ee71.zip | |
API changes
* lanes.sleep accepts 'indefinitely'.
* settings.with_timers is false by default
| -rw-r--r-- | docs/index.html | 6 | ||||
| -rw-r--r-- | src/lanes.lua | 6 | ||||
| -rw-r--r-- | tests/track_lanes.lua | 50 |
3 files changed, 36 insertions, 26 deletions
diff --git a/docs/index.html b/docs/index.html index f08947e..e811074 100644 --- a/docs/index.html +++ b/docs/index.html | |||
| @@ -311,7 +311,7 @@ | |||
| 311 | </td> | 311 | </td> |
| 312 | <td> | 312 | <td> |
| 313 | If equal to <tt>false</tt> or <tt>nil</tt>, Lanes doesn't start the timer service, and the associated API will be absent from the interface (see below). | 313 | If equal to <tt>false</tt> or <tt>nil</tt>, Lanes doesn't start the timer service, and the associated API will be absent from the interface (see below). |
| 314 | Default is <tt>true</tt>. | 314 | Default is <tt>false</tt>. |
| 315 | </td> | 315 | </td> |
| 316 | </tr> | 316 | </tr> |
| 317 | 317 | ||
| @@ -1328,7 +1328,7 @@ events to a common Linda, but... :).</font> | |||
| 1328 | </pre></td></tr></table> | 1328 | </pre></td></tr></table> |
| 1329 | 1329 | ||
| 1330 | <p> | 1330 | <p> |
| 1331 | Timers are implemented as a lane. They can be disabled by setting "<tt><a href="#with_timers">with_timers</a></tt>" to <tt>nil</tt> or <tt>false</tt> to <tt>lanes.configure()</tt>. | 1331 | Timers are implemented as a lane. They can be enabled by setting "<tt><a href="#with_timers">with_timers</a></tt>" to <tt>true</tt> in <tt>lanes.configure()</tt> settings. |
| 1332 | </p> | 1332 | </p> |
| 1333 | 1333 | ||
| 1334 | <p> | 1334 | <p> |
| @@ -1395,7 +1395,7 @@ events to a common Linda, but... :).</font> | |||
| 1395 | </p> | 1395 | </p> |
| 1396 | 1396 | ||
| 1397 | <table border="1" bgcolor="#E0E0FF" cellpadding="10" style="width:50%"><tr><td><pre> | 1397 | <table border="1" bgcolor="#E0E0FF" cellpadding="10" style="width:50%"><tr><td><pre> |
| 1398 | void = lanes.sleep([seconds|false]) | 1398 | void = lanes.sleep(['indefinitely'|seconds|false]) |
| 1399 | </pre></td></tr></table> | 1399 | </pre></td></tr></table> |
| 1400 | 1400 | ||
| 1401 | <p> | 1401 | <p> |
diff --git a/src/lanes.lua b/src/lanes.lua index f95dddf..caa8818 100644 --- a/src/lanes.lua +++ b/src/lanes.lua | |||
| @@ -71,7 +71,7 @@ local default_params = | |||
| 71 | on_state_create = nil, | 71 | on_state_create = nil, |
| 72 | shutdown_timeout = 0.25, | 72 | shutdown_timeout = 0.25, |
| 73 | shutdown_mode = "hard", | 73 | shutdown_mode = "hard", |
| 74 | with_timers = true, | 74 | with_timers = false, |
| 75 | track_lanes = false, | 75 | track_lanes = false, |
| 76 | demote_full_userdata = nil, | 76 | demote_full_userdata = nil, |
| 77 | verbose_errors = false, | 77 | verbose_errors = false, |
| @@ -626,7 +626,9 @@ end | |||
| 626 | -- PUBLIC LANES API | 626 | -- PUBLIC LANES API |
| 627 | local sleep = function(seconds_) | 627 | local sleep = function(seconds_) |
| 628 | seconds_ = seconds_ or 0.0 -- this causes false and nil to be a valid input, equivalent to 0.0, but that's ok | 628 | seconds_ = seconds_ or 0.0 -- this causes false and nil to be a valid input, equivalent to 0.0, but that's ok |
| 629 | if type(seconds_) ~= "number" then | 629 | if seconds_ == 'indefinitely' then |
| 630 | seconds_ = nil | ||
| 631 | elseif type(seconds_) ~= "number" then | ||
| 630 | error("invalid duration " .. string_format("%q", tostring(seconds_))) | 632 | error("invalid duration " .. string_format("%q", tostring(seconds_))) |
| 631 | end | 633 | end |
| 632 | -- receive data on a channel no-one ever sends anything, thus blocking for the specified duration | 634 | -- receive data on a channel no-one ever sends anything, thus blocking for the specified duration |
diff --git a/tests/track_lanes.lua b/tests/track_lanes.lua index 46cfdad..daaa94c 100644 --- a/tests/track_lanes.lua +++ b/tests/track_lanes.lua | |||
| @@ -1,22 +1,20 @@ | |||
| 1 | local lanes = require "lanes" .configure{ with_timers = false, track_lanes = true} | 1 | local lanes = require "lanes" .configure{ with_timers = false, track_lanes = true} |
| 2 | 2 | local wait = lanes.sleep | |
| 3 | local wait | ||
| 4 | do | ||
| 5 | local linda = lanes.linda() | ||
| 6 | wait = function( seconds_) | ||
| 7 | linda:receive( seconds_, "dummy_key") | ||
| 8 | end | ||
| 9 | end | ||
| 10 | 3 | ||
| 11 | print "hello" | 4 | print "hello" |
| 12 | 5 | ||
| 13 | local track = function( title_) | 6 | local track = function( title_, expected_count_) |
| 14 | print( title_) | 7 | print( title_) |
| 15 | for k, v in pairs( lanes.threads()) | 8 | local count = 0 |
| 9 | local threads = lanes.threads() | ||
| 10 | for k, v in pairs(threads) | ||
| 16 | do | 11 | do |
| 17 | print( k, v.name, v.status) | 12 | print( k, v.name, v.status) |
| 13 | count = count + 1 | ||
| 18 | end | 14 | end |
| 15 | assert(count == expected_count_, "unexpected active lane count") | ||
| 19 | print( "\n") | 16 | print( "\n") |
| 17 | return threads | ||
| 20 | end | 18 | end |
| 21 | 19 | ||
| 22 | local sleeper = function( name_, seconds_) | 20 | local sleeper = function( name_, seconds_) |
| @@ -29,7 +27,7 @@ local sleeper = function( name_, seconds_) | |||
| 29 | set_debug_threadname( name_) | 27 | set_debug_threadname( name_) |
| 30 | end | 28 | end |
| 31 | -- suspend the lane for the specified duration with a failed linda read | 29 | -- suspend the lane for the specified duration with a failed linda read |
| 32 | wait( seconds_) | 30 | lanes.sleep(seconds_) |
| 33 | -- print( "exiting '" .. name_ .. "'") | 31 | -- print( "exiting '" .. name_ .. "'") |
| 34 | end | 32 | end |
| 35 | 33 | ||
| @@ -39,7 +37,7 @@ end | |||
| 39 | local g = lanes.gen( "*", sleeper) | 37 | local g = lanes.gen( "*", sleeper) |
| 40 | 38 | ||
| 41 | -- start a forever-waiting lane (nil timeout) | 39 | -- start a forever-waiting lane (nil timeout) |
| 42 | g( "forever") | 40 | g( "forever", 'indefinitely') |
| 43 | 41 | ||
| 44 | -- start a lane that will last 2 seconds | 42 | -- start a lane that will last 2 seconds |
| 45 | g( "two_seconds", 2) | 43 | g( "two_seconds", 2) |
| @@ -47,18 +45,17 @@ g( "two_seconds", 2) | |||
| 47 | -- give a bit of time to reach the linda waiting call | 45 | -- give a bit of time to reach the linda waiting call |
| 48 | wait( 0.1) | 46 | wait( 0.1) |
| 49 | 47 | ||
| 50 | -- list the known lanes | 48 | -- list the known lanes (should be living lanes) |
| 51 | track( "============= START") | 49 | local threads = track( "============= START", 2) |
| 50 | -- two_seconds forever | ||
| 51 | assert(threads[1].status == 'waiting' and threads[2].status == 'waiting') | ||
| 52 | 52 | ||
| 53 | -- wait until "two_seconds has completed" | 53 | -- wait until "two_seconds has completed" |
| 54 | wait(2.1) | 54 | wait(2.1) |
| 55 | 55 | ||
| 56 | track( "============= two_seconds dead") | 56 | local threads = track( "============= two_seconds dead", 2) |
| 57 | 57 | -- two_seconds forever | |
| 58 | -- this will collect the completed lane (and remove it from the tracking queue) | 58 | assert(threads[1].status == 'done' and threads[2].status == 'waiting') |
| 59 | -- collectgarbage() | ||
| 60 | |||
| 61 | -- track( "============= two_seconds dead (after collectgarbage)") | ||
| 62 | 59 | ||
| 63 | -- start another lane that will last 2 seconds, with the same name | 60 | -- start another lane that will last 2 seconds, with the same name |
| 64 | g( "two_seconds", 2) | 61 | g( "two_seconds", 2) |
| @@ -67,6 +64,17 @@ g( "two_seconds", 2) | |||
| 67 | wait( 0.1) | 64 | wait( 0.1) |
| 68 | 65 | ||
| 69 | -- list the known lanes | 66 | -- list the known lanes |
| 70 | track( "============= ANOTHER") | 67 | -- unless garbage collector cleaned it, we should have 3 lanes |
| 68 | local threads = track( "============= ANOTHER", 3) | ||
| 69 | -- two_seconds #2 two_seconds #1 forever | ||
| 70 | assert(threads[1].status == 'waiting' and threads[2].status == 'done' and threads[3].status == 'waiting') | ||
| 71 | |||
| 72 | -- this will collect the completed lane (and remove it from the tracking queue) | ||
| 73 | collectgarbage() | ||
| 74 | |||
| 75 | -- list the known lanes | ||
| 76 | local threads = track( "============= AFTER COLLECTGARBAGE", 2) | ||
| 77 | -- two_seconds #2 forever | ||
| 78 | assert(threads[1].status == 'waiting' and threads[2].status == 'waiting') | ||
| 71 | 79 | ||
| 72 | print "done" \ No newline at end of file | 80 | print "done" \ No newline at end of file |
