Cobra_Tomtrein-temp-

Untitled

Oct 22nd, 2017
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.94 KB | None | 0 0
  1. local taskManager = {}
  2.  
  3.  
  4. local tasks = {}
  5. local aliveTasks = {}
  6.  
  7.  
  8.  
  9. local pidCount = 0
  10.  
  11.  
  12.  
  13.  
  14.  
  15. local env_os = {}
  16.  
  17. for i,v in pairs(os) do
  18. env_os[i] = v
  19. end
  20.  
  21. function env_os.pullEventRaw(sFilter)
  22. while true do
  23. local data = {coroutine.yield()}
  24. if data[1] == sFilter or data[1] == "terminate" then
  25. return table.unpack(data)
  26. end
  27. end
  28. end
  29.  
  30. function env_os.pullEvent( sFilter )
  31. local eventData = table.pack( env_os.pullEventRaw( sFilter ) )
  32. if eventData[1] == "terminate" then
  33. error( "Terminated", 0 )
  34. end
  35. return table.unpack( eventData, 1, eventData.n )
  36. end
  37.  
  38.  
  39. function taskManager.add(func,name)
  40. local env = setmetatable({},{__index = _ENV})
  41. env.os = env_os
  42. local pid = pidCount
  43. pidCount = pidCount + 1
  44. local task = {}
  45. task.pid = pid
  46. task.name = name or "unknown"
  47.  
  48. func = setfenv(func,env)
  49.  
  50.  
  51. local function errorHandle()
  52. local err,info = pcall(func)
  53. if (not err) and task.onError then
  54. task.onError(pid,info)
  55. end
  56. end
  57.  
  58. task.coroutine = coroutine.create(errorHandle)
  59. coroutine.resume(task.coroutine)
  60. tasks[pid] = task
  61. return pid
  62.  
  63. end
  64.  
  65. function taskManager.onError(pid,func)
  66. if tasks[pid] and tasks[pid].coroutine then
  67. tasks[pid].onError = func
  68. end
  69. end
  70.  
  71. function taskManager.kill(pid)
  72. local task = tasks[pid]
  73. if task and task.coroutine then
  74. coroutine.resume(task.coroutine,"terminate")
  75. end
  76. tasks[pid] = {}
  77. end
  78.  
  79. function taskManager.list()
  80. local taskTable = {}
  81. for i,task in pairs(tasks) do
  82. if task.coroutine then
  83. local taskinfo = {}
  84. taskinfo.pid = task.pid
  85. taskinfo.name = task.name
  86. taskTable[#taskTable+1] = taskinfo
  87. end
  88. end
  89. return taskTable
  90. end
  91.  
  92. function taskManager.stop()
  93. for i,task in pairs(taskManager.list()) do
  94. if task.coroutine then
  95. taskManager.kill(task.pid)
  96. end
  97. end
  98. end
  99.  
  100. function taskManager.run(...)
  101. local funcs = {...}
  102. for i,task in pairs(funcs) do
  103. taskManager.add(task)
  104. end
  105. while true do
  106. local event = {coroutine.yield()}
  107. aliveTasks = {}
  108. for i,task in pairs(tasks) do
  109. if task.coroutine and coroutine.status(task.coroutine) ~= "dead" then
  110. aliveTasks[#aliveTasks+1] = task
  111. end
  112. end
  113. if #aliveTasks == 0 then
  114. print("ENDED")
  115. return
  116. end
  117. for i,task in pairs(aliveTasks) do
  118. coroutine.resume(task.coroutine,unpack(event))
  119. end
  120. end
  121. end
  122.  
  123. function displayTime()
  124. local m_term = term.native()
  125. local w,h = m_term.getSize()
  126. local newTerm = window.create(m_term,1,2,w,h-1)
  127. term.redirect(newTerm)
  128. while true do
  129. error("sjdfhaskj")
  130. local x,y = m_term.getCursorPos()
  131. m_term.setCursorPos(1, 1)
  132. m_term.clearLine()
  133. m_term.write(os.time())
  134. m_term.setCursorPos(x,y)
  135. end
  136. end
  137.  
  138.  
  139.  
  140. _G.task = taskManager
  141.  
  142. _G.task.run(function() dofile("rom/programs/lua.lua") end,displayTime)
Advertisement
Add Comment
Please, Sign In to add comment