This repository was archived by the owner on Nov 20, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimulua.lua
More file actions
174 lines (152 loc) · 5.24 KB
/
simulua.lua
File metadata and controls
174 lines (152 loc) · 5.24 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
171
172
173
174
-- Simulua: discrete-event simulation, based on Simula
-- Version: 0.1
-- Check license at the bottom of the file
-- $Id: simulua.lua,v 1.1.1.1 2008/08/19 23:36:38 carvalho Exp $
local setmetatable, getmetatable = setmetatable, getmetatable
local assert, error, type = assert, error, type
local yield, create, resume = coroutine.yield, coroutine.create, coroutine.resume
local heap = require "binomial"
module "simulua"
-- internals
local procmt = {} procmt.__metatable = procmt -- protected metatable
local thread = setmetatable({}, {__mode = "k"}) -- thread cache
local events -- event list
local isidle -- idle event set
-- simulation globals
local _time, _current -- current time and process
local main -- main thread
local function cancelprocess (proc)
if events:remove(proc) then -- removed?
isidle[proc] = true -- add to idle event set
end
end
local function checkprocess (proc)
assert(getmetatable(proc) == procmt,
"process expected, got " .. type(proc))
return proc
end
local function scheduler () -- generator
return create(function()
while true do
if events:isempty() then break end
_time, _current = events:min()
local active, op, p, d, after = resume(thread[_current])
if not active then yield(op) end -- propagate error
if op ~= nil then
if op == "hold" then
if type(p) ~= "number" or p < 0 then p = 0 end
events:change(_current, _time + p) -- with priority
elseif op == "cancel" then
cancelprocess(checkprocess(p))
elseif op == "activate" then
-- set delay time
local delay
if d == nil then delay = 0
elseif type(d) == "number" then
delay = d >= 0 and d or 0
else -- process
d = checkprocess(d)
if not isidle[d] then
d = events:get(d) -- d's time
if d ~= nil then -- in event list?
delay = d - _time -- relative delay
end
end
end
p = checkprocess(p)
if delay == nil then
cancelprocess(p)
else
if isidle[p] or events:get(p) == nil then
events:insert(_time + delay, p, after)
isidle[p] = nil -- remove from idle set
else -- reactivate
events:change(p, _time + delay, after)
end
end
else -- failsafe
yield("unknown operation: " .. op)
end
else
if _current == main then break end -- stop simulation
cancelprocess(_current) -- passivate
end
end
end)
end
-- ======= simulation =======
function process (task, att)
local p = setmetatable(att or {}, procmt)
thread[p] = create(task)
return p
end
-- query
function current () return _current end
function time (proc)
if proc == nil then return _time end
return events:get(checkprocess(proc))
end
function idle (proc)
local p = checkprocess(proc)
return isidle[p] or events:get(p) == nil -- idle or not scheduled yet?
end
-- actions
function hold (delay) yield("hold", delay) end
function cancel (proc) yield("cancel", proc) end
function passivate () yield("cancel", _current) end
function activate (proc, d, after)
yield("activate", proc, d, after)
end
function wait (res, ...)
assert(res ~= nil, "invalid resource")
assert(type(res.into) == "function",
"resource has invalid `into' method")
res:into(_current, ...)
yield("cancel", _current) -- passivate
end
-- simulation control
function start (task)
events, isidle, main = heap(), {}, process(task)
events:insert(0, main)
local active, msg = resume(scheduler())
assert(active, msg)
if msg ~= nil then error(msg) end
end
function stop () yield("cancel", main) end
-- ======= accumulator =======
local _acc = {
__index = { -- methods
update = function(a, v, t)
local time = t or _time
assert(v ~= nil and type(v) == "number", "invalid value to update")
assert(type(time) == "number", "invalid time to update")
local last = a.last
a.mean = (a.mean * last + (time - last) * v) / time
a.last = time
end
}
}
_acc.__metatable = _acc -- protect
function accumulator ()
return setmetatable({last = 0, mean = 0}, _acc)
end
--[[
Copyright (c) 2008 Luis Carvalho
Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation files
(the "Software"), to deal in the Software without restriction,
including without limitation the rights to use, copy, modify,
merge, publish, distribute, sublicense, and/or sell copies of the
Software, and to permit persons to whom the Software is furnished
to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
--]]