Class: LockFile
- Inherits:
-
Object
- Object
- LockFile
- Defined in:
- lib/lockf.rb,
lib/lockf/version.rb
Overview
Constant Summary collapse
- VERSION =
"1.0.0"
Instance Attribute Summary collapse
-
#file ⇒ <File, Tempfile, #fileno>
readonly
Returns a file object.
Class Method Summary collapse
-
.from_temporary_file(basename: "lockf", tmpdir: Dir.tmpdir) ⇒ LockFile
(also: temporary_file)
Returns an instance of LockFile backed by an unlinked instance of Tempfile.
-
.lockf(fd, cmd, len) ⇒ Integer
Returns 0 on success.
Instance Method Summary collapse
-
#close ⇒ void
Closes LockFile#file.
-
#initialize(file, len = 0) ⇒ LockFile
constructor
Returns an instance of LockFile.
-
#lock ⇒ Integer
Acquire a lock (blocking).
-
#lock_nonblock ⇒ Integer
Acquire a lock (non-blocking).
-
#release ⇒ Integer
Release a lock.
-
#locked? ⇒ Boolean
Returns true when a lock has been acquired by another process.
Constructor Details
Instance Attribute Details
#file ⇒ <File, Tempfile, #fileno> (readonly)
Returns a file object.
64 65 66 |
# File 'lib/lockf.rb', line 64 def file @file end |
Class Method Details
.from_temporary_file(basename: "lockf", tmpdir: Dir.tmpdir) ⇒ LockFile Also known as: temporary_file
Returns an instance of LockFile backed by an unlinked instance of Tempfile.
52 53 54 55 56 |
# File 'lib/lockf.rb', line 52 def self.from_temporary_file(basename: "lockf", tmpdir: Dir.tmpdir) require "tempfile" unless defined?(Tempfile) file = Tempfile.new(basename, tmpdir:).tap(&:unlink) LockFile.new(file) end |
.lockf(fd, cmd, len) ⇒ Integer
Returns 0 on success.
|
# File 'lib/lockf.rb', line 10
|
Instance Method Details
#close ⇒ void
This method returns an undefined value.
Closes LockFile#file.
138 139 140 141 |
# File 'lib/lockf.rb', line 138 def close return unless @file.respond_to?(:close) @file.close end |
#lock ⇒ Integer
Acquire a lock (blocking).
89 90 91 92 93 94 95 |
# File 'lib/lockf.rb', line 89 def lock attempts ||= 0 LockFile.lockf(@file.fileno, F_LOCK, @len) rescue Errno::EINTR => ex attempts += 1 attempts == 3 ? raise(ex) : retry end |
#lock_nonblock ⇒ Integer
Acquire a lock (non-blocking).
105 106 107 |
# File 'lib/lockf.rb', line 105 def lock_nonblock LockFile.lockf(@file.fileno, F_TLOCK, @len) end |