summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorruki <[email protected]>2025-04-16 00:44:46 +0800
committerruki <[email protected]>2025-08-28 11:35:53 +0800
commit3fa63c937ef4b442d120bc5ba86e9b4eb29b009c (patch)
tree98935162c42784aa4ace767daecb8d1fb1847552
parentb63b1e9f2635242113724bc8c0f41afbaa15f9ee (diff)
add thread module stub
-rw-r--r--tests/modules/thread/sleep.lua17
-rw-r--r--xmake/core/sandbox/modules/coroutine.lua4
-rw-r--r--xmake/core/sandbox/modules/import/core/thread/thread.lua119
-rw-r--r--xmake/core/thread/thread.lua20
4 files changed, 156 insertions, 4 deletions
diff --git a/tests/modules/thread/sleep.lua b/tests/modules/thread/sleep.lua
new file mode 100644
index 000000000..1a6ed8d91
--- /dev/null
+++ b/tests/modules/thread/sleep.lua
@@ -0,0 +1,17 @@
+import("core.thread.thread")
+
+function callback(id)
+ print("%s: %d ..", thread.running(), id)
+ local dt = os.mclock()
+ os.sleep(1000)
+ dt = os.mclock() - dt
+ print("%s: %d end, dt: %d ms", thread.running(), id, dt)
+end
+
+function main()
+ for i = 1, 10 do
+ thread.new(callback, {argv = {i}, name = "session_" .. i}):start()
+ end
+ --thread.wait()
+end
+
diff --git a/xmake/core/sandbox/modules/coroutine.lua b/xmake/core/sandbox/modules/coroutine.lua
index 60d67be96..b1af6581f 100644
--- a/xmake/core/sandbox/modules/coroutine.lua
+++ b/xmake/core/sandbox/modules/coroutine.lua
@@ -34,8 +34,6 @@ sandbox_coroutine.running = coroutine.running
-- resume coroutine
function sandbox_coroutine.resume(co, ...)
-
- -- resume it
local ok, results = coroutine.resume(co, ...)
if not ok then
@@ -54,8 +52,6 @@ function sandbox_coroutine.resume(co, ...)
-- raise it
raise(errors)
end
-
- -- ok
return results
end
diff --git a/xmake/core/sandbox/modules/import/core/thread/thread.lua b/xmake/core/sandbox/modules/import/core/thread/thread.lua
new file mode 100644
index 000000000..d88b6ef1f
--- /dev/null
+++ b/xmake/core/sandbox/modules/import/core/thread/thread.lua
@@ -0,0 +1,119 @@
+--!A cross-platform build utility based on Lua
+--
+-- Licensed under the Apache License, Version 2.0 (the "License");
+-- you may not use this file except in compliance with the License.
+-- You may obtain a copy of the License at
+--
+-- http://www.apache.org/licenses/LICENSE-2.0
+--
+-- Unless required by applicable law or agreed to in writing, software
+-- distributed under the License is distributed on an "AS IS" BASIS,
+-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+-- See the License for the specific language governing permissions and
+-- limitations under the License.
+--
+-- Copyright (C) 2015-present, TBOOX Open Source Group.
+--
+-- @author ruki
+-- @file thread.lua
+--
+
+-- load modules
+local utils = require("base/utils")
+local string = require("base/string")
+local thread = require("thread/thread")
+local raise = require("sandbox/modules/raise")
+
+-- define module
+local sandbox_core_thread = sandbox_core_thread or {}
+local sandbox_core_thread_instance = sandbox_core_thread_instance or {}
+
+-- export the thread types
+sandbox_core_thread.TCP = thread.TCP
+sandbox_core_thread.UDP = thread.UDP
+sandbox_core_thread.ICMP = thread.ICMP
+
+-- export the thread families
+sandbox_core_thread.IPV4 = thread.IPV4
+sandbox_core_thread.IPV6 = thread.IPV6
+
+-- export the thread events
+sandbox_core_thread.EV_RECV = thread.EV_RECV
+sandbox_core_thread.EV_SEND = thread.EV_SEND
+sandbox_core_thread.EV_CONN = thread.EV_CONN
+sandbox_core_thread.EV_ACPT = thread.EV_ACPT
+
+-- export the thread control code
+sandbox_core_thread.CTRL_SET_RECVBUFF = thread.CTRL_SET_RECVBUFF
+sandbox_core_thread.CTRL_SET_SENDBUFF = thread.CTRL_SET_SENDBUFF
+
+-- wrap thread
+function _thread_wrap(instance)
+
+ -- hook thread interfaces
+ local hooked = {}
+ for name, func in pairs(sandbox_core_thread_instance) do
+ if not name:startswith("_") and type(func) == "function" then
+ hooked["_" .. name] = instance["_" .. name] or instance[name]
+ hooked[name] = func
+ end
+ end
+ for name, func in pairs(hooked) do
+ instance[name] = func
+ end
+ return instance
+end
+
+-- start thread
+function sandbox_core_thread_instance.start(instance)
+ local ok, errors = instance:_start()
+ if not ok then
+ raise(errors)
+ end
+end
+
+-- suspend thread
+function sandbox_core_thread_instance.suspend(instance)
+ local ok, errors = instance:_suspend()
+ if not ok then
+ raise(errors)
+ end
+end
+
+-- resume thread
+function sandbox_core_thread_instance.resume(instance)
+ local ok, errors = instance:_resume()
+ if not ok then
+ raise(errors)
+ end
+end
+
+-- wait thread
+function sandbox_core_thread_instance.wait(instance, timeout)
+ local ok, errors = instance:_wait(timeout)
+ if ok < 0 then
+ raise(errors)
+ end
+end
+
+-- new thread
+function sandbox_core_thread.new(callback, opt)
+ local instance, errors = thread.new(callback, opt)
+ if not instance then
+ raise(errors)
+ end
+ return _thread_wrap(instance)
+end
+
+-- get the running thread
+function sandbox_core_thread.running()
+ local instance, errors = thread.running()
+ if not instance then
+ raise(errors)
+ end
+ return instance
+end
+
+-- return module
+return sandbox_core_thread
+
diff --git a/xmake/core/thread/thread.lua b/xmake/core/thread/thread.lua
index e4f82374f..3c8170f69 100644
--- a/xmake/core/thread/thread.lua
+++ b/xmake/core/thread/thread.lua
@@ -80,6 +80,22 @@ function _instance:is_dead()
return self:status() == thread.STATUS_DEAD
end
+-- start thread
+function _instance:start(instance)
+end
+
+-- suspend thread
+function _instance:suspend(instance)
+end
+
+-- resume thread
+function _instance:resume(instance)
+end
+
+-- wait thread
+function _instance:wait(instance, timeout)
+end
+
-- tostring(thread)
function _instance:__tostring()
local status_strs = self._STATUS_STRS
@@ -122,6 +138,10 @@ function thread.new(callback, opt)
end
end
+-- get the running thread
+function thread.running()
+end
+
-- return module
return thread