Sha256: 737167a92fc3ba4a5239236b9ce864bd7acbba8fe742504bd15cea12971dbb34

Contents?: true

Size: 965 Bytes

Versions: 12

Compression:

Stored size: 965 Bytes

Contents

module File::Lock
  #
  # File.locked implements recursive locking based on lock files. 
  def locked(path, &block)
    file = File.open "#{path}.lck", "w+"
    begin
      # First try to lock the file nonblockingly.
      # Failing that it might be already locked by *this* process. 
      # Otherwise it is locked by someone else.
      if locked = file.flock(File::LOCK_EX | File::LOCK_NB)
        File.write("#{path}.pid", Thread.uid)
      elsif File.read("#{path}.pid") == Thread.uid
        # already locked by us.
      else
        locked = file.flock(File::LOCK_EX)
      end

      yield
    ensure
      file.flock(File::LOCK_UN) if locked
    end
  end
end

File.extend File::Lock

module File::Lock::Etest
  TESTFILE = "#{__FILE__}.test"

  def test_lock
    i = 1
    File.locked TESTFILE do
      File.locked TESTFILE do
        i = 2
      end
    end
    
    assert_equal(2, i)
  end

  def test_lock_unsuccessful
  end
end if VEX_TEST == "base"

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
vex-0.6.2 lib/vex/base/filesystem/lock.rb
vex-0.4.4 lib/vex/base/filesystem/lock.rb
vex-0.4.2 lib/vex/base/filesystem/lock.rb
vex-0.3.3 lib/vex/base/filesystem/lock.rb
vex-0.2.9 lib/vex/base/filesystem/lock.rb
vex-0.2.8 lib/vex/base/filesystem/lock.rb
vex-0.2.7 lib/vex/base/filesystem/lock.rb
vex-0.2.6 lib/vex/base/filesystem/lock.rb
vex-0.2.5 lib/vex/base/filesystem/lock.rb
vex-0.2.2 lib/vex/base/filesystem/lock.rb
vex-0.2.1 lib/vex/base/filesystem/lock.rb
vex-0.2 lib/vex/base/filesystem/lock.rb