forked from rjpcomputing/luaforwindows
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIOScheduler.lua
More file actions
executable file
·170 lines (153 loc) · 7.17 KB
/
IOScheduler.lua
File metadata and controls
executable file
·170 lines (153 loc) · 7.17 KB
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
--------------------------------------------------------------------------------
---------------------- ## ##### ##### ###### -----------------------
---------------------- ## ## ## ## ## ## ## -----------------------
---------------------- ## ## ## ## ## ###### -----------------------
---------------------- ## ## ## ## ## ## -----------------------
---------------------- ###### ##### ##### ## -----------------------
---------------------- -----------------------
----------------------- Lua Object-Oriented Programming ------------------------
--------------------------------------------------------------------------------
-- Project: LOOP Class Library --
-- Release: 2.3 beta --
-- Title : Cooperative Threads Scheduler with Integrated I/O --
-- Author : Renato Maia <[email protected]> --
--------------------------------------------------------------------------------
--[[VERBOSE]] local rawget = rawget
--[[VERBOSE]] local tostring = tostring
local ipairs = ipairs
local getmetatable = getmetatable
local math = require "math"
local oo = require "loop.simple"
local MapWithArrayOfKeys = require "loop.collection.MapWithArrayOfKeys"
local Scheduler = require "loop.thread.Scheduler"
module "loop.thread.IOScheduler"
oo.class(_M, Scheduler)
--------------------------------------------------------------------------------
-- Initialization Code ---------------------------------------------------------
--------------------------------------------------------------------------------
function __init(class, self)
self = Scheduler.__init(class, self)
self.reading = MapWithArrayOfKeys()
self.writing = MapWithArrayOfKeys()
return self
end
__init(getmetatable(_M), _M)
--------------------------------------------------------------------------------
-- Internal Functions ----------------------------------------------------------
--------------------------------------------------------------------------------
function signalall(self, timeout) --[[VERBOSE]] local verbose = self.verbose
if timeout then timeout = math.max(timeout - self:time(), 0) end
local reading, writing = self.reading, self.writing
if #reading > 0 or #writing > 0 then --[[VERBOSE]] verbose:scheduler(true, "signaling blocked threads for ",timeout," seconds")
local running = self.running
local readok, writeok = self.select(reading, writing, timeout)
local index = 1
while index <= #reading do
local channel = reading[index]
if readok[channel] then --[[VERBOSE]] verbose:threads("unblocking reading ",reading[channel])
running:enqueue(reading[channel])
reading:removeat(index)
else
index = index + 1
end
end
index = 1
while index <= #writing do
local channel = writing[index]
if writeok[channel] then --[[VERBOSE]] verbose:threads("unblocking writing ", writing[channel])
running:enqueue(writing[channel])
writing:removeat(index)
else
index = index + 1
end
end --[[VERBOSE]] verbose:scheduler(false, "blocked threads signaled")
return true
elseif timeout and timeout > 0 then --[[VERBOSE]] verbose:scheduler("no threads blocked, sleeping for ",timeout," seconds")
self.sleep(timeout)
end
return false
end
--------------------------------------------------------------------------------
-- Customizable Behavior -------------------------------------------------------
--------------------------------------------------------------------------------
idle = signalall
--------------------------------------------------------------------------------
-- Exported API ----------------------------------------------------------------
--------------------------------------------------------------------------------
function register(self, routine, previous)
local reading, writing = self.reading, self.writing
for _, channel in ipairs(reading) do
if reading[channel] == routine then return end
end
for _, channel in ipairs(writing) do
if writing[channel] == routine then return end
end
return Scheduler.register(self, routine, previous)
end
local function handleremoved(self, routine, removed, ...)
local reading, writing = self.reading, self.writing
local index = 1
while index <= #reading do
local channel = reading[index]
if reading[channel] == routine then
reading:removeat(index)
removed = routine
else
index = index + 1
end
end
index = 1
while index <= #writing do
local channel = writing[index]
if writing[channel] == routine then
writing:removeat(index)
removed = routine
else
index = index + 1
end
end
return removed, ...
end
function remove(self, routine)
return handleremoved(self, routine, Scheduler.remove(self, routine))
end
--------------------------------------------------------------------------------
-- Control Functions -----------------------------------------------------------
--------------------------------------------------------------------------------
function step(self, ...) --[[VERBOSE]] local verbose = self.verbose; verbose:scheduler(true, "performing scheduling step")
local signaled = self:signalall(0)
local wokenup = self:wakeupall()
local resumed = self:resumeall(nil, ...) --[[VERBOSE]] verbose:scheduler(false, "scheduling step performed")
return signaled or wokenup or resumed
end
--------------------------------------------------------------------------------
-- Verbose Support -------------------------------------------------------------
--------------------------------------------------------------------------------
--[[VERBOSE]] local oldfunc = verbose.custom.threads
--[[VERBOSE]] function verbose.custom:threads(...)
--[[VERBOSE]] local viewer = self.viewer
--[[VERBOSE]] local output = self.viewer.output
--[[VERBOSE]]
--[[VERBOSE]] oldfunc(self, ...)
--[[VERBOSE]]
--[[VERBOSE]] local scheduler = rawget(self, "schedulerdetails")
--[[VERBOSE]] if scheduler then
--[[VERBOSE]] local newline = "\n"..viewer.prefix..viewer.indentation
--[[VERBOSE]]
--[[VERBOSE]] output:write(newline)
--[[VERBOSE]] output:write("Reading:")
--[[VERBOSE]] for _, current in ipairs(scheduler.reading) do
--[[VERBOSE]] current = scheduler.reading[current]
--[[VERBOSE]] output:write(" ")
--[[VERBOSE]] output:write(tostring(self.labels[current]))
--[[VERBOSE]] end
--[[VERBOSE]]
--[[VERBOSE]] output:write(newline)
--[[VERBOSE]] output:write("Writing:")
--[[VERBOSE]] for _, current in ipairs(scheduler.writing) do
--[[VERBOSE]] current = scheduler.writing[current]
--[[VERBOSE]] output:write(" ")
--[[VERBOSE]] output:write(tostring(self.labels[current]))
--[[VERBOSE]] end
--[[VERBOSE]] end
--[[VERBOSE]] end