Advertisement
Guest User

OC Parallel Library

a guest
Sep 21st, 2015
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.42 KB | None | 0 0
  1. local list = {}
  2. coroutine = require("coroutine")
  3. os = require("os")
  4.  
  5. function list.create( first, ... )
  6.         if first ~= nil then
  7.             if type( first ) ~= "function" then
  8.                 error( "Expected function, got "..type( first ), 3 )
  9.         end
  10.                 return coroutine.create(first), list.create( ... )
  11.     end
  12.     return nil
  13. end
  14.  
  15. local function runUntilLimit( _routines, _limit )
  16.     local count = #_routines
  17.     local living = count
  18.  
  19.     local tFilters = {}
  20.     local eventData = {}
  21.     while true do
  22.         for n=1,count do
  23.                 local r = _routines[n]
  24.                 if r then
  25.                         if tFilters[r] == nil or tFilters[r] == eventData[1] or eventData[1] == "terminate" then
  26.                                 local ok, param = coroutine.resume( r, table.unpack(eventData) )
  27.                                         if not ok then
  28.                                                 error( param, 0 )
  29.                                         else
  30.                                                 tFilters[r] = param
  31.                                         end
  32.                                         if coroutine.status( r ) == "dead" then
  33.                                                 _routines[n] = nil
  34.                                                 living = living - 1
  35.                                                 if living <= _limit then
  36.                                                         return n
  37.                                                 end
  38.                                         end
  39.                                 end
  40.                 end
  41.         end
  42.                 for n=1,count do
  43.                 local r = _routines[n]
  44.                         if r and coroutine.status( r ) == "dead" then
  45.                                 _routines[n] = nil
  46.                                 living = living - 1
  47.                                 if living <= _limit then
  48.                                         return n
  49.                                 end
  50.                         end
  51.                 end
  52.         eventData = { os.pullEventRaw() }
  53.     end
  54. end
  55.  
  56. function list.waitForAny( ... )
  57.     local routines = { list.create( ... ) }
  58.     return runUntilLimit( routines, #routines - 1 )
  59. end
  60.  
  61. function list.waitForAll( ... )
  62.     local routines = { list.create( ... ) }
  63.         runUntilLimit( routines, 0 )
  64.  
  65. end
  66.  
  67. return list --return the list of functions
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement