Guest User

Untitled

a guest
Nov 6th, 2016
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.72 KB | None | 0 0
  1. --#wrapper.lua
  2. tTask={}
  3. rTask={}
  4. rTask[1] = true
  5. rTask[2] = true
  6. rTask[3] = true
  7. ctask=0
  8.  
  9.  
  10.  
  11. function Timed()
  12.  
  13.     while true do
  14.         coroutine.yield()
  15.         local event, timerID = os.pullEvent("timer")
  16.         if timerID == tTask[1] then coroutine.resume(task1) end
  17.         if timerID == tTask[2] then coroutine.resume(task2) end
  18.         if timerID == tTask[3] then coroutine.resume(task3) end
  19.     end
  20.  
  21. end
  22.  
  23. function Event()
  24.  
  25.  
  26.  
  27. end
  28.  
  29. function Launch()
  30.  
  31.     dofile("functions.lua")
  32.     dofile("task.lua")
  33.  
  34. end
  35.  
  36. local t = coroutine.create(Timed)
  37. local e = coroutine.create(Event)
  38.  
  39. local s = coroutine.create(Launch)
  40.  
  41.  
  42. while true do
  43. coroutine.resume(s,t)
  44. sleep(1)
  45. --print(os.clock())
  46. end
  47. --#task.lua
  48.  
  49.  
  50. function task1()
  51.     for i=1,10 do
  52.         a=2
  53.         print(1)
  54.         newSleep(3)
  55.  
  56.     end
  57. end
  58.  
  59.  
  60. function task2()
  61.     for i=1,10 do
  62.         print(2)
  63.         coroutine.yield()
  64.  
  65.     end
  66. end
  67.  
  68.  
  69. function task3()
  70.     for i=1,10 do
  71.         print(3)
  72.         coroutine.yield()
  73.     end
  74. end
  75.  
  76. t1 = coroutine.create(task1)
  77. t2 = coroutine.create(task2)
  78. t3 = coroutine.create(task3)
  79.  
  80. while true do
  81.  coroutine.yield()
  82.  ctask = 1
  83.  if rTask[1] == true then
  84.     coroutine.resume(t1)
  85.     print(coroutine.status(t1))
  86.  end
  87.  
  88.  coroutine.yield()
  89.  ctask = 2
  90.  if rTask[2] == true then
  91.     coroutine.resume(t2)
  92.     print(coroutine.status(t2))
  93.  end
  94.  
  95.  coroutine.yield()
  96.  ctask = 3
  97.  if rTask[3] == true then
  98.     coroutine.resume(t3)
  99.     print(coroutine.status(t3))
  100.  end
  101.  
  102. end
  103. --#functions.lua
  104. local OY = coroutine.yield
  105. --os.pullEvent = os.pullEventRaw
  106. local OPE = os.pullEvent
  107.  
  108. --coroutine = {}
  109.  
  110. function newYield()
  111.  
  112. rTask[ctask] = true
  113. OY()
  114.  
  115. end
  116. function newSleep(sec)
  117.  
  118.     tTask[ctask] = os.startTimer(sec)
  119.     rTask[ctask] = false
  120.  
  121.     coroutine.yield()
  122.  
  123.  
  124. end
  125.  
  126. --coroutine.yield = newYield
Advertisement
Add Comment
Please, Sign In to add comment