Skip to content

Commit a8c4e6e

Browse files
committed
finish support System.Threads.Timer
1 parent be69019 commit a8c4e6e

9 files changed

Lines changed: 277 additions & 192 deletions

File tree

‎.editorconfig‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ root = true
88
indent_style = space
99

1010
# Code files
11-
[*.{cs,csx,vb,vbx}]
11+
[*.{cs,csx,vb,vbx,lua}]
1212
indent_size = 2
1313
insert_final_newline = true
1414
charset = utf-8

‎CSharp.lua/CoreSystem.Lua/CoreSystem/DateTime.lua‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,7 @@ DateTime.BaseUtcOffset = TimeSpan(timeZoneTicks)
320320

321321
local time = System.config.time or ostime
322322
System.time = time
323+
System.currentTimeMillis = function () return trunc(time() * 1000) end
323324

324325
function DateTime.getUtcNow()
325326
local seconds = time()

‎CSharp.lua/CoreSystem.Lua/CoreSystem/IO/File.lua‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ local io = io
2222
local open = io.open
2323
local tinsert = table.insert
2424

25-
local IOException = System.define("System.IOException", {
25+
local IOException = System.define("System.IO.IOException", {
2626
__tostring = System.Exception.ToString,
2727
__inherits__ = { System.Exception },
2828
__ctor__ = function(this, message, innerException)

‎CSharp.lua/CoreSystem.Lua/CoreSystem/Text/StringBuilder.lua‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,4 +155,4 @@ end
155155
StringBuilder.ToString = tconcat
156156
StringBuilder.__tostring = StringBuilder.ToString
157157

158-
System.define("System.Text.StringBuilder", StringBuilder)
158+
System.define("System.StringBuilder", StringBuilder)

‎CSharp.lua/CoreSystem.Lua/CoreSystem/Threads/Task.lua‎

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ local throw = System.throw
1919
local try = System.try
2020
local trunc = System.trunc
2121
local post = System.post
22+
local addTimer = System.addTimer
23+
local removeTimer = System.removeTimer
2224
local Exception = System.Exception
2325
local NotImplementedException = System.NotImplementedException
2426
local ArgumentOutOfRangeException = System.ArgumentOutOfRangeException
@@ -255,15 +257,7 @@ local function newWaitingTask(isVoid)
255257
return setmetatable({ status = TaskStatusWaitingForActivation, isVoid = isVoid }, Task)
256258
end
257259

258-
local config = System.config
259-
local setTimeout = config.setTimeout
260-
local clearTimeout = config.clearTimeout
261-
262260
function Task.Delay(delay, cancellationToken)
263-
if not setTimeout or not clearTimeout then
264-
throw(NotImplementedException("System.config.setTimeout or clearTimeout is not set"))
265-
end
266-
267261
if type(delay) == "table" then
268262
delay = trunc(delay:getTotalMilliseconds())
269263
if delay < -1 or delay > 2147483647 then
@@ -280,24 +274,24 @@ function Task.Delay(delay, cancellationToken)
280274
end
281275

282276
local t = newWaitingTask()
283-
local timeoutId, registration
277+
local timerId, registration
284278

285279
if cancellationToken and cancellationToken:getCanBeCanceled() then
286280
registration = cancellationToken.source:register(function ()
287281
local success = trySetCanceled(t, cancellationToken)
288-
if success and timeoutId then
289-
clearTimeout(timeoutId)
282+
if success and timerId then
283+
removeTimer(timerId)
290284
end
291285
end)
292286
end
293287

294288
if delay ~= -1 then
295-
timeoutId = setTimeout(delay, function ()
289+
timerId = addTimer(function ()
296290
local success = trySetResult(t)
297291
if success and registration then
298292
registration:Dispose()
299293
end
300-
end)
294+
end, delay)
301295
end
302296

303297
return t
Lines changed: 61 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
--[[
1+
--[[
22
Copyright 2017 YANG Huan (sy.yanghuan@gmail.com).
33
44
Licensed under the Apache License, Version 2.0 (the "License");
@@ -17,6 +17,8 @@ limitations under the License.
1717
local System = System
1818
local throw = System.throw
1919
local trunc = System.trunc
20+
local post = System.post
21+
local addTimer = System.addTimer
2022
local Exception = System.Exception
2123
local ArgumentNullException = System.ArgumentNullException
2224
local ArgumentOutOfRangeException = System.ArgumentOutOfRangeException
@@ -39,70 +41,16 @@ local ThreadStateException = System.define("System.ThreadStateException", {
3941
end
4042
})
4143

42-
local Thread = {}
4344
local nextThreadId = 1
4445
local threadYield = {}
46+
local currentThread
4547

4648
local function getThreadId()
4749
local id = nextThreadId
4850
nextThreadId = nextThreadId + 1
4951
return id
5052
end
5153

52-
local mainThread = setmetatable({
53-
id = getThreadId(),
54-
}, Thread)
55-
local currentThread = mainThread
56-
57-
function Thread.getCurrentThread()
58-
return currentThread
59-
end
60-
61-
function Thread.__ctor__(this, start)
62-
if start == nil then throw(ArgumentNullException("start")) end
63-
this.start = start
64-
end
65-
66-
function Thread.getIsAlive(this)
67-
local co = this.co
68-
return co and cstatus(co) ~= "dead"
69-
end
70-
71-
function Thread.ManagedThreadId(this)
72-
local id = this.id
73-
if not id then
74-
id = getThreadId()
75-
this.id = id
76-
end
77-
return id
78-
end
79-
80-
function Thread.Sleep(timeout)
81-
if type(timeout) == "table" then
82-
timeout = trunc(timeout:getTotalMilliseconds())
83-
if timeout < -1 or timeout > 2147483647 then
84-
throw(ArgumentOutOfRangeException("timeout"))
85-
end
86-
end
87-
if currentThread == mainThread then
88-
throw(NotSupportedException("mainThread not support"))
89-
end
90-
cyield(timeout)
91-
end
92-
93-
function Thread.Yield()
94-
if currentThread == mainThread then
95-
return false
96-
end
97-
cyield(threadYield)
98-
return true
99-
end
100-
101-
Thread.IsBackground = false
102-
Thread.IsThreadPoolThread = false
103-
Thread.Priority = 2
104-
Thread.ApartmentState = 2
105-
10654
local function run(t, obj)
10755
post(function ()
10856
currentThread = t
@@ -113,7 +61,9 @@ local function run(t, obj)
11361
run(t)
11462
elseif v ~= nil then
11563
if v ~= -1 then
116-
64+
addTimer(function ()
65+
run(t)
66+
end, v)
11767
end
11868
else
11969
t.co = false
@@ -123,13 +73,58 @@ local function run(t, obj)
12373
end
12474
end)
12575
end
126-
127-
function Thread.start(this, parameter)
128-
if this.co ~= nil then throw(ThreadStateException()) end
129-
local co = ccreate(this.start)
130-
this.co = co
131-
this.start = nil
132-
run(this, parameter)
133-
end
13476

135-
System.define("System.Thread", Thread)
77+
local Thread = System.define("System.Thread", {
78+
IsBackground = false,
79+
IsThreadPoolThread = false,
80+
Priority = 2,
81+
ApartmentState = 2,
82+
getCurrentThread = function ()
83+
return currentThread
84+
end,
85+
__ctor__ = function (this, start)
86+
if start == nil then throw(ArgumentNullException("start")) end
87+
this.start = start
88+
end,
89+
getIsAlive = function (this)
90+
local co = this.co
91+
return co and cstatus(co) ~= "dead"
92+
end,
93+
ManagedThreadId = function (this)
94+
local id = this.id
95+
if not id then
96+
id = getThreadId()
97+
this.id = id
98+
end
99+
return id
100+
end,
101+
Sleep = function (timeout)
102+
if currentThread == mainThread then
103+
throw(NotSupportedException("mainThread not support"))
104+
end
105+
if type(timeout) == "table" then
106+
timeout = trunc(timeout:getTotalMilliseconds())
107+
if timeout < -1 or timeout > 2147483647 then
108+
throw(ArgumentOutOfRangeException("timeout"))
109+
end
110+
end
111+
cyield(timeout)
112+
end,
113+
Yield = function ()
114+
if currentThread == mainThread then
115+
return false
116+
end
117+
cyield(threadYield)
118+
return true
119+
end,
120+
Start = function (this, parameter)
121+
if this.co ~= nil then throw(ThreadStateException()) end
122+
local co = ccreate(this.start)
123+
this.co = co
124+
this.start = nil
125+
run(this, parameter)
126+
end
127+
})
128+
129+
local mainThread = setmetatable({ id = getThreadId() }, Thread)
130+
currentThread = mainThread

0 commit comments

Comments
 (0)