aboutsummaryrefslogtreecommitdiff
path: root/src/lanes.lua
diff options
context:
space:
mode:
authorBenoit Germain <bnt.germain@gmail.com>2012-02-18 14:08:52 +0100
committerBenoit Germain <bnt.germain@gmail.com>2012-02-18 14:08:52 +0100
commit076344633e7b2525cf48005f84f3935f324b60bf (patch)
tree2a013da0ba2cd4a9f7eaf073a5ba128723f13154 /src/lanes.lua
parentb4582dd0bb65a916de7fa9612880b75a17c7ae7f (diff)
downloadlanes-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.lua32
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
46local lanes = {} 46local lanes = {}
47 47
48lanes.configure = function( _nb_keepers, _timers) 48lanes.configure = function( _params)
49_params = _params or { nb_keepers = 1, with_timers = true, on_state_create = nil}
50if type( _params) ~= "table" then
51 error( "Bad parameter #1 to lanes.configure(), should be a table")
52end
53-- on_state_create may be nil or a function
54if _params.on_state_create and (type( _params.on_state_create) ~= "function") then
55 error( "Bad on_state_create: " .. tostring( _params.on_state_create), 2)
56end
49 57
50local mm = require "lua51-lanes" 58local mm = require "lua51-lanes"
51assert( type(mm)=="table" ) 59assert( 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
54if mm.configure then mm.configure( _nb_keepers) end 62if mm.configure then mm.configure( _params.nb_keepers, _params.on_state_create) end
55 63
56local thread_new = assert(mm.thread_new) 64local 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
243end 251end
244 252
@@ -258,7 +266,7 @@ local linda = mm.linda
258-- PUBLIC LANES API 266-- PUBLIC LANES API
259local timer = function() error "timers are not active" end 267local timer = function() error "timers are not active" end
260 268
261if _timers ~= "NO_TIMERS" then 269if _params.with_timers ~= false then
262 270
263local timer_gateway= assert( mm.timer_gateway ) 271local 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 )
478end 486end
479 487
480end -- _timers 488end -- _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