mirror of
https://github.com/CCLeonOS/LeonOS.git
synced 2026-03-03 15:01:12 +00:00
添加 LeonOS 的基本实现,包括: - 核心 API 模块(colors, disk, gps, keys, multishell, parallel, rednet, redstone, settings, vector) - 命令行程序(about, alias, bg, clear, copy, delete, edit, fg, help, list, lua, mkdir, move, paint, peripherals, programs, reboot, set, shutdown, threads) - 系统启动脚本和包管理 - 文档(README.md, LICENSE) - 开发工具(devbin)和更新程序 实现功能: - 完整的线程管理系统 - 兼容 ComputerCraft 的 API 设计 - 改进的 shell 和命令补全系统 - 多标签终端支持 - 设置管理系统
55 lines
1009 B
Lua
55 lines
1009 B
Lua
-- 'parallel' implementation
|
|
-- uses LeonOS's native threading
|
|
|
|
local parallel = {}
|
|
|
|
local thread = require("rc.thread")
|
|
local expect = require("cc.expect").expect
|
|
|
|
local function rand_id()
|
|
local id = "parallel-"
|
|
for _=1, 8, 1 do
|
|
id = id .. string.char(math.random(33, 126))
|
|
end
|
|
return id
|
|
end
|
|
|
|
local function waitForN(num, ...)
|
|
local funcs = table.pack(...)
|
|
|
|
local threads = {}
|
|
for i=1, #funcs, 1 do
|
|
expect(i, funcs[i], "function")
|
|
end
|
|
|
|
for i=1, #funcs, 1 do
|
|
threads[i] = thread.spawn(funcs[i], rand_id())
|
|
end
|
|
|
|
local dead = 0
|
|
repeat
|
|
coroutine.yield()
|
|
for i=#threads, 1, -1 do
|
|
if not thread.exists(threads[i]) then
|
|
table.remove(threads, i)
|
|
dead = dead + 1
|
|
end
|
|
end
|
|
until dead >= num
|
|
|
|
-- clean up excess
|
|
for i=1, #threads, 1 do
|
|
thread.remove(threads[i])
|
|
end
|
|
end
|
|
|
|
function parallel.waitForAny(...)
|
|
return waitForN(1, ...)
|
|
end
|
|
|
|
function parallel.waitForAll(...)
|
|
return waitForN(select("#", ...), ...)
|
|
end
|
|
|
|
return parallel
|