ComputerCraft Archive

sync

computer utility kepler155c github

Description

ComputerCraft OS

Installation

Copy one of these commands into your ComputerCraft terminal:

wget:wget https://raw.githubusercontent.com/kepler155c/opus/develop-1.8/sys/modules/opus/sync.lua sync
Archive:wget https://cc.shobie.xyz/cc/get/gh-kepler155c-opus-sys-modules-opus-sync sync
Quick Install: wget https://cc.shobie.xyz/cc/get/gh-kepler155c-opus-sys-modules-opus-sync sync

Usage

Run: sync

Tags

none

Source

View Original Source

Code Preview

local Sync = {
	syncLocks = { }
}

local os = _G.os

function Sync.sync(obj, fn)
	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
	local s, m = pcall(fn)
	local co = table.remove(Sync.syncLocks[key], 1)
	if co then
		os.queueEvent('sync_lock', co)
	else
		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