diff options
Diffstat (limited to 'src/lanes.lua')
-rw-r--r-- | src/lanes.lua | 33 |
1 files changed, 24 insertions, 9 deletions
diff --git a/src/lanes.lua b/src/lanes.lua index c68506d..7ec8c76 100644 --- a/src/lanes.lua +++ b/src/lanes.lua | |||
@@ -6,7 +6,8 @@ | |||
6 | -- Author: Asko Kauppi <akauppi@gmail.com> | 6 | -- Author: Asko Kauppi <akauppi@gmail.com> |
7 | -- | 7 | -- |
8 | -- History: | 8 | -- History: |
9 | -- Jun-08 AKa: major revise | 9 | -- 3-Dec-10 BGe: Added support to generate a lane from a string |
10 | -- Jun-08 AKa: major revise | ||
10 | -- 15-May-07 AKa: pthread_join():less version, some speedup & ability to | 11 | -- 15-May-07 AKa: pthread_join():less version, some speedup & ability to |
11 | -- handle more threads (~ 8000-9000, up from ~ 5000) | 12 | -- handle more threads (~ 8000-9000, up from ~ 5000) |
12 | -- 26-Feb-07 AKa: serialization working (C side) | 13 | -- 26-Feb-07 AKa: serialization working (C side) |
@@ -15,7 +16,7 @@ | |||
15 | --[[ | 16 | --[[ |
16 | =============================================================================== | 17 | =============================================================================== |
17 | 18 | ||
18 | Copyright (C) 2007-08 Asko Kauppi <akauppi@gmail.com> | 19 | Copyright (C) 2007-10 Asko Kauppi <akauppi@gmail.com> |
19 | 20 | ||
20 | Permission is hereby granted, free of charge, to any person obtaining a copy | 21 | Permission is hereby granted, free of charge, to any person obtaining a copy |
21 | of this software and associated documentation files (the "Software"), to deal | 22 | of this software and associated documentation files (the "Software"), to deal |
@@ -89,7 +90,7 @@ ABOUT= | |||
89 | author= "Asko Kauppi <akauppi@gmail.com>", | 90 | author= "Asko Kauppi <akauppi@gmail.com>", |
90 | description= "Running multiple Lua states in parallel", | 91 | description= "Running multiple Lua states in parallel", |
91 | license= "MIT/X11", | 92 | license= "MIT/X11", |
92 | copyright= "Copyright (c) 2007-08, Asko Kauppi", | 93 | copyright= "Copyright (c) 2007-10, Asko Kauppi", |
93 | version= _version, | 94 | version= _version, |
94 | } | 95 | } |
95 | 96 | ||
@@ -123,6 +124,14 @@ end | |||
123 | -- | 124 | -- |
124 | -- lane_h.state: "pending"/"running"/"waiting"/"done"/"error"/"cancelled" | 125 | -- lane_h.state: "pending"/"running"/"waiting"/"done"/"error"/"cancelled" |
125 | -- | 126 | -- |
127 | -- Note: Would be great to be able to have '__ipairs' metamethod, that gets | ||
128 | -- called by 'ipairs()' function to custom iterate objects. We'd use it | ||
129 | -- for making sure a lane has ended (results are available); not requiring | ||
130 | -- the user to precede a loop by explicit 'h[0]' or 'h:join()'. | ||
131 | -- | ||
132 | -- Or, even better, 'ipairs()' should start valuing '__index' instead | ||
133 | -- of using raw reads that bypass it. | ||
134 | -- | ||
126 | local lane_mt= { | 135 | local lane_mt= { |
127 | __index= function( me, k ) | 136 | __index= function( me, k ) |
128 | if type(k) == "number" then | 137 | if type(k) == "number" then |
@@ -260,8 +269,9 @@ function gen( ... ) | |||
260 | end | 269 | end |
261 | 270 | ||
262 | local func= select(n,...) | 271 | local func= select(n,...) |
263 | if type(func)~="function" then | 272 | local functype = type(func) |
264 | error( "Last parameter not function: "..tostring(func) ) | 273 | if functype ~= "function" and functype ~= "string" then |
274 | error( "Last parameter not function or string: "..tostring(func)) | ||
265 | end | 275 | end |
266 | 276 | ||
267 | -- Check 'libs' already here, so the error goes in the right place | 277 | -- Check 'libs' already here, so the error goes in the right place |
@@ -302,9 +312,10 @@ lane_proxy= function( ud ) | |||
302 | local proxy= { | 312 | local proxy= { |
303 | _ud= ud, | 313 | _ud= ud, |
304 | 314 | ||
305 | -- void= me:cancel() | 315 | -- true|false= me:cancel() |
306 | -- | 316 | -- |
307 | cancel= function(me) thread_cancel(me._ud) end, | 317 | cancel= function(me, time, force) return thread_cancel(me._ud, time, force) end, |
318 | |||
308 | 319 | ||
309 | -- [...] | [nil,err,stack_tbl]= me:join( [wait_secs=-1] ) | 320 | -- [...] | [nil,err,stack_tbl]= me:join( [wait_secs=-1] ) |
310 | -- | 321 | -- |
@@ -495,14 +506,18 @@ if first_time then | |||
495 | -- We let the timer lane be a "free running" thread; no handle to it | 506 | -- We let the timer lane be a "free running" thread; no handle to it |
496 | -- remains. | 507 | -- remains. |
497 | -- | 508 | -- |
498 | gen( "io", { priority=max_prio }, function() | 509 | gen( "io", { priority=max_prio, globals={threadName="LanesTimer"} }, function() |
499 | 510 | ||
500 | while true do | 511 | while true do |
501 | local next_wakeup= check_timers() | 512 | local next_wakeup= check_timers() |
502 | 513 | ||
503 | -- Sleep until next timer to wake up, or a set/clear command | 514 | -- Sleep until next timer to wake up, or a set/clear command |
504 | -- | 515 | -- |
505 | local secs= next_wakeup and (next_wakeup - now_secs()) or nil | 516 | local secs |
517 | if next_wakeup then | ||
518 | secs = next_wakeup - now_secs() | ||
519 | if secs < 0 then secs = 0 end | ||
520 | end | ||
506 | local linda= timer_gateway:receive( secs, TGW_KEY ) | 521 | local linda= timer_gateway:receive( secs, TGW_KEY ) |
507 | 522 | ||
508 | if linda then | 523 | if linda then |