-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathasync.lua
457 lines (387 loc) · 13.5 KB
/
async.lua
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
local function loadClass()
---------
-- Start of slither.lua dependency
---------
local _LICENSE = -- zlib / libpng
[[
Copyright (c) 2011-2014 Bart van Strien
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source
distribution.
]]
local class =
{
_VERSION = "Slither 20140904",
-- I have no better versioning scheme, deal with it
_DESCRIPTION = "Slither is a pythonic class library for lua",
_URL = "http://bitbucket.org/bartbes/slither",
_LICENSE = _LICENSE,
}
local function stringtotable(path)
local t = _G
local name
for part in path:gmatch("[^%.]+") do
t = name and t[name] or t
name = part
end
return t, name
end
local function class_generator(name, b, t)
local parents = {}
for _, v in ipairs(b) do
parents[v] = true
for _, v in ipairs(v.__parents__) do
parents[v] = true
end
end
local temp = { __parents__ = {} }
for i, v in pairs(parents) do
table.insert(temp.__parents__, i)
end
local class = setmetatable(temp, {
__index = function(self, key)
if key == "__class__" then return temp end
if key == "__name__" then return name end
if t[key] ~= nil then return t[key] end
for i, v in ipairs(b) do
if v[key] ~= nil then return v[key] end
end
if tostring(key):match("^__.+__$") then return end
if self.__getattr__ then
return self:__getattr__(key)
end
end,
__newindex = function(self, key, value)
t[key] = value
end,
allocate = function(instance)
local smt = getmetatable(temp)
local mt = {__index = smt.__index}
function mt:__newindex(key, value)
if self.__setattr__ then
return self:__setattr__(key, value)
else
return rawset(self, key, value)
end
end
if temp.__cmp__ then
if not smt.eq or not smt.lt then
function smt.eq(a, b)
return a.__cmp__(a, b) == 0
end
function smt.lt(a, b)
return a.__cmp__(a, b) < 0
end
end
mt.__eq = smt.eq
mt.__lt = smt.lt
end
for i, v in pairs{
__call__ = "__call", __len__ = "__len",
__add__ = "__add", __sub__ = "__sub",
__mul__ = "__mul", __div__ = "__div",
__mod__ = "__mod", __pow__ = "__pow",
__neg__ = "__unm", __concat__ = "__concat",
__str__ = "__tostring",
} do
if temp[i] then mt[v] = temp[i] end
end
return setmetatable(instance or {}, mt)
end,
__call = function(self, ...)
local instance = getmetatable(self).allocate()
if instance.__init__ then instance:__init__(...) end
return instance
end
})
for i, v in ipairs(t.__attributes__ or {}) do
class = v(class) or class
end
return class
end
local function inheritance_handler(set, name, ...)
local args = {...}
for i = 1, select("#", ...) do
if args[i] == nil then
error("nil passed to class, check the parents")
end
end
local t = nil
if #args == 1 and type(args[1]) == "table" and not args[1].__class__ then
t = args[1]
args = {}
end
for i, v in ipairs(args) do
if type(v) == "string" then
local t, name = stringtotable(v)
args[i] = t[name]
end
end
local func = function(t)
local class = class_generator(name, args, t)
if set then
local root_table, name = stringtotable(name)
root_table[name] = class
end
return class
end
if t then
return func(t)
else
return func
end
end
function class.private(name)
return function(...)
return inheritance_handler(false, name, ...)
end
end
class = setmetatable(class, {
__call = function(self, name)
return function(...)
return inheritance_handler(true, name, ...)
end
end,
})
function class.issubclass(class, parents)
if parents.__class__ then parents = {parents} end
for i, v in ipairs(parents) do
local found = true
if v ~= class then
found = false
for _, p in ipairs(class.__parents__) do
if v == p then
found = true
break
end
end
end
if not found then return false end
end
return true
end
function class.isinstance(obj, parents)
return type(obj) == "table" and obj.__class__ and class.issubclass(obj.__class__, parents)
end
-- Export a Class Commons interface
-- to allow interoperability between
-- class libraries.
-- See https://github.com/bartbes/Class-Commons
--
-- NOTE: Implicitly global, as per specification, unfortunately there's no nice
-- way to both provide this extra interface, and use locals.
if common_class ~= false then
common = {}
function common.class(name, prototype, superclass)
prototype.__init__ = prototype.init
return class_generator(name, {superclass}, prototype)
end
function common.instance(class, ...)
return class(...)
end
end
---------
-- End of slither.lua dependency
---------
return class;
end
local class = loadClass();
--- GTA:MTA Lua async thread scheduler.
-- @author Inlife
-- @license MIT
-- @url https://github.com/Inlife/mta-lua-async
-- @dependency slither.lua https://bitbucket.org/bartbes/slither
class "_Async" {
-- Constructor mehtod
-- Starts timer to manage scheduler
-- @access public
-- @usage local asyncmanager = async();
__init__ = function(self)
self.threads = {};
self.resting = 50; -- in ms (resting time)
self.maxtime = 200; -- in ms (max thread iteration time)
self.current = 0; -- starting frame (resting)
self.state = "suspended"; -- current scheduler executor state
self.debug = false;
self.priority = {
low = {500, 50}, -- better fps
normal = {200, 200}, -- medium
high = {50, 500} -- better perfomance
};
self:setPriority("normal");
end,
-- Switch scheduler state
-- @access private
-- @param boolean [istimer] Identifies whether or not
-- switcher was called from main loop
switch = function(self, istimer)
self.state = "running";
if (self.current + 1 <= #self.threads) then
self.current = self.current + 1;
self:execute(self.current);
else
self.current = 0;
if (#self.threads <= 0) then
self.state = "suspended";
return;
end
-- setTimer(function theFunction, int timeInterval, int timesToExecute)
-- (GTA:MTA server scripting function)
-- For other environments use alternatives.
setTimer(function()
self:switch();
end, self.resting, 1);
end
end,
-- Managing thread (resuming, removing)
-- In case of "dead" thread, removing, and skipping to the next (recursive)
-- @access private
-- @param int id Thread id (in table async.threads)
execute = function(self, id)
local thread = self.threads[id];
if (thread == nil or coroutine.status(thread) == "dead") then
table.remove(self.threads, id);
self:switch();
else
coroutine.resume(thread);
self:switch();
end
end,
-- Adding thread
-- @access private
-- @param function func Function to operate with
add = function(self, func)
local thread = coroutine.create(func);
table.insert(self.threads, thread);
end,
-- Set priority for executor
-- Use before you call 'iterate' or 'foreach'
-- @access public
-- @param string|int param1 "low"|"normal"|"high" or number to set 'resting' time
-- @param int|void param2 number to set 'maxtime' of thread
-- @usage async:setPriority("normal");
-- @usage async:setPriority(50, 200);
setPriority = function(self, param1, param2)
if (type(param1) == "string") then
if (self.priority[param1] ~= nil) then
self.resting = self.priority[param1][1];
self.maxtime = self.priority[param1][2];
end
else
self.resting = param1;
self.maxtime = param2;
end
end,
-- Set debug mode enabled/disabled
-- @access public
-- @param boolean value true - enabled, false - disabled
-- @usage async:setDebug(true);
setDebug = function(self, value)
self.debug = value;
end,
-- Iterate on interval (for cycle)
-- @access public
-- @param int from Iterate from
-- @param int to Iterate to
-- @param function func Iterate using func
-- Function func params:
-- @param int [i] Iteration index
-- @param function [callback] Callback function, called when execution finished
-- Usage:
-- @usage async:iterate(1, 10000, function(i)
-- print(i);
-- end);
iterate = function(self, from, to, func, callback)
self:add(function()
local a = getTickCount();
local lastresume = getTickCount();
for i = from, to do
func(i);
-- int getTickCount()
-- (GTA:MTA server scripting function)
-- For other environments use alternatives.
if getTickCount() > lastresume + self.maxtime then
coroutine.yield()
lastresume = getTickCount()
end
end
if (self.debug) then
print("[DEBUG]Async iterate: " .. (getTickCount() - a) .. "ms");
end
if (callback) then
callback();
end
end);
self:switch();
end,
-- Iterate over array (foreach cycle)
-- @access public
-- @param table array Input array
-- @param function func Iterate using func
-- Function func params:
-- @param int [v] Iteration value
-- @param int [k] Iteration key
-- @param function [callback] Callback function, called when execution finished
-- Usage:
-- @usage async:foreach(vehicles, function(vehicle, id)
-- print(vehicle.title);
-- end);
foreach = function(self, array, func, callback)
self:add(function()
local a = getTickCount();
local lastresume = getTickCount();
for k,v in ipairs(array) do
func(v,k);
-- int getTickCount()
-- (GTA:MTA server scripting function)
-- For other environments use alternatives.
if getTickCount() > lastresume + self.maxtime then
coroutine.yield()
lastresume = getTickCount()
end
end
if (self.debug) then
print("[DEBUG]Async foreach: " .. (getTickCount() - a) .. "ms");
end
if (callback) then
callback();
end
end);
self:switch();
end,
}
-- Async Singleton wrapper
Async = {
instance = nil,
};
-- After first call, creates an instance and stores it
local function getInstance()
if Async.instance == nil then
Async.instance = _Async();
end
return Async.instance;
end
-- proxy methods for public members
function Async:setDebug(...)
getInstance():setDebug(...);
end
function Async:setPriority(...)
getInstance():setPriority(...);
end
function Async:iterate(...)
getInstance():iterate(...);
end
function Async:foreach(...)
getInstance():foreach(...);
end