diff options
| author | ruki <[email protected]> | 2025-04-18 22:42:48 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2025-08-28 11:35:53 +0800 |
| commit | ad77d170e407fc7d346df32a88683aae050bf36b (patch) | |
| tree | e1ae2f06dc31a84ae42b4dd6086cb570ef88b95b /xmake | |
| parent | 6eccd1efa540d634e35eb29cf923025e0667aa0c (diff) | |
run thread
Diffstat (limited to 'xmake')
| -rw-r--r-- | xmake/core/_xmake_main.lua | 1 | ||||
| -rw-r--r-- | xmake/core/main.lua | 51 | ||||
| -rw-r--r-- | xmake/core/thread/thread.lua | 9 |
3 files changed, 44 insertions, 17 deletions
diff --git a/xmake/core/_xmake_main.lua b/xmake/core/_xmake_main.lua index 282106806..5d2d8f7a3 100644 --- a/xmake/core/_xmake_main.lua +++ b/xmake/core/_xmake_main.lua @@ -36,6 +36,7 @@ xmake._PROJECT_FILE = "xmake.lua" xmake._WORKING_DIR = os.curdir() xmake._FEATURES = _FEATURES xmake._LUAJIT = _LUAJIT +xmake._THREAD_CALLINFO = _THREAD_CALLINFO -- In order to be compatible with updates from lower versions of engine core -- @see https://github.com/xmake-io/xmake/issues/1694#issuecomment-925507210 diff --git a/xmake/core/main.lua b/xmake/core/main.lua index 87e94c824..95202b04c 100644 --- a/xmake/core/main.lua +++ b/xmake/core/main.lua @@ -258,6 +258,33 @@ function main._limit_root() return not option.get("root") and os.getenv("XMAKE_ROOT") ~= 'y' and os.host() ~= 'haiku' end +-- run task +function main._run_task(taskname) + local taskinst = task.task(taskname) or project.task(taskname) + if not taskinst then + return main._exit(false, string.format("do unknown task(%s)!", taskname)) + end + + scheduler:co_start_named("xmake " .. taskname, function () + local ok, errors = taskinst:run() + if not ok then + os.raise(errors) + end + end) +end + +-- run thread +function main._run_thread(callinfo_str) + local callinfo, errors = string.deserialize(callinfo_str) + if not callinfo then + return main._exit(false, string.format("invalid thread callinfo, %s!", errors or "unknown")) + end + local callback = callinfo.callback + if callback then + callback(callinfo.argv) + end +end + -- the main entry function function main.entry() @@ -314,22 +341,18 @@ Or you can add `--root` option or XMAKE_ROOT=y to allow run as root temporarily. localcache.save("history") end - -- get task instance - local taskname = option.taskname() or "build" - local taskinst = task.task(taskname) or project.task(taskname) - if not taskinst then - return main._exit(false, string.format("do unknown task(%s)!", taskname)) - end - - -- run task + -- enable scheduler scheduler:enable(true) - scheduler:co_start_named("xmake " .. taskname, function () - local ok, errors = taskinst:run() - if not ok then - os.raise(errors) - end - end) + -- run task or thread + local thread_callinfo = xmake._THREAD_CALLINFO + if thread_callinfo then + main._run_thread(thread_callinfo) + else + main._run_task(option.taskname() or "build") + end + + -- start runloop ok, errors = scheduler:runloop() if not ok then return main._exit(ok, errors) diff --git a/xmake/core/thread/thread.lua b/xmake/core/thread/thread.lua index cac5d916d..33ac3dca0 100644 --- a/xmake/core/thread/thread.lua +++ b/xmake/core/thread/thread.lua @@ -91,11 +91,14 @@ function _instance:start() assert(not self:cdata()) -- serialize and pass callback and arguments to this thread - local callback = string.serialize(self._CALLBACK, {strip = true, indent = false}) - local argv = string.serialize(self._ARGV, {strip = true, indent = false}) + local callinfo = { + callback = self._CALLBACK, + argv = self._ARGV + } + callinfo = string.serialize(callinfo, {strip = true, indent = false}) -- init and start thread - local handle, errors = thread.thread_init(self:name(), callback, argv, self._STACKSIZE) + local handle, errors = thread.thread_init(self:name(), callinfo, self._STACKSIZE) if not handle then return nil, errors or string.format("%s: failed to create thread!", self) end |
