Class: ACE::FileMutex

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

Instance Method Summary collapse

Constructor Details

#initialize(lock_file) ⇒ FileMutex

Returns a new instance of FileMutex.



7
8
9
# File 'lib/ace/file_mutex.rb', line 7

def initialize(lock_file)
  @lock_file = lock_file
end

Instance Method Details

#with_read_lockObject



11
12
13
14
15
16
17
18
# File 'lib/ace/file_mutex.rb', line 11

def with_read_lock
  fh = File.open(@lock_file, File::CREAT)
  fh.flock(File::LOCK_SH)
  yield
ensure
  fh.flock(File::LOCK_UN)
  fh.close
end

#with_write_lockObject



20
21
22
23
24
25
26
27
# File 'lib/ace/file_mutex.rb', line 20

def with_write_lock
  fh = File.open(@lock_file, File::CREAT)
  fh.flock(File::LOCK_EX)
  yield
ensure
  fh.flock(File::LOCK_UN)
  fh.close
end