class Sgfa::LockFs
Lock based on file locks
Attributes
state[R]
Lock state
@return [Symbol] :closed, :unlocked, :shared, or :exclusive
Public Class Methods
new()
click to toggle source
Initialize
# File lib/sgfa/lock_fs.rb, line 31 def initialize @file = nil @state = :closed end
Public Instance Methods
close()
click to toggle source
Close lock file
@raise [Error::Sanity] if lock not open
# File lib/sgfa/lock_fs.rb, line 57 def close raise Error::Sanity, 'Lock file not open' if !@file @file.close @file = nil @state = :closed end
do_ex(rel=true) { || ... }
click to toggle source
Run block while holding exclusive lock
@param rel [Boolean] Release lock on return @raise (see exclusive
)
# File lib/sgfa/lock_fs.rb, line 112 def do_ex(rel=true) exclusive begin ret = yield ensure unlock if rel end return ret end
do_sh(rel=true) { || ... }
click to toggle source
Run block while holding shared lock
@param rel [Boolean] Release lock on return @riase (see shared
)
# File lib/sgfa/lock_fs.rb, line 128 def do_sh(rel=true) shared begin ret = yield ensure unlock if rel end return ret end
exclusive()
click to toggle source
Take exclusive lock
@raise [Error::Sanity] if lock not open
# File lib/sgfa/lock_fs.rb, line 69 def exclusive raise Error::Sanity, 'Lock file not open' if !@file if @state == :exclusive return elsif @state == :shared @file.flock(File::LOCK_UN) end @file.flock(File::LOCK_EX) @state = :exclusive end
open(fnam)
click to toggle source
Open lock file
@todo Handle exceptions from File.open()
@param fnam [String] File name of lock file @return [String] Contents of the lock file @raise [Error::Sanity] if lock already open
# File lib/sgfa/lock_fs.rb, line 45 def open(fnam) raise Error::Sanity, 'Lock file already open' if @file @file = File.open(fnam, 'r', :encoding => 'utf-8') @state = :unlocked return @file.read end
unlock()
click to toggle source
Release lock
@raise [Error::Sanity] if lock not open
# File lib/sgfa/lock_fs.rb, line 99 def unlock raise Error::Sanity, 'Lock file not open' if !@file return if @state == :unlocked @file.flock(File::LOCK_UN) @state = :unlocked end