Guest User

Untitled

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