Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local com = require('component')
- local cmp = require('computer')
- local evt = require('event')
- local cor = coroutine
- local last_gpu = 0
- local gpu_addrs = {}
- local threads = {}
- local mt_thread = {}
- local DEBUG_MODE = false
- -- copy GPU addresses to table
- for k in com.list('gpu', true) do table.insert(gpu_addrs, k) end
- io.write("Commands: ")
- local cmd = io.read()
- io.write("Count: ")
- local cnt = tonumber(io.read())
- cmd = (cmd .. ' '):rep(cnt or 1)
- for v in cmd:gmatch('%S+') do
- io.write('Arguments for ' .. tostring(v) .. ': ')
- local args = io.read()
- -- replace %GPU% in arguments with real GPU address
- while args:match('%%GPU%%') do
- last_gpu = last_gpu + 1
- if last_gpu > #gpu_addrs then last_gpu = 1 end
- local gpu_addr = tostring(gpu_addrs[last_gpu] or "nil")
- print(tostring(v) .. ' - %GPU% found, substituting GPU address: ' .. gpu_addr:sub(1, 10) .. '...')
- args = args:gsub('%%GPU%%', gpu_addr, 1)
- end
- local t = {}
- t.thread = cor.create(function() os.execute(v .. ' ' .. args) end)
- t.is_ready = true
- t.yield_data = {}
- table.insert(threads, t)
- end
- local _pullSignal = cmp.pullSignal
- cmp.pullSignal = cor.yield
- while #threads > 0 do
- -- execute ALL threads with the last event, if its name matches their event filter
- local le = {_pullSignal(0)}
- local t_idx = 1
- while t_idx <= #threads do
- local ct = threads[t_idx]
- local has_timeout = tonumber(ct.yield_data[1]) and 1 or 0
- if ct.is_ready or le[1] == (ct.yield_data[has_timeout + 1] or le[1]) then
- ct.yield_data = table.pack(cor.resume(ct.thread, table.unpack(le)))
- if DEBUG_MODE then print('Thread', t_idx, 'yield data:', table.unpack(ct.yield_data)) end
- if not table.remove(ct.yield_data, 1) or cor.status(ct.thread) == "dead" then
- table.remove(threads, t_idx)
- if DEBUG_MODE then print('Thread', t_idx, 'removed from list') end
- t_idx = t_idx - 1
- end
- ct.is_ready = (ct.yield_data.n == 0)
- end
- t_idx = t_idx + 1
- end
- end
- cmp.pullSignal = _pullSignal
Add Comment
Please, Sign In to add comment