summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorruki <[email protected]>2021-05-11 23:05:05 +0800
committerruki <[email protected]>2021-05-11 23:05:05 +0800
commita19a2e76fbd8abe1f592f013776a8776a758aa1f (patch)
tree637b83038ba3c69dde63b95f8907220d5ff37296
parent25ab4b16a712d599246afd9ee7512194783d9706 (diff)
add trampoline for coroutine
-rw-r--r--xmake/core/base/os.lua4
-rw-r--r--xmake/core/base/scheduler.lua15
-rw-r--r--xmake/modules/private/async/runjobs.lua12
3 files changed, 26 insertions, 5 deletions
diff --git a/xmake/core/base/os.lua b/xmake/core/base/os.lua
index f2c62ee45..8fe7457e9 100644
--- a/xmake/core/base/os.lua
+++ b/xmake/core/base/os.lua
@@ -1002,7 +1002,7 @@ end
function os.getenvs()
local envs = os._CURENVS
if not envs then
- --print("os.getenvs")
+-- print("os.getenvs")
envs = {}
for _, line in ipairs(os._getenvs()) do
local p = line:find('=', 1, true)
@@ -1055,7 +1055,7 @@ end
os._setenv2 = os._setenv
os._setenv = function (name, value)
print("setenv", name, value)
- os._setenv2(name, value)
+ return os._setenv2(name, value)
end
os._getenv = os.getenv
diff --git a/xmake/core/base/scheduler.lua b/xmake/core/base/scheduler.lua
index bfdde4b83..34dfacb85 100644
--- a/xmake/core/base/scheduler.lua
+++ b/xmake/core/base/scheduler.lua
@@ -87,6 +87,17 @@ function _coroutine:is_suspended()
return self:status() == "suspended"
end
+-- is trampoline?
+function _coroutine:is_trampoline()
+ return self._TRAMPOLINE
+end
+
+-- mark this coroutine as trampoline,
+-- envs and curdir will not be changed when switch to this coroutine
+function _coroutine:set_trampoline(trampoline)
+ self._TRAMPOLINE = trampoline
+end
+
-- get the current timer task
function _coroutine:_timer_task()
return self._TIMER_TASK
@@ -382,14 +393,14 @@ function scheduler:co_suspend(...)
local running = assert(self:co_running())
local curdir = self._CO_CURDIR_HASH
local olddir = self._CO_CURDIRS and self._CO_CURDIRS[running] or nil
- if olddir and curdir ~= olddir[1] then -- hash changed?
+ if olddir and curdir ~= olddir[1] and not running:is_trampoline() then -- hash changed?
os.cd(olddir[2])
end
-- if the current environments has been changed? restore it
local curenvs = self._CO_CURENVS_HASH
local oldenvs = self._CO_CURENVS and self._CO_CURENVS[running] or nil
- if oldenvs and curenvs ~= oldenvs[1] then -- hash changed?
+ if oldenvs and curenvs ~= oldenvs[1] and not running:is_trampoline() then -- hash changed?
os.setenvs(oldenvs[2])
end
diff --git a/xmake/modules/private/async/runjobs.lua b/xmake/modules/private/async/runjobs.lua
index 1f5e35634..23e6b7ecf 100644
--- a/xmake/modules/private/async/runjobs.lua
+++ b/xmake/modules/private/async/runjobs.lua
@@ -73,6 +73,12 @@ function main(name, jobs, opt)
progress_helper = progress.new(nil, opt)
end
+ -- mark current main coroutine as trampoline to avoid change envs/curdir
+ local co_running = scheduler.co_running()
+ if co_running then
+ co_running:set_trampoline(true)
+ end
+
-- run timer
local stop = false
local running_jobs_indices = {}
@@ -194,7 +200,6 @@ function main(name, jobs, opt)
os.cd(opt.curdir)
end
if jobenvs then
- --print("jobenvs", jobenvs)
os.addenvs(jobenvs)
end
jobfunc(count_as_index and count or i, total)
@@ -251,6 +256,11 @@ function main(name, jobs, opt)
progress_helper:stop()
end
+ -- restore current main coroutine
+ if co_running then
+ co_running:set_trampoline(false)
+ end
+
-- do exit callback
if opt.on_exit then
opt.on_exit()