Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion example.lua
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,6 @@ scheduler.resume(coroutine.create(function()
end
end))

while scheduler.run() do end
local socket = require("socket")

while scheduler.run() do socket.sleep(0.2) end
18 changes: 10 additions & 8 deletions scheduler.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ local coroutine = coroutine
local table = table
local os = os

local socket = require("socket")

local scheduler; scheduler = {
tasks = {}; --// Registered tasks
lastID = 0; --// Last registered task ID
Expand All @@ -21,7 +23,7 @@ local scheduler; scheduler = {
c = c; --// Coroutine
id = id;
args = {...};
start = os.clock();
start = socket.gettime();
performance = {
calls = 0;
cpuTimeSpent = 0;
Expand All @@ -30,7 +32,7 @@ local scheduler; scheduler = {
scheduler.tasks[id] = task

--// Store time before resuming task to measure performance
local startTime = os.clock()
local startTime = socket.gettime()

--// Resume task
table.insert(scheduler.runningIDs, id)
Expand All @@ -39,7 +41,7 @@ local scheduler; scheduler = {

--// Performance info
task.performance.calls = 1
task.performance.cpuTimeSpent = os.clock() - startTime
task.performance.cpuTimeSpent = socket.gettime() - startTime

return id
end;
Expand All @@ -49,7 +51,7 @@ local scheduler; scheduler = {
local id = table.remove(scheduler.runningIDs)

--// Insert task ID into sleeping table
table.insert(scheduler.sleeping, {wakeupTime = os.clock() + sleepTime; id = id;})
table.insert(scheduler.sleeping, {wakeupTime = socket.gettime() + sleepTime; id = id;})

return id
end;
Expand All @@ -67,7 +69,7 @@ local scheduler; scheduler = {
local taskSleepInfo = scheduler.sleeping[i + sleepingOffset]

--// Check if it's time to wake up the task
if os.clock() >= taskSleepInfo.wakeupTime then
if socket.gettime() >= taskSleepInfo.wakeupTime then
local task = scheduler.tasks[taskSleepInfo.id]

--// Make sure task exists
Expand All @@ -76,7 +78,7 @@ local scheduler; scheduler = {
end

--// Store time before resuming task to measure performance
local runStart = os.clock()
local runStart = socket.gettime()

--// Remove task from sleeping table
table.remove(scheduler.sleeping, i + sleepingOffset)
Expand All @@ -87,7 +89,7 @@ local scheduler; scheduler = {
local success, err = coroutine.resume(task.c, unpack(task.args))

--// Performance
local runTook = os.clock() - runStart
local runTook = socket.gettime() - runStart

task.performance.calls = task.performance.calls + 1
task.performance.cpuTimeSpent = task.performance.cpuTimeSpent + runTook
Expand All @@ -114,4 +116,4 @@ local scheduler; scheduler = {
end;
}

return scheduler
return scheduler