diff options
author | Benoit Germain <bnt.germain@gmail.com> | 2012-12-12 10:51:56 +0100 |
---|---|---|
committer | Benoit Germain <bnt.germain@gmail.com> | 2012-12-12 10:51:56 +0100 |
commit | a0a8e992e9f8b14189f506e7730c562da6555682 (patch) | |
tree | 36a8ad48540646588082c42993d5fb71132829cd | |
parent | d890d38e425ea65c272a552537029072dcfec12f (diff) | |
download | lanes-a0a8e992e9f8b14189f506e7730c562da6555682.tar.gz lanes-a0a8e992e9f8b14189f506e7730c562da6555682.tar.bz2 lanes-a0a8e992e9f8b14189f506e7730c562da6555682.zip |
New API lanes.timers()
-rw-r--r-- | src/lanes.lua | 54 |
1 files changed, 42 insertions, 12 deletions
diff --git a/src/lanes.lua b/src/lanes.lua index 20168b8..b39e31f 100644 --- a/src/lanes.lua +++ b/src/lanes.lua | |||
@@ -3,20 +3,16 @@ | |||
3 | -- | 3 | -- |
4 | -- Multithreading and -core support for Lua | 4 | -- Multithreading and -core support for Lua |
5 | -- | 5 | -- |
6 | -- Author: Asko Kauppi <akauppi@gmail.com> | 6 | -- Authors: Asko Kauppi <akauppi@gmail.com> |
7 | -- Benoit Germain <bnt.germain@gmail.com> | ||
7 | -- | 8 | -- |
8 | -- History: | 9 | -- History: see CHANGES |
9 | -- 3-Dec-10 BGe: Added support to generate a lane from a string | ||
10 | -- Jun-08 AKa: major revise | ||
11 | -- 15-May-07 AKa: pthread_join():less version, some speedup & ability to | ||
12 | -- handle more threads (~ 8000-9000, up from ~ 5000) | ||
13 | -- 26-Feb-07 AKa: serialization working (C side) | ||
14 | -- 17-Sep-06 AKa: started the module (serialization) | ||
15 | -- | 10 | -- |
16 | --[[ | 11 | --[[ |
17 | =============================================================================== | 12 | =============================================================================== |
18 | 13 | ||
19 | Copyright (C) 2007-10 Asko Kauppi <akauppi@gmail.com> | 14 | Copyright (C) 2007-10 Asko Kauppi <akauppi@gmail.com> |
15 | Copyright (C) 2010-13 Benoit Germain <bnt.germain@gmail.com> | ||
20 | 16 | ||
21 | Permission is hereby granted, free of charge, to any person obtaining a copy | 17 | Permission is hereby granted, free of charge, to any person obtaining a copy |
22 | of this software and associated documentation files (the "Software"), to deal | 18 | of this software and associated documentation files (the "Software"), to deal |
@@ -303,6 +299,7 @@ local linda = core.linda | |||
303 | 299 | ||
304 | -- PUBLIC LANES API | 300 | -- PUBLIC LANES API |
305 | local timer = function() error "timers are not active" end | 301 | local timer = function() error "timers are not active" end |
302 | local timers = timer | ||
306 | 303 | ||
307 | if _params.with_timers ~= false then | 304 | if _params.with_timers ~= false then |
308 | 305 | ||
@@ -318,6 +315,7 @@ local timer_gateway = assert( core.timer_gateway) | |||
318 | -- TGW_KEY: linda_h, key, [wakeup_at_secs], [repeat_secs] | 315 | -- TGW_KEY: linda_h, key, [wakeup_at_secs], [repeat_secs] |
319 | -- | 316 | -- |
320 | local TGW_KEY= "(timer control)" -- the key does not matter, a 'weird' key may help debugging | 317 | local TGW_KEY= "(timer control)" -- the key does not matter, a 'weird' key may help debugging |
318 | local TGW_QUERY, TGW_REPLY = "(timer query)", "(timer reply)" | ||
321 | local first_time_key= "first time" | 319 | local first_time_key= "first time" |
322 | 320 | ||
323 | local first_time= timer_gateway:get(first_time_key) == nil | 321 | local first_time= timer_gateway:get(first_time_key) == nil |
@@ -349,6 +347,19 @@ if first_time then | |||
349 | -- | 347 | -- |
350 | local collection= {} | 348 | local collection= {} |
351 | 349 | ||
350 | local function get_timers() | ||
351 | local r = {} | ||
352 | for deep, t in pairs( collection) do | ||
353 | -- WR( tostring( deep)) | ||
354 | local l = t[deep] | ||
355 | for key, timer_data in pairs( t) do | ||
356 | if key ~= deep then | ||
357 | table_insert( r, {l, key, timer_data}) | ||
358 | end | ||
359 | end | ||
360 | end | ||
361 | return r | ||
362 | end | ||
352 | -- | 363 | -- |
353 | -- set_timer( linda_h, key [,wakeup_at_secs [,period_secs]] ) | 364 | -- set_timer( linda_h, key [,wakeup_at_secs [,period_secs]] ) |
354 | -- | 365 | -- |
@@ -488,12 +499,18 @@ if first_time then | |||
488 | secs = next_wakeup - now_secs() | 499 | secs = next_wakeup - now_secs() |
489 | if secs < 0 then secs = 0 end | 500 | if secs < 0 then secs = 0 end |
490 | end | 501 | end |
491 | local _, linda = timer_gateway:receive( secs, TGW_KEY) | 502 | local key, what = timer_gateway:receive( secs, TGW_KEY, TGW_QUERY) |
492 | 503 | ||
493 | if linda then | 504 | if key == TGW_KEY then |
505 | assert( getmetatable( what) == "Linda") -- 'what' should be a linda on which the client sets a timer | ||
494 | local _, key, wakeup_at, period = timer_gateway:receive( 0, timer_gateway_batched, TGW_KEY, 3) | 506 | local _, key, wakeup_at, period = timer_gateway:receive( 0, timer_gateway_batched, TGW_KEY, 3) |
495 | assert( key) | 507 | assert( key) |
496 | set_timer( linda, key, wakeup_at, period and period > 0 and period or nil) | 508 | set_timer( what, key, wakeup_at, period and period > 0 and period or nil) |
509 | elseif key == TGW_QUERY then | ||
510 | if what == "get_timers" then | ||
511 | timer_gateway:send( TGW_REPLY, get_timers()) | ||
512 | end | ||
513 | timer_gateway:send( TGW_REPLY, "unknown query " .. what) | ||
497 | --elseif secs == nil then -- got no value while block-waiting? | 514 | --elseif secs == nil then -- got no value while block-waiting? |
498 | -- WR( "timer lane: no linda, aborted?") | 515 | -- WR( "timer lane: no linda, aborted?") |
499 | end | 516 | end |
@@ -507,7 +524,9 @@ end | |||
507 | -- | 524 | -- |
508 | -- PUBLIC LANES API | 525 | -- PUBLIC LANES API |
509 | timer = function( linda, key, a, period ) | 526 | timer = function( linda, key, a, period ) |
510 | 527 | if getmetatable( linda) ~= "Linda" then | |
528 | error "expecting a Linda" | ||
529 | end | ||
511 | if a==0.0 then | 530 | if a==0.0 then |
512 | -- Caller expects to get current time stamp in Linda, on return | 531 | -- Caller expects to get current time stamp in Linda, on return |
513 | -- (like the timer had expired instantly); it would be good to set this | 532 | -- (like the timer had expired instantly); it would be good to set this |
@@ -530,6 +549,16 @@ timer = function( linda, key, a, period ) | |||
530 | timer_gateway:send( TGW_KEY, linda, key, wakeup_at, period ) | 549 | timer_gateway:send( TGW_KEY, linda, key, wakeup_at, period ) |
531 | end | 550 | end |
532 | 551 | ||
552 | ----- | ||
553 | -- {[{linda, slot, when, period}[,...]]} = timers() | ||
554 | -- | ||
555 | -- PUBLIC LANES API | ||
556 | timers = function() | ||
557 | timer_gateway:send( TGW_QUERY, "get_timers") | ||
558 | local _, r = timer_gateway:receive( TGW_REPLY) | ||
559 | return r | ||
560 | end | ||
561 | |||
533 | end -- _params.with_timers | 562 | end -- _params.with_timers |
534 | 563 | ||
535 | ---=== Lock & atomic generators ===--- | 564 | ---=== Lock & atomic generators ===--- |
@@ -602,6 +631,7 @@ end | |||
602 | lanes.cancel_error = core.cancel_error | 631 | lanes.cancel_error = core.cancel_error |
603 | lanes.nameof = core.nameof | 632 | lanes.nameof = core.nameof |
604 | lanes.timer = timer | 633 | lanes.timer = timer |
634 | lanes.timers = timers | ||
605 | lanes.genlock = genlock | 635 | lanes.genlock = genlock |
606 | lanes.now_secs = now_secs | 636 | lanes.now_secs = now_secs |
607 | lanes.genatomic = genatomic | 637 | lanes.genatomic = genatomic |