Class: DataMapper::GlobalMutex

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

Instance Method Summary collapse

Constructor Details

#initialize(lockfile) ⇒ GlobalMutex

Returns a new instance of GlobalMutex.



15
16
17
18
# File 'lib/base/datamapper_l.rb', line 15

def initialize(lockfile)
  @lockfile = lockfile
  @monitor = Monitor.new
end

Instance Method Details

#synchronizeObject



20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/base/datamapper_l.rb', line 20

def synchronize
  @monitor.synchronize do
    File.open(@lockfile, 'r') do |file|
      # Only Lock/Unlock on first entrance of synchronize to avoid
      # deadlock on flock
      file.flock(File::LOCK_EX) if @monitor.count == 1
      begin
        yield
      ensure
        file.flock(File::LOCK_UN) if @monitor.count == 1
      end
    end
  end
end