summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorruki <[email protected]>2024-01-26 22:32:15 +0800
committerruki <[email protected]>2024-01-26 22:32:15 +0800
commita4f12b48cc062cade64b7e5eaaeb1a04e3c80632 (patch)
tree266ceb1c37a0e41191a297bca797c3d199277c35
parent4dc26106cea35b5fed96c58b1684bd4235b3ff4b (diff)
use timer callback
-rw-r--r--xmake/core/base/scheduler.lua35
1 files changed, 12 insertions, 23 deletions
diff --git a/xmake/core/base/scheduler.lua b/xmake/core/base/scheduler.lua
index ac7aed9cc..262c842dd 100644
--- a/xmake/core/base/scheduler.lua
+++ b/xmake/core/base/scheduler.lua
@@ -541,11 +541,6 @@ function scheduler:co_lock(lockname)
co_locked_tasks = {}
self._CO_LOCKED_TASKS = co_locked_tasks
end
- local co_waiting_tasks = self._CO_WAITING_TASKS
- if co_waiting_tasks == nil then
- co_waiting_tasks = {}
- self._CO_WAITING_TASKS = co_waiting_tasks
- end
while true do
-- try to lock it
@@ -554,9 +549,18 @@ function scheduler:co_lock(lockname)
return true
end
- -- this lock has been occupied, we need to wait it
- co_waiting_tasks[lockname] = co_waiting_tasks[lockname] or {}
- table.insert(co_waiting_tasks[lockname], running)
+ -- register timeout task to timer
+ local function timer_callback (cancel)
+ if co_locked_tasks[lockname] == nil then
+ if running:is_suspended() then
+ return self:co_resume(running)
+ end
+ else
+ self:_timer():post(timer_callback, 500)
+ end
+ return true
+ end
+ self:_timer():post(timer_callback, 500)
-- wait
self:co_suspend()
@@ -589,21 +593,6 @@ function scheduler:co_unlock(lockname)
end
if co_locked_tasks[lockname] == running then
co_locked_tasks[lockname] = nil
- local co_waiting_tasks = self._CO_WAITING_TASKS
- if co_waiting_tasks then
- local waiting_tasks = co_waiting_tasks[lockname]
- if waiting_tasks and #waiting_tasks > 0 then
- co_waiting_tasks[lockname] = nil
- for _, co_task in ipairs(waiting_tasks) do
- if co_task:is_suspended() then
- local ok, errors = self:co_resume(co_task)
- if not ok then
- return false, errors
- end
- end
- end
- end
- end
else
return false, string.format("unlock(%s) is called in other %s", lockname, running)
end