Advertisement
Guest User

Untitled

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