def lock(ltype, type, key, &block)
@mutex.synchronize do
begin
d, f = path_for(type, key)
fn = File.join(d, f)
FileUtils.mkdir_p(d) if ltype == :create && ( ! File.exist?(d))
FileUtils.touch(fn) if ltype == :create && ( ! File.exist?(fn))
file = File.new(fn, 'r+') rescue nil
return false if file.nil?
file.flock(File::LOCK_EX) unless @nolock
if ltype == :write
Thread.pass
21.times { return false unless File.exist?(fn) }
end
block.call(file)
ensure
begin
file.flock(File::LOCK_UN) unless @nolock
rescue Exception => e
end
begin
file.close if file
rescue Exception => e
end
end
end
end