Class: FlockMutex
- Inherits:
-
Object
- Object
- FlockMutex
- Defined in:
- lib/flock_mutex.rb
Instance Method Summary collapse
-
#initialize(path) ⇒ FlockMutex
constructor
A new instance of FlockMutex.
- #lock ⇒ Object
- #locked? ⇒ Boolean
- #synchronize ⇒ Object
- #unlock ⇒ Object
Constructor Details
#initialize(path) ⇒ FlockMutex
Returns a new instance of FlockMutex.
2 3 4 |
# File 'lib/flock_mutex.rb', line 2 def initialize(path) @file = File.open(path, 'a') end |
Instance Method Details
#lock ⇒ Object
6 7 8 9 |
# File 'lib/flock_mutex.rb', line 6 def lock @file.flock(File::LOCK_EX) self end |
#locked? ⇒ Boolean
23 24 25 26 27 |
# File 'lib/flock_mutex.rb', line 23 def locked? File.open(@file.path, 'a') do |f| ! f.flock(File::LOCK_EX | File::LOCK_NB) end end |
#synchronize ⇒ Object
16 17 18 19 20 21 |
# File 'lib/flock_mutex.rb', line 16 def synchronize lock yield ensure unlock end |
#unlock ⇒ Object
11 12 13 14 |
# File 'lib/flock_mutex.rb', line 11 def unlock @file.flock(File::LOCK_UN) self end |