add thread timer · Drake53/CSharp.lua@be69019 · GitHub
Skip to content

Commit be69019

Browse files
committed
add thread timer
1 parent 6789475 commit be69019

9 files changed

Lines changed: 336 additions & 34 deletions

File tree

‎CSharp.lua/CoreSystem.Lua/All.lua‎

Lines changed: 2 additions & 0 deletions

‎CSharp.lua/CoreSystem.Lua/CoreSystem/Collection.lua‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ limitations under the License.
1616

1717
local System = System
1818
local throw = System.throw
19-
local sr = System.sr
19+
local div = System.div
2020
local ArgumentOutOfRangeException = System.ArgumentOutOfRangeException
2121
local InvalidOperationException = System.InvalidOperationException
2222
local ArgumentNullException = System.ArgumentNullException
@@ -227,7 +227,7 @@ local function binarySearchArray(t, index, count, v, comparer)
227227
local lo = index
228228
local hi = index + count - 1
229229
while lo <= hi do
230-
local i = lo + sr(hi - lo, 1)
230+
local i = lo + div(hi - lo, 2)
231231
local order = compare(unWrap(t[i + 1]), v);
232232
if order == 0 then return i end
233233
if order < 0 then
@@ -278,7 +278,7 @@ local function findIndexOfArray(t, ...)
278278
for i = startIndex + 1, endIndex do
279279
local item = unWrap(t[i])
280280
if match(item) then
281-
return i - 1
281+
return i - 1
282282
end
283283
end
284284
return -1

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

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

321321
local time = System.config.time or ostime
322+
System.time = time
322323

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

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

Lines changed: 34 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@ limitations under the License.
1616

1717
local System = System
1818
local throw = System.throw
19+
local try = System.try
1920
local trunc = System.trunc
21+
local post = System.post
2022
local Exception = System.Exception
2123
local NotImplementedException = System.NotImplementedException
2224
local ArgumentOutOfRangeException = System.ArgumentOutOfRangeException
@@ -134,13 +136,13 @@ function TaskExceptionHolder.__gc(this)
134136
end
135137

136138
local Task = {}
137-
local taskIdCounter = 1
139+
local nextTaskId = 1
138140
local currentTask
139141
local completedTask
140142

141143
local function getNewId()
142-
local id = taskIdCounter
143-
taskIdCounter = taskIdCounter + 1
144+
local id = nextTaskId
145+
nextTaskId = nextTaskId + 1
144146
return id
145147
end
146148

@@ -167,12 +169,10 @@ function Task.getStatus(this)
167169
end
168170

169171
local function getException(this)
170-
local exceptionsHolder = this.exceptionsHolder
171-
if exceptionsHolder then
172-
return createExceptionObject(exceptionsHolder)
173-
else
174-
return nil
172+
if this.status == TaskStatusFaulted then
173+
return createExceptionObject(this.data)
175174
end
175+
return nil
176176
end
177177

178178
Task.getException = getException
@@ -193,7 +193,7 @@ function Task.getIsFaulted(this)
193193
end
194194

195195
local function fromResult(result)
196-
return setmetatable({ status = TaskStatusRanToCompletion, result = result }, Task)
196+
return setmetatable({ status = TaskStatusRanToCompletion, data = result }, Task)
197197
end
198198

199199
Task.FromResult = fromResult
@@ -202,7 +202,7 @@ local function fromCanceled(cancellationToken)
202202
if cancellationToken and cancellationToken:getIsCancellationRequested() then
203203
throw(ArgumentOutOfRangeException("cancellationToken"))
204204
end
205-
return setmetatable({ status = TaskStatusCanceled, cancellationToken = cancellationToken }, Task)
205+
return setmetatable({ status = TaskStatusCanceled, data = cancellationToken }, Task)
206206
end
207207

208208
Task.FromCanceled = fromCanceled
@@ -224,13 +224,7 @@ local function trySetComplete(this, status, data)
224224
end
225225

226226
this.status = status
227-
if status == TaskStatusRanToCompletion then
228-
this.result = data
229-
elseif status == TaskStatusFaulted then
230-
this.exceptionsHolder = data
231-
elseif status == TaskStatusCanceled then
232-
this.cancellationToken = data
233-
end
227+
this.data = data
234228

235229
local continueActions = this.continueActions
236230
if continueActions then
@@ -261,8 +255,9 @@ local function newWaitingTask(isVoid)
261255
return setmetatable({ status = TaskStatusWaitingForActivation, isVoid = isVoid }, Task)
262256
end
263257

264-
local setTimeout = System.config.setTimeout
265-
local clearTimeout = System.config.clearTimeout
258+
local config = System.config
259+
local setTimeout = config.setTimeout
260+
local clearTimeout = config.clearTimeout
266261

267262
function Task.Delay(delay, cancellationToken)
268263
if not setTimeout or not clearTimeout then
@@ -317,7 +312,22 @@ local function getContinueActions(task)
317312
return continueActions
318313
end
319314

320-
function Task.ContinueWith(this, ...)
315+
function Task.ContinueWith(this, continuationAction)
316+
local t = newWaitingTask()
317+
local function f()
318+
try(function ()
319+
assert(trySetResult(t, continuationAction()))
320+
end, function (e)
321+
assert(trySetException(t, e))
322+
end)
323+
end
324+
if isCompleted(this) then
325+
post(f)
326+
else
327+
local continueActions = getContinueActions(task)
328+
tinsert(continueActions, f)
329+
end
330+
return t
321331
end
322332

323333
local function await(t, task)
@@ -327,9 +337,9 @@ local function await(t, task)
327337
local status = task.status
328338
local ok, v
329339
if status == TaskStatusRanToCompletion then
330-
ok, v = true, task.result
340+
ok, v = true, task.data
331341
elseif status == TaskStatusFaulted then
332-
ok, v = false, getException(task)
342+
ok, v = false, createExceptionObject(task.data)
333343
elseif status == TaskStatusCanceled then
334344
ok, v = false, TaskCanceledException(task)
335345
else
@@ -353,9 +363,9 @@ function Task.await(this, task)
353363
if status == TaskStatusWaitingForActivation then
354364
return await(this, task)
355365
elseif status == TaskStatusRanToCompletion then
356-
return task.result
366+
return task.data
357367
elseif status == TaskStatusFaulted then
358-
throw(getException(task))
368+
throw(createExceptionObject(task.data))
359369
elseif status == TaskStatusCanceled then
360370
throw(TaskCanceledException(task))
361371
else
Lines changed: 135 additions & 0 deletions

0 commit comments

Comments
 (0)