1
0
mirror of https://github.com/kepler155c/opus synced 2025-02-02 18:39:09 +00:00

sync module improvements

This commit is contained in:
kepler155c@gmail.com 2018-10-31 19:38:09 -04:00
parent 846569952c
commit 88f126bf2f
5 changed files with 53 additions and 10 deletions

View File

@ -1,5 +1,5 @@
local Socket = require('socket') local Socket = require('socket')
local synchronized = require('sync') local synchronized = require('sync').sync
local fs = _G.fs local fs = _G.fs

View File

@ -1,26 +1,61 @@
local syncLocks = { } local Sync = {
syncLocks = { }
}
local os = _G.os local os = _G.os
return function(obj, fn) function Sync.sync(obj, fn)
local key = tostring(obj) local key = tostring(obj)
if syncLocks[key] then if Sync.syncLocks[key] then
local cos = tostring(coroutine.running()) local cos = tostring(coroutine.running())
table.insert(syncLocks[key], cos) table.insert(Sync.syncLocks[key], cos)
repeat repeat
local _, co = os.pullEvent('sync_lock') local _, co = os.pullEvent('sync_lock')
until co == cos until co == cos
else else
syncLocks[key] = { } Sync.syncLocks[key] = { }
end end
local s, m = pcall(fn) local s, m = pcall(fn)
local co = table.remove(syncLocks[key], 1) local co = table.remove(Sync.syncLocks[key], 1)
if co then if co then
os.queueEvent('sync_lock', co) os.queueEvent('sync_lock', co)
else else
syncLocks[key] = nil Sync.syncLocks[key] = nil
end end
if not s then if not s then
error(m) error(m)
end end
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

View File

@ -2959,6 +2959,7 @@ UI.Chooser.defaults = {
choices = { }, choices = { },
nochoice = 'Select', nochoice = 'Select',
backgroundFocusColor = colors.lightGray, backgroundFocusColor = colors.lightGray,
textInactiveColor = colors.gray,
leftIndicator = '<', leftIndicator = '<',
rightIndicator = '>', rightIndicator = '>',
height = 1, height = 1,
@ -2981,13 +2982,14 @@ function UI.Chooser:draw()
if self.focused then if self.focused then
bg = self.backgroundFocusColor bg = self.backgroundFocusColor
end end
local fg = self.inactive and self.textInactiveColor or self.textColor
local choice = Util.find(self.choices, 'value', self.value) local choice = Util.find(self.choices, 'value', self.value)
local value = self.nochoice local value = self.nochoice
if choice then if choice then
value = choice.name value = choice.name
end end
self:write(1, 1, self.leftIndicator, self.backgroundColor, colors.black) 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) self:write(self.width, 1, self.rightIndicator, self.backgroundColor, colors.black)
end end

View File

@ -7,7 +7,7 @@ _G.requireInjector(_ENV)
local Pathing = require('turtle.pathfind') local Pathing = require('turtle.pathfind')
local GPS = require('gps') local GPS = require('gps')
local Point = require('point') local Point = require('point')
local synchronized = require('sync') local synchronized = require('sync').sync
local Util = require('util') local Util = require('util')
local os = _G.os local os = _G.os

View File

@ -34,6 +34,12 @@ _G._debug = function(pattern, ...)
term.redirect(oldTerm) term.redirect(oldTerm)
end 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 -- any function that runs in a kernel hook does not run in
-- a separate coroutine or have a window. an error in a hook -- a separate coroutine or have a window. an error in a hook
-- function will crash the system. -- function will crash the system.