Advertisement
Freack100

Multitasking

May 16th, 2014
363
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.27 KB | None | 0 0
  1. --[[
  2.  
  3. #   Freack100's first try at a "multitasking" manager
  4. #   I highly recommend to NOT use this in actual programs because it might be unoptimized and only for development!
  5. #   You ARE allowed to used it everywhere and modify the code. But you MUST give credits to me as the owner of the "core" of the code.
  6.  
  7. ]]
  8.  
  9. local _tasks = {}
  10. local _filter = {}
  11. local _coreTasks = {}
  12. local _runningTask = 0
  13. local stackRunning = false
  14.  
  15. --[[
  16.  
  17.     @description
  18.     Allows you to add a new task to the stack
  19.  
  20.     @param func
  21.     The function you want to use as task
  22.  
  23.     @return
  24.     Returns the ID of the task you added
  25.  
  26. ]]
  27. local function addTask(func)
  28.     if type(func)  ~= "function" then error("Please only feed me with functions!",2) end
  29.     _tasks[#_tasks+1] = coroutine.create(func)
  30.     return #_tasks
  31. end
  32.  
  33. --[[
  34.  
  35.     @description
  36.     Allows you to remove a task from the stack
  37.  
  38.     @param ID
  39.     The ID of the coroutine you want to remove
  40.  
  41.     @return
  42.     Returns nothing
  43.  
  44. ]]
  45. local function removeTask(ID)
  46.     if _tasks[ID] == nil then error("I never have eaten that function, sorry.",2) end
  47.     _tasks[ID] = nil
  48. end
  49.  
  50. --[[
  51.  
  52.     @description
  53.     The function you call to start the stack
  54.  
  55.     @param
  56.     None
  57.  
  58.     @return
  59.     Nothing
  60.  
  61. ]]
  62. function runTaskStack()
  63.     stackRunning = true
  64.     local _eventData = {coroutine.yield()}
  65.     while stackRunning do
  66.         for k,v in pairs(_tasks) do
  67.             if type(v) == "coroutine" then
  68.                 _runningTask = v
  69.                 if coroutine.status(_runningTask) == dead then _tasks[_runningTask] = nil end
  70.                 if _filter[_runningTask] == nil or _filter[_runningTask] == _eventData[1] or _eventData[1] == "terminate" then
  71.                     coroutine.resume(_runningTask,unpack(_eventData))
  72.                 end
  73.             end
  74.         end
  75.         _eventData = {coroutine.yield()}
  76.     end
  77. end
  78.  
  79. --[[
  80.  
  81.     @description
  82.     The function you call to stop the stack
  83.  
  84.     @param
  85.     None
  86.  
  87.     @return
  88.     Nothing
  89.  
  90. ]]
  91. function stopTaskStack()
  92.     stackRunning = false
  93. end
  94.  
  95. --[[
  96.  
  97.     @description
  98.     This function you call to get the ID of the current running task
  99.  
  100.     @param
  101.     None
  102.  
  103.     @return
  104.     The current running task ID
  105.  
  106. ]]
  107.  
  108. --# COMMENT: This function could be used for a program to "terminate" itself
  109. function getRunningTask()
  110.     return _runningTask
  111. end
  112.  
  113. addTask(function()
  114.  
  115.         while true do
  116.             local evt = {coroutine.yield("stopTask")}
  117.             stopTaskStack()
  118.         end
  119.  
  120.     end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement