Class: LockFile

Inherits:
Object
  • Object
show all
Defined in:
brew/Library/Homebrew/lock_file.rb

Direct Known Subclasses

CaskLock, FormulaLock

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ LockFile

Returns a new instance of LockFile



8
9
10
11
12
# File 'brew/Library/Homebrew/lock_file.rb', line 8

def initialize(name)
  @name = name.to_s
  @path = HOMEBREW_LOCKS/"#{@name}.lock"
  @lockfile = nil
end

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path



6
7
8
# File 'brew/Library/Homebrew/lock_file.rb', line 6

def path
  @path
end

Instance Method Details

#lockObject



14
15
16
17
18
19
20
# File 'brew/Library/Homebrew/lock_file.rb', line 14

def lock
  @path.parent.mkpath
  create_lockfile
  return if @lockfile.flock(File::LOCK_EX | File::LOCK_NB)

  raise OperationInProgressError, @name
end

#unlockObject



22
23
24
25
26
27
# File 'brew/Library/Homebrew/lock_file.rb', line 22

def unlock
  return if @lockfile.nil? || @lockfile.closed?

  @lockfile.flock(File::LOCK_UN)
  @lockfile.close
end

#with_lockObject



29
30
31
32
33
34
# File 'brew/Library/Homebrew/lock_file.rb', line 29

def with_lock
  lock
  yield
ensure
  unlock
end