Alakazard12

Untitled

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