blob: e526efa96135658ef7ed52128b130349c7d0e8a9 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
|
pending = {}
function schedule(time, action)
pending[#pending+1] = {
time = time,
action = action
}
sort_by_time(pending)
end
function sort_by_time(array)
table.sort(array, function(e1, e2) return e1.time < e2.time end)
end
function wait(seconds)
coroutine.yield(seconds)
end
function run()
while #pending > 0 do
while os.clock() < pending[1].time do end -- busy-wait
local item = remove_first(pending)
local _, seconds = coroutine.resume(item.action)
if seconds then
later = os.clock() + seconds
schedule(later, item.action)
end
end
end
function remove_first(array)
result = array[1]
array[1] = array[#array]
array[#array] = nil
return result
end
|