Class: LockFile

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

Direct Known Subclasses

CaskLock, FormulaLock

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ LockFile

Returns a new instance of LockFile



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

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

Instance Method Details

#lockObject



10
11
12
13
14
15
# File 'Library/Homebrew/lock_file.rb', line 10

def lock
  @path.parent.mkpath
  create_lockfile
  return if @lockfile.flock(File::LOCK_EX | File::LOCK_NB)
  raise OperationInProgressError, @name
end

#unlockObject



17
18
19
20
21
# File 'Library/Homebrew/lock_file.rb', line 17

def unlock
  return if @lockfile.nil? || @lockfile.closed?
  @lockfile.flock(File::LOCK_UN)
  @lockfile.close
end

#with_lockObject



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

def with_lock
  lock
  yield
ensure
  unlock
end