mirror of
https://github.com/kepler155c/opus
synced 2025-02-02 18:39:09 +00:00
sync module improvements
This commit is contained in:
parent
846569952c
commit
88f126bf2f
@ -1,5 +1,5 @@
|
||||
local Socket = require('socket')
|
||||
local synchronized = require('sync')
|
||||
local synchronized = require('sync').sync
|
||||
|
||||
local fs = _G.fs
|
||||
|
||||
|
@ -1,26 +1,61 @@
|
||||
local syncLocks = { }
|
||||
local Sync = {
|
||||
syncLocks = { }
|
||||
}
|
||||
|
||||
local os = _G.os
|
||||
|
||||
return function(obj, fn)
|
||||
function Sync.sync(obj, fn)
|
||||
local key = tostring(obj)
|
||||
if syncLocks[key] then
|
||||
if Sync.syncLocks[key] then
|
||||
local cos = tostring(coroutine.running())
|
||||
table.insert(syncLocks[key], cos)
|
||||
table.insert(Sync.syncLocks[key], cos)
|
||||
repeat
|
||||
local _, co = os.pullEvent('sync_lock')
|
||||
until co == cos
|
||||
else
|
||||
syncLocks[key] = { }
|
||||
Sync.syncLocks[key] = { }
|
||||
end
|
||||
local s, m = pcall(fn)
|
||||
local co = table.remove(syncLocks[key], 1)
|
||||
local co = table.remove(Sync.syncLocks[key], 1)
|
||||
if co then
|
||||
os.queueEvent('sync_lock', co)
|
||||
else
|
||||
syncLocks[key] = nil
|
||||
Sync.syncLocks[key] = nil
|
||||
end
|
||||
if not s then
|
||||
error(m)
|
||||
end
|
||||
end
|
||||
|
||||
function Sync.lock(obj)
|
||||
local key = tostring(obj)
|
||||
if Sync.syncLocks[key] then
|
||||
local cos = tostring(coroutine.running())
|
||||
table.insert(Sync.syncLocks[key], cos)
|
||||
repeat
|
||||
local _, co = os.pullEvent('sync_lock')
|
||||
until co == cos
|
||||
else
|
||||
Sync.syncLocks[key] = { }
|
||||
end
|
||||
end
|
||||
|
||||
function Sync.release(obj)
|
||||
local key = tostring(obj)
|
||||
if not Sync.syncLocks[key] then
|
||||
error('Sync.release: Lock was not obtained', 2)
|
||||
end
|
||||
local co = table.remove(Sync.syncLocks[key], 1)
|
||||
if co then
|
||||
os.queueEvent('sync_lock', co)
|
||||
else
|
||||
Sync.syncLocks[key] = nil
|
||||
end
|
||||
end
|
||||
|
||||
function Sync.isLocked(obj)
|
||||
local key = tostring(obj)
|
||||
return not not Sync.syncLocks[key]
|
||||
end
|
||||
|
||||
return Sync
|
||||
|
@ -2959,6 +2959,7 @@ UI.Chooser.defaults = {
|
||||
choices = { },
|
||||
nochoice = 'Select',
|
||||
backgroundFocusColor = colors.lightGray,
|
||||
textInactiveColor = colors.gray,
|
||||
leftIndicator = '<',
|
||||
rightIndicator = '>',
|
||||
height = 1,
|
||||
@ -2981,13 +2982,14 @@ function UI.Chooser:draw()
|
||||
if self.focused then
|
||||
bg = self.backgroundFocusColor
|
||||
end
|
||||
local fg = self.inactive and self.textInactiveColor or self.textColor
|
||||
local choice = Util.find(self.choices, 'value', self.value)
|
||||
local value = self.nochoice
|
||||
if choice then
|
||||
value = choice.name
|
||||
end
|
||||
self:write(1, 1, self.leftIndicator, self.backgroundColor, colors.black)
|
||||
self:write(2, 1, ' ' .. Util.widthify(tostring(value), self.width-4) .. ' ', bg)
|
||||
self:write(2, 1, ' ' .. Util.widthify(tostring(value), self.width-4) .. ' ', bg, fg)
|
||||
self:write(self.width, 1, self.rightIndicator, self.backgroundColor, colors.black)
|
||||
end
|
||||
|
||||
|
@ -7,7 +7,7 @@ _G.requireInjector(_ENV)
|
||||
local Pathing = require('turtle.pathfind')
|
||||
local GPS = require('gps')
|
||||
local Point = require('point')
|
||||
local synchronized = require('sync')
|
||||
local synchronized = require('sync').sync
|
||||
local Util = require('util')
|
||||
|
||||
local os = _G.os
|
||||
|
@ -34,6 +34,12 @@ _G._debug = function(pattern, ...)
|
||||
term.redirect(oldTerm)
|
||||
end
|
||||
|
||||
if not _G.debug then -- don't clobber lua debugger
|
||||
function _G.debug(...)
|
||||
_G._debug(...)
|
||||
end
|
||||
end
|
||||
|
||||
-- any function that runs in a kernel hook does not run in
|
||||
-- a separate coroutine or have a window. an error in a hook
|
||||
-- function will crash the system.
|
||||
|
Loading…
Reference in New Issue
Block a user