diff options
Diffstat (limited to 'src/lanes.lua')
-rw-r--r-- | src/lanes.lua | 107 |
1 files changed, 5 insertions, 102 deletions
diff --git a/src/lanes.lua b/src/lanes.lua index b6fbc08..95bdeeb 100644 --- a/src/lanes.lua +++ b/src/lanes.lua | |||
@@ -45,10 +45,7 @@ local mm = require "lua51-lanes" | |||
45 | assert( type(mm)=="table" ) | 45 | assert( type(mm)=="table" ) |
46 | 46 | ||
47 | 47 | ||
48 | local thread_new= assert(mm.thread_new) | 48 | local thread_new = assert(mm.thread_new) |
49 | local thread_status= assert(mm.thread_status) | ||
50 | local thread_join= assert(mm.thread_join) | ||
51 | local thread_cancel= assert(mm.thread_cancel) | ||
52 | 49 | ||
53 | local _single= assert(mm._single) | 50 | local _single= assert(mm._single) |
54 | local _version= assert(mm._version) | 51 | local _version= assert(mm._version) |
@@ -77,8 +74,6 @@ local type= assert( type ) | |||
77 | local pairs= assert( pairs ) | 74 | local pairs= assert( pairs ) |
78 | local tostring= assert( tostring ) | 75 | local tostring= assert( tostring ) |
79 | local error= assert( error ) | 76 | local error= assert( error ) |
80 | local setmetatable= assert( setmetatable ) | ||
81 | local rawget= assert( rawget ) | ||
82 | 77 | ||
83 | ABOUT= | 78 | ABOUT= |
84 | { | 79 | { |
@@ -127,76 +122,8 @@ end | |||
127 | -- Or, even better, 'ipairs()' should start valuing '__index' instead | 122 | -- Or, even better, 'ipairs()' should start valuing '__index' instead |
128 | -- of using raw reads that bypass it. | 123 | -- of using raw reads that bypass it. |
129 | -- | 124 | -- |
130 | local lane_mt= { | ||
131 | __index= function( me, k ) | ||
132 | if type(k) == "number" then | ||
133 | -- 'me[0]=true' marks we've already taken in the results | ||
134 | -- | ||
135 | if not rawget( me, 0 ) then | ||
136 | -- Wait indefinately; either propagates an error or | ||
137 | -- returns the return values | ||
138 | -- | ||
139 | me[0]= true -- marker, even on errors | ||
140 | |||
141 | local t= { thread_join(me._ud) } -- wait indefinate | ||
142 | -- | ||
143 | -- { ... } "done": regular return, 0..N results | ||
144 | -- { } "cancelled" | ||
145 | -- { nil, err_str, stack_tbl } "error" | ||
146 | |||
147 | local st= thread_status(me._ud) | ||
148 | if st=="done" then | ||
149 | -- Use 'pairs' and not 'ipairs' so that nil holes in | ||
150 | -- the returned values are tolerated. | ||
151 | -- | ||
152 | for i,v in pairs(t) do | ||
153 | me[i]= v | ||
154 | end | ||
155 | elseif st=="error" then | ||
156 | assert( t[1]==nil and t[2] and type(t[3])=="table" ) | ||
157 | me[-1]= t[2] | ||
158 | -- me[-2] could carry the stack table, but even | ||
159 | -- me[-1] is rather unnecessary (and undocumented); | ||
160 | -- use ':join()' instead. --AKa 22-Jan-2009 | ||
161 | elseif st=="cancelled" then | ||
162 | -- do nothing | ||
163 | else | ||
164 | error( "Unexpected status: "..st ) | ||
165 | end | ||
166 | end | ||
167 | |||
168 | -- Check errors even if we'd first peeked them via [-1] | ||
169 | -- and then came for the actual results. | ||
170 | -- | ||
171 | local err= rawget(me, -1) | ||
172 | if err~=nil and k~=-1 then | ||
173 | -- Note: Lua 5.1 interpreter is not prepared to show | ||
174 | -- non-string errors, so we use 'tostring()' here | ||
175 | -- to get meaningful output. --AKa 22-Jan-2009 | ||
176 | -- | ||
177 | -- Also, the stack dump we get is no good; it only | ||
178 | -- lists our internal Lanes functions. There seems | ||
179 | -- to be no way to switch it off, though. | ||
180 | |||
181 | -- Level 3 should show the line where 'h[x]' was read | ||
182 | -- but this only seems to work for string messages | ||
183 | -- (Lua 5.1.4). No idea, why. --AKa 22-Jan-2009 | ||
184 | -- | ||
185 | error( tostring(err), 3 ) -- level 3 should show the line where 'h[x]' was read | ||
186 | end | ||
187 | return rawget( me, k ) | ||
188 | -- | ||
189 | elseif k=="status" then -- me.status | ||
190 | return thread_status(me._ud) | ||
191 | -- | ||
192 | else | ||
193 | error( "Unknown key: "..k ) | ||
194 | end | ||
195 | end | ||
196 | } | ||
197 | |||
198 | ----- | 125 | ----- |
199 | -- h= lanes.gen( [libs_str|opt_tbl [, ...],] lane_func ) ( [...] ) | 126 | -- lanes.gen( [libs_str|opt_tbl [, ...],] lane_func ) ( [...] ) -> h |
200 | -- | 127 | -- |
201 | -- 'libs': nil: no libraries available (default) | 128 | -- 'libs': nil: no libraries available (default) |
202 | -- "": only base library ('assert', 'print', 'unpack' etc.) | 129 | -- "": only base library ('assert', 'print', 'unpack' etc.) |
@@ -220,8 +147,6 @@ local lane_mt= { | |||
220 | -- modifiers, and prepares a lane generator. One can either finish here, | 147 | -- modifiers, and prepares a lane generator. One can either finish here, |
221 | -- and call the generator later (maybe multiple times, with different parameters) | 148 | -- and call the generator later (maybe multiple times, with different parameters) |
222 | -- or add on actual thread arguments to also ignite the thread on the same call. | 149 | -- or add on actual thread arguments to also ignite the thread on the same call. |
223 | -- | ||
224 | local lane_proxy | ||
225 | 150 | ||
226 | local valid_libs= { | 151 | local valid_libs= { |
227 | ["package"]= true, | 152 | ["package"]= true, |
@@ -298,39 +223,16 @@ function gen( ... ) | |||
298 | -- Lane generator | 223 | -- Lane generator |
299 | -- | 224 | -- |
300 | return function(...) | 225 | return function(...) |
301 | return lane_proxy( thread_new( func, libs, cs, prio, g_tbl, | 226 | return thread_new( func, libs, cs, prio, g_tbl, ...) -- args |
302 | ... ) ) -- args | ||
303 | end | 227 | end |
304 | end | 228 | end |
305 | 229 | ||
306 | lane_proxy= function( ud ) | ||
307 | local proxy= { | ||
308 | _ud= ud, | ||
309 | |||
310 | -- true|false= me:cancel() | ||
311 | -- | ||
312 | cancel= function(me, time, force) return thread_cancel(me._ud, time, force) end, | ||
313 | |||
314 | |||
315 | -- [...] | [nil,err,stack_tbl]= me:join( [wait_secs=-1] ) | ||
316 | -- | ||
317 | join= function( me, wait ) | ||
318 | return thread_join( me._ud, wait ) | ||
319 | end, | ||
320 | } | ||
321 | assert( proxy._ud ) | ||
322 | setmetatable( proxy, lane_mt ) | ||
323 | |||
324 | return proxy | ||
325 | end | ||
326 | |||
327 | |||
328 | ---=== Lindas ===--- | 230 | ---=== Lindas ===--- |
329 | 231 | ||
330 | -- We let the C code attach methods to userdata directly | 232 | -- We let the C code attach methods to userdata directly |
331 | 233 | ||
332 | ----- | 234 | ----- |
333 | -- linda_ud= lanes.linda() | 235 | -- lanes.linda() -> linda_ud |
334 | -- | 236 | -- |
335 | linda = mm.linda | 237 | linda = mm.linda |
336 | 238 | ||
@@ -613,5 +515,6 @@ function genatomic( linda, key, initial_val ) | |||
613 | end | 515 | end |
614 | end | 516 | end |
615 | 517 | ||
518 | -- newuserdata = mm.newuserdata | ||
616 | 519 | ||
617 | --the end | 520 | --the end |