1- --[[
1+ --[[
22Copyright 2017 YANG Huan (sy.yanghuan@gmail.com).
33
44Licensed under the Apache License, Version 2.0 (the "License");
@@ -17,6 +17,8 @@ limitations under the License.
1717local System = System
1818local throw = System .throw
1919local trunc = System .trunc
20+ local post = System .post
21+ local addTimer = System .addTimer
2022local Exception = System .Exception
2123local ArgumentNullException = System .ArgumentNullException
2224local ArgumentOutOfRangeException = System .ArgumentOutOfRangeException
@@ -39,70 +41,16 @@ local ThreadStateException = System.define("System.ThreadStateException", {
3941 end
4042})
4143
42- local Thread = {}
4344local nextThreadId = 1
4445local threadYield = {}
46+ local currentThread
4547
4648local function getThreadId ()
4749 local id = nextThreadId
4850 nextThreadId = nextThreadId + 1
4951 return id
5052end
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-
10654local 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 )
12575end
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