Class: FlockMutex

Inherits:
Object
  • Object
show all
Defined in:
lib/flock_mutex.rb

Instance Method Summary collapse

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

#lockObject



6
7
8
9
# File 'lib/flock_mutex.rb', line 6

def lock
  @file.flock(File::LOCK_EX)
  self
end

#locked?Boolean

Returns:

  • (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

#synchronizeObject



16
17
18
19
20
21
# File 'lib/flock_mutex.rb', line 16

def synchronize
  lock
  yield
ensure
  unlock
end

#unlockObject



11
12
13
14
# File 'lib/flock_mutex.rb', line 11

def unlock
  @file.flock(File::LOCK_UN)
  self
end