Guest User

Untitled

a guest
May 12th, 2017
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.25 KB | None | 0 0
  1. --[[
  2.     Nature Thread Manager Version 0.01a - Under the GNU GPL v3 license
  3.     Nature Copyright (C) 2015 - Redall
  4. ]]--
  5.  
  6. Nature.Thread = {};
  7. Nature.Thread.List = {};
  8.  
  9. local ThreadMode = false;
  10. local EventTable = {};
  11. local LastID = 0;
  12.  
  13. function Nature.Thread.Switch()
  14.     if ThreadMode then
  15.         error("Nature.Thread.Switch: Cannot switch because Thread Mode is already activated");
  16.     end
  17.  
  18.     Nature.Logger.LogLine("THRD", "Switching to ThreadMode...");
  19.     ThreadMode = true;
  20.  
  21.     while #Nature.Thread.List > 0 do
  22.         for Index = 1, #Nature.Thread.List do
  23.             if Nature.Thread.List[Index] ~= nil then
  24.                 if Index > #Nature.Thread.List then
  25.                     Index = 1;
  26.                 end
  27.  
  28.                 local Event = Nature.Thread.GetEvent() or {};
  29.                 local CanResume, Parameter = coroutine.resume(Nature.Thread.List[Index].Thread, unpack(Event));
  30.  
  31.                 write("Thread #" .. Index .. " is running... ");
  32.                 print(tostring(coroutine.status(Nature.Thread.List[Index].Thread)));
  33.  
  34.                 if CanResume == false then
  35.                     Nature.Logger.LogLine("THRD", "Error while running thread ID " .. Index .. ": " .. Parameter);
  36.                     table.remove(Nature.Thread.List, Index);
  37.                     next();
  38.                 elseif coroutine.status(Nature.Thread.List[Index].Thread) == "dead" then
  39.                     table.remove(Nature.Thread.List, Index);
  40.                     next();
  41.                 end
  42.             end
  43.         end
  44.     end
  45. end
  46.  
  47. function Nature.Thread.IsThreadMode()
  48.     return ThreadMode;
  49. end
  50.  
  51. function Nature.Thread.QueueEvent(Event, ...)
  52.     if Event == nil then
  53.         return;
  54.     end
  55.  
  56.     table.insert(EventTable, {Event, ...});
  57. end
  58.  
  59. function Nature.Thread.GetEvent()
  60.     if #EventTable < 1 then
  61.         return nil;
  62.     end
  63.  
  64.     local Event = EventTable[#EventTable];
  65.     table.remove(EventTable, #EventTable);
  66.     return Event;
  67. end
  68.  
  69. local InternalQueueEvent = os.queueEvent;
  70.  
  71. function os.queueEvent(...)
  72.     if ThreadMode then
  73.         Nature.Thread.QueueEvent(...);
  74.     else
  75.         InternalQueueEvent(...);
  76.     end
  77. end
  78.  
  79. function Nature.Thread.NewThread(Function)
  80.     local Thread = coroutine.create(Function);
  81.     local ThreadInfo = {
  82.         ["Thread"] = Thread,
  83.         ["ID"] = LastID,
  84.     }
  85.  
  86.     table.insert(Nature.Thread.List, ThreadInfo);
  87.     LastID = LastID + 1;
  88. end
  89.  
  90. function Nature.Thread.KillThread(ID)
  91.     if Nature.Thread.List[ID] ~= nil and table(Nature.Thread.List[ID]) == "table" then
  92.         table.remove(Nature.Thread.List, ID);
  93.     end
  94. end
Add Comment
Please, Sign In to add comment