Class: Xxeo::LockFile
- Inherits:
-
Object
- Object
- Xxeo::LockFile
- Defined in:
- lib/lockfile.rb
Instance Method Summary collapse
-
#initialize(pathname, options = {}) ⇒ LockFile
constructor
A new instance of LockFile.
-
#lock ⇒ Object
We allow a process that holds a lock to recursively acquire the lock, the allows the higher level programs to not have to keep track of the lock status This is becase the flock mechanism doesn’t do reference counting.
- #lock_count ⇒ Object
- #locked? ⇒ Boolean
- #unlock ⇒ Object
Constructor Details
#initialize(pathname, options = {}) ⇒ LockFile
Returns a new instance of LockFile.
31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/lockfile.rb', line 31 def initialize(pathname, = {}) [:env] ||= 'development' raise "lockfile not writable" if not File.writable? pathname @f = File.open(pathname, 'r') raise "could not open file" if not @f @lock_count = 0 end |
Instance Method Details
#lock ⇒ Object
We allow a process that holds a lock to recursively acquire the lock, the allows the higher level programs to not have to keep track of the lock status This is becase the flock mechanism doesn’t do reference counting.
49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/lockfile.rb', line 49 def lock # already locked, just increase the ref count if @lock_count > 0 @lock_count += 1 return end # THIS COULD BLOCK @f.flock(File::LOCK_EX) @lock_count = 1 end |
#lock_count ⇒ Object
82 83 84 |
# File 'lib/lockfile.rb', line 82 def lock_count return @lock_count end |
#locked? ⇒ Boolean
78 79 80 |
# File 'lib/lockfile.rb', line 78 def locked? return @lock_count > 0 end |
#unlock ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/lockfile.rb', line 61 def unlock if @lock_count > 1 @lock_count -= 1 return end if @lock_count == 1 @f.flock(File::LOCK_UN) @lock_count -= 1 return end if @lock_count < 1 raise "Attempt to unlock an unlocked LockFile" end end |