Preview Ruby 4.1 is a preview. It can change before the final release. Switch to Ruby 4.0 →
class method
Gem.open_file_with_flock
open_file_with_flock(path) { |io| ... }
lib/rubygems.rb
View on GitHub ↗
# File tmp/rubies/ruby-4.1.0-preview1/lib/rubygems.rb, line 884
def self.open_file_with_flock(path, &block)
# read-write mode is used rather than read-only in order to support NFS
mode = IO::RDWR | IO::APPEND | IO::CREAT | IO::BINARY
mode |= IO::SHARE_DELETE if IO.const_defined?(:SHARE_DELETE)
File.open(path, mode) do |io|
begin
# Try to get a lock without blocking.
# If we do, the file is locked.
# Otherwise, explain why we're waiting and get a lock, but block this time.
if io.flock(File::LOCK_EX | File::LOCK_NB) != 0
warn "Waiting for another process to let go of lock: #{path}"
io.flock(File::LOCK_EX)
end
io.puts(Process.pid)
rescue Errno::ENOSYS, Errno::ENOTSUP
end
yield io
end
end
Open a file with given flags, and protect access with flock