Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[
- Nature Thread Manager Version 0.01a - Under the GNU GPL v3 license
- Nature Copyright (C) 2015 - Redall
- ]]--
- Nature.Thread = {};
- Nature.Thread.List = {};
- local ThreadMode = false;
- local EventTable = {};
- local LastID = 0;
- function Nature.Thread.Switch()
- if ThreadMode then
- error("Nature.Thread.Switch: Cannot switch because Thread Mode is already activated");
- end
- Nature.Logger.LogLine("THRD", "Switching to ThreadMode...");
- ThreadMode = true;
- while #Nature.Thread.List > 0 do
- for Index = 1, #Nature.Thread.List do
- if Nature.Thread.List[Index] ~= nil then
- if Index > #Nature.Thread.List then
- Index = 1;
- end
- local Event = Nature.Thread.GetEvent() or {};
- local CanResume, Parameter = coroutine.resume(Nature.Thread.List[Index].Thread, unpack(Event));
- write("Thread #" .. Index .. " is running... ");
- print(tostring(coroutine.status(Nature.Thread.List[Index].Thread)));
- if CanResume == false then
- Nature.Logger.LogLine("THRD", "Error while running thread ID " .. Index .. ": " .. Parameter);
- table.remove(Nature.Thread.List, Index);
- next();
- elseif coroutine.status(Nature.Thread.List[Index].Thread) == "dead" then
- table.remove(Nature.Thread.List, Index);
- next();
- end
- end
- end
- end
- end
- function Nature.Thread.IsThreadMode()
- return ThreadMode;
- end
- function Nature.Thread.QueueEvent(Event, ...)
- if Event == nil then
- return;
- end
- table.insert(EventTable, {Event, ...});
- end
- function Nature.Thread.GetEvent()
- if #EventTable < 1 then
- return nil;
- end
- local Event = EventTable[#EventTable];
- table.remove(EventTable, #EventTable);
- return Event;
- end
- local InternalQueueEvent = os.queueEvent;
- function os.queueEvent(...)
- if ThreadMode then
- Nature.Thread.QueueEvent(...);
- else
- InternalQueueEvent(...);
- end
- end
- function Nature.Thread.NewThread(Function)
- local Thread = coroutine.create(Function);
- local ThreadInfo = {
- ["Thread"] = Thread,
- ["ID"] = LastID,
- }
- table.insert(Nature.Thread.List, ThreadInfo);
- LastID = LastID + 1;
- end
- function Nature.Thread.KillThread(ID)
- if Nature.Thread.List[ID] ~= nil and table(Nature.Thread.List[ID]) == "table" then
- table.remove(Nature.Thread.List, ID);
- end
- end
Add Comment
Please, Sign In to add comment