summaryrefslogtreecommitdiff
path: root/xmake/core/base/scheduler.lua
diff options
context:
space:
mode:
authorruki <[email protected]>2020-02-06 00:54:46 +0800
committerruki <[email protected]>2020-02-07 22:45:57 +0800
commitdfef2f8ca41884138c5105cc7cbf55a67510f54d (patch)
tree0b3f363f7a8758b9a1c6ec6f8650cd9e45f20a95 /xmake/core/base/scheduler.lua
parentd40841b322485ec4200a857979b2ea77677a906a (diff)
fix curdir for scheduler
Diffstat (limited to 'xmake/core/base/scheduler.lua')
-rw-r--r--xmake/core/base/scheduler.lua41
1 files changed, 40 insertions, 1 deletions
diff --git a/xmake/core/base/scheduler.lua b/xmake/core/base/scheduler.lua
index 63829a913..a6ad21264 100644
--- a/xmake/core/base/scheduler.lua
+++ b/xmake/core/base/scheduler.lua
@@ -234,6 +234,26 @@ function scheduler:_poller_events_cb(obj, events)
return true
end
+-- update the current directory hash of current coroutine
+function scheduler:_co_curdir_update(curdir)
+
+ -- save the current directory hash
+ curdir = curdir or os.curdir()
+ local curdir_hash = hash.uuid4(path.absolute(curdir)):sub(1, 8)
+ self._CO_CURDIR = curdir_hash
+
+ -- save the current directory for each coroutine
+ local running = self:co_running()
+ if running then
+ local co_curdirs = self._CO_CURDIRS
+ if not co_curdirs then
+ co_curdirs = {}
+ self._CO_CURDIRS = co_curdirs
+ end
+ co_curdirs[running] = {curdir_hash, curdir}
+ end
+end
+
-- get all suspended coroutine tasks
function scheduler:_co_tasks_suspended()
local co_tasks_suspended = self._CO_TASKS_SUSPENDED
@@ -306,6 +326,7 @@ function scheduler:co_start_named(coname, cotask, ...)
-- start coroutine
local co
co = _coroutine.new(coname, coroutine.create(function(...)
+ self:_co_curdir_update()
cotask(...)
self:co_tasks()[co:thread()] = nil
if self:co_count() > 0 then
@@ -341,7 +362,20 @@ end
-- suspend the current coroutine
function scheduler:co_suspend(...)
- return coroutine.yield(...)
+
+ -- suspend it
+ local results = table.pack(coroutine.yield(...))
+
+ -- if the current directory has been changed? restore it
+ local running = assert(self:co_running())
+ local curdir = self._CO_CURDIR
+ local olddir = self._CO_CURDIRS and self._CO_CURDIRS[running] or nil
+ if olddir and curdir ~= olddir[1] then -- hash changed?
+ os.cd(olddir[2])
+ end
+
+ -- return results
+ return table.unpack(results)
end
-- yield the current coroutine
@@ -710,6 +744,11 @@ function scheduler:runloop()
self._SUPPORT_EV_POLLER_CLEAR = true
end
+ -- set on change directory callback for scheduler
+ os._sched_chdir_set(function (oldir, curdir)
+ self:_co_curdir_update(curdir)
+ end)
+
-- start all ready coroutine tasks
local co_ready_tasks = self._CO_READY_TASKS
if co_ready_tasks then