diff options
author | Benoit Germain <bnt.germain@gmail.com> | 2012-02-18 14:08:52 +0100 |
---|---|---|
committer | Benoit Germain <bnt.germain@gmail.com> | 2012-02-18 14:08:52 +0100 |
commit | 076344633e7b2525cf48005f84f3935f324b60bf (patch) | |
tree | 2a013da0ba2cd4a9f7eaf073a5ba128723f13154 /src/lanes.lua | |
parent | b4582dd0bb65a916de7fa9612880b75a17c7ae7f (diff) | |
download | lanes-076344633e7b2525cf48005f84f3935f324b60bf.tar.gz lanes-076344633e7b2525cf48005f84f3935f324b60bf.tar.bz2 lanes-076344633e7b2525cf48005f84f3935f324b60bf.zip |
* changed lanes.configure signature to receive a table instead of individual parameters
* added support for an on_state_create callback called to load custom functions in a state in addition to the base libraries
Diffstat (limited to 'src/lanes.lua')
-rw-r--r-- | src/lanes.lua | 32 |
1 files changed, 22 insertions, 10 deletions
diff --git a/src/lanes.lua b/src/lanes.lua index d56e05f..ec5a4e4 100644 --- a/src/lanes.lua +++ b/src/lanes.lua | |||
@@ -45,13 +45,21 @@ THE SOFTWARE. | |||
45 | -- -> simply create a table, populate it, return it, and be done | 45 | -- -> simply create a table, populate it, return it, and be done |
46 | local lanes = {} | 46 | local lanes = {} |
47 | 47 | ||
48 | lanes.configure = function( _nb_keepers, _timers) | 48 | lanes.configure = function( _params) |
49 | _params = _params or { nb_keepers = 1, with_timers = true, on_state_create = nil} | ||
50 | if type( _params) ~= "table" then | ||
51 | error( "Bad parameter #1 to lanes.configure(), should be a table") | ||
52 | end | ||
53 | -- on_state_create may be nil or a function | ||
54 | if _params.on_state_create and (type( _params.on_state_create) ~= "function") then | ||
55 | error( "Bad on_state_create: " .. tostring( _params.on_state_create), 2) | ||
56 | end | ||
49 | 57 | ||
50 | local mm = require "lua51-lanes" | 58 | local mm = require "lua51-lanes" |
51 | assert( type(mm)=="table" ) | 59 | assert( type(mm)=="table" ) |
52 | 60 | ||
53 | -- configure() is available only the first time lua51-lanes is required process-wide, and we *must* call it to have the other functions in the interface | 61 | -- configure() is available only the first time lua51-lanes is required process-wide, and we *must* call it to have the other functions in the interface |
54 | if mm.configure then mm.configure( _nb_keepers) end | 62 | if mm.configure then mm.configure( _params.nb_keepers, _params.on_state_create) end |
55 | 63 | ||
56 | local thread_new = assert(mm.thread_new) | 64 | local thread_new = assert(mm.thread_new) |
57 | 65 | ||
@@ -238,7 +246,7 @@ local function gen( ... ) | |||
238 | -- Lane generator | 246 | -- Lane generator |
239 | -- | 247 | -- |
240 | return function(...) | 248 | return function(...) |
241 | return thread_new( func, libs, cs, prio, g_tbl, package_tbl, required, ...) -- args | 249 | return thread_new( func, libs, _params.on_state_create, cs, prio, g_tbl, package_tbl, required, ...) -- args |
242 | end | 250 | end |
243 | end | 251 | end |
244 | 252 | ||
@@ -258,7 +266,7 @@ local linda = mm.linda | |||
258 | -- PUBLIC LANES API | 266 | -- PUBLIC LANES API |
259 | local timer = function() error "timers are not active" end | 267 | local timer = function() error "timers are not active" end |
260 | 268 | ||
261 | if _timers ~= "NO_TIMERS" then | 269 | if _params.with_timers ~= false then |
262 | 270 | ||
263 | local timer_gateway= assert( mm.timer_gateway ) | 271 | local timer_gateway= assert( mm.timer_gateway ) |
264 | -- | 272 | -- |
@@ -477,7 +485,7 @@ timer = function( linda, key, a, period ) | |||
477 | timer_gateway:send( TGW_KEY, linda, key, wakeup_at, period ) | 485 | timer_gateway:send( TGW_KEY, linda, key, wakeup_at, period ) |
478 | end | 486 | end |
479 | 487 | ||
480 | end -- _timers | 488 | end -- _params.with_timers |
481 | 489 | ||
482 | ---=== Lock & atomic generators ===--- | 490 | ---=== Lock & atomic generators ===--- |
483 | 491 | ||
@@ -552,12 +560,16 @@ end | |||
552 | lanes.genlock = genlock | 560 | lanes.genlock = genlock |
553 | lanes.genatomic = genatomic | 561 | lanes.genatomic = genatomic |
554 | -- from now on, calling configure does nothing but checking that we don't call it with parameters that changed compared to the first invocation | 562 | -- from now on, calling configure does nothing but checking that we don't call it with parameters that changed compared to the first invocation |
555 | lanes.configure = function( _nk, _t) | 563 | lanes.configure = function( _params2) |
556 | if _nk ~= _nb_keepers then | 564 | _params2 = _params2 or _params |
557 | error( "mismatched configuration: " .. tostring( _nk) .. " keepers instead of " .. tostring( _nb_keepers)) | 565 | if _params2.nb_keepers ~= _params.nb_keepers then |
566 | error( "mismatched configuration: " .. tostring( _params2.nb_keepers) .. " keepers instead of " .. tostring( _params.nb_keepers)) | ||
567 | end | ||
568 | if _params2.with_timers ~= _params.with_timers then | ||
569 | error( "mismatched configuration: " .. tostring( _params2.with_timers) .. " timer activity instead of " .. tostring( _params.with_timers)) | ||
558 | end | 570 | end |
559 | if _t ~= _timers then | 571 | if _params2.on_create_state and _params2.on_create_state ~= _params.on_create_state then |
560 | error( "mismatched configuration: " .. tostring( _t) .. " timer activity instead of " .. tostring( _timers)) | 572 | error( "mismatched configuration: " .. tostring( _params2.on_create_state) .. " timer activity instead of " .. tostring( _params.on_create_state)) |
561 | end | 573 | end |
562 | return lanes | 574 | return lanes |
563 | end | 575 | end |