diff options
| author | ruki <[email protected]> | 2025-09-02 10:16:24 +0800 |
|---|---|---|
| committer | GitHub <[email protected]> | 2025-09-02 10:16:24 +0800 |
| commit | 148501afafa5e9e05da7a54bc804880d6c415849 (patch) | |
| tree | f5cbcee2b05d4b836478a45698c8a42b43ea38b7 /tests/modules/thread | |
| parent | 90b7015682c5523dfa252ebfbab6ed16b3e4b16a (diff) | |
| parent | 37a499eeb5fd84be4ac83d639211e18e55dccd32 (diff) | |
Merge pull request #6324 from xmake-io/thread
Add native thread support
Diffstat (limited to 'tests/modules/thread')
| -rw-r--r-- | tests/modules/thread/coroutine.lua | 32 | ||||
| -rw-r--r-- | tests/modules/thread/event.lua | 25 | ||||
| -rw-r--r-- | tests/modules/thread/mutex.lua | 24 | ||||
| -rw-r--r-- | tests/modules/thread/queue.lua | 28 | ||||
| -rw-r--r-- | tests/modules/thread/semaphore.lua | 25 | ||||
| -rw-r--r-- | tests/modules/thread/sharedata.lua | 26 | ||||
| -rw-r--r-- | tests/modules/thread/sleep.lua | 21 |
7 files changed, 181 insertions, 0 deletions
diff --git a/tests/modules/thread/coroutine.lua b/tests/modules/thread/coroutine.lua new file mode 100644 index 000000000..28aa8971e --- /dev/null +++ b/tests/modules/thread/coroutine.lua @@ -0,0 +1,32 @@ +import("core.base.thread") +import("core.base.scheduler") + +function thread_loop() + import("core.base.thread") + print("%s: starting ..", thread.running()) + local dt = os.mclock() + for i = 1, 10 do + print("%s: %d", thread.running(), i) + os.sleep(1000) + end + dt = os.mclock() - dt + print("%s: end, dt: %d ms", thread.running(), dt) +end + +function coroutine_loop() + print("%s: starting ..", scheduler.co_running()) + local dt = os.mclock() + for i = 1, 10 do + print("%s: %d", scheduler.co_running(), i) + os.sleep(1000) + end + dt = os.mclock() - dt + print("%s: end, dt: %d ms", scheduler.co_running(), dt) +end + +function main() + scheduler.co_start_named("coroutine", coroutine_loop) + local t = thread.start_named("thread", thread_loop) + t:wait(-1) +end + diff --git a/tests/modules/thread/event.lua b/tests/modules/thread/event.lua new file mode 100644 index 000000000..fde1511ee --- /dev/null +++ b/tests/modules/thread/event.lua @@ -0,0 +1,25 @@ +import("core.base.thread") + +function callback(event) + import("core.base.thread") + print("%s: starting ..", thread.running()) + while true do + print("%s: waiting ..", thread.running()) + if event:wait(-1) > 0 then + print("%s: triggered", thread.running()) + end + end +end + +function main() + local event = thread.event() + local t = thread.start_named("keyboard", callback, event) + while true do + local ch = io.read() + if ch then + event:post() + end + end + t:wait(-1) +end + diff --git a/tests/modules/thread/mutex.lua b/tests/modules/thread/mutex.lua new file mode 100644 index 000000000..e969c0747 --- /dev/null +++ b/tests/modules/thread/mutex.lua @@ -0,0 +1,24 @@ +import("core.base.thread") + +function callback(mutex) + import("core.base.thread") + print("%s: starting ..", thread.running()) + local dt = os.mclock() + for i = 1, 10 do + mutex:lock() + print("%s: %d", thread.running(), i) + mutex:unlock() + os.sleep(1000) + end + dt = os.mclock() - dt + print("%s: end, dt: %d ms", thread.running(), dt) +end + +function main() + local mutex = thread.mutex() + local t0 = thread.start_named("thread_0", callback, mutex) + local t1 = thread.start_named("thread_1", callback, mutex) + t0:wait(-1) + t1:wait(-1) +end + diff --git a/tests/modules/thread/queue.lua b/tests/modules/thread/queue.lua new file mode 100644 index 000000000..ebf298ec2 --- /dev/null +++ b/tests/modules/thread/queue.lua @@ -0,0 +1,28 @@ +import("core.base.thread") + +function callback(event, queue) + print("starting ..") + while true do + print("waiting ..") + if event:wait(-1) > 0 then + while not queue:empty() do + print(" -> %s", queue:pop()) + end + end + end +end + +function main() + local event = thread.event() + local queue = thread.queue() + local t = thread.start_named("", callback, event, queue) + while true do + local ch = io.read() + if ch then + queue:push(ch) + event:post() + end + end + t:wait(-1) +end + diff --git a/tests/modules/thread/semaphore.lua b/tests/modules/thread/semaphore.lua new file mode 100644 index 000000000..b05774e1d --- /dev/null +++ b/tests/modules/thread/semaphore.lua @@ -0,0 +1,25 @@ +import("core.base.thread") + +function callback(semaphore) + import("core.base.thread") + print("%s: starting ..", thread.running()) + while true do + print("%s: waiting ..", thread.running()) + if semaphore:wait(-1) > 0 then + print("%s: triggered", thread.running()) + end + end +end + +function main() + local semaphore = thread.semaphore("", 1) + local t = thread.start_named("keyboard", callback, semaphore) + while true do + local ch = io.read() + if ch then + semaphore:post(2) + end + end + t:wait(-1) +end + diff --git a/tests/modules/thread/sharedata.lua b/tests/modules/thread/sharedata.lua new file mode 100644 index 000000000..cce265ae1 --- /dev/null +++ b/tests/modules/thread/sharedata.lua @@ -0,0 +1,26 @@ +import("core.base.thread") + +function callback(event, sharedata) + print("starting ..") + while true do + print("waiting ..") + if event:wait(-1) > 0 then + print(" -> %s", sharedata:get()) + end + end +end + +function main() + local event = thread.event() + local sharedata = thread.sharedata() + local t = thread.start_named("", callback, event, sharedata) + while true do + local ch = io.read() + if ch then + sharedata:set(ch) + event:post() + end + end + t:wait(-1) +end + diff --git a/tests/modules/thread/sleep.lua b/tests/modules/thread/sleep.lua new file mode 100644 index 000000000..2e21aa3d2 --- /dev/null +++ b/tests/modules/thread/sleep.lua @@ -0,0 +1,21 @@ +import("core.base.thread") + +function callback(id) + import("core.base.thread") + print("%s: %d starting ..", thread.running(), id) + local dt = os.mclock() + for i = 1, 10 do + print("%s: %d", thread.running(), i) + os.sleep(1000) + end + dt = os.mclock() - dt + print("%s: %d end, dt: %d ms", thread.running(), id, dt) +end + +function main() + local t0 = thread.start_named("thread_0", callback, 0) + local t1 = thread.start_named("thread_1", callback, 1) + t0:wait(-1) + t1:wait(-1) +end + |
