Class: Crimson::Tools::FileMutationQueue

Inherits:
Object
  • Object
show all
Defined in:
lib/crimson/tools/file_mutation_queue.rb

Overview

Per-file mutex queue to serialize write/edit operations on the same path.

Instance Method Summary collapse

Constructor Details

#initializeFileMutationQueue

Returns a new instance of FileMutationQueue.



7
8
9
10
# File 'lib/crimson/tools/file_mutation_queue.rb', line 7

def initialize
  @queues = {}
  @global_mutex = Mutex.new
end

Instance Method Details

#with_file(path) { ... } ⇒ Object

Execute a block with exclusive access to the given file.

Parameters:

  • path (String)

    file path

Yields:

  • block to run under the file’s mutex

Returns:

  • (Object)

    the block’s result



16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/crimson/tools/file_mutation_queue.rb', line 16

def with_file(path)
  normalized = File.expand_path(path)
  queue = @global_mutex.synchronize do
    @queues[normalized] ||= Mutex.new
  end

  queue.synchronize { yield }
ensure
  @global_mutex.synchronize do
    @queues.delete(normalized) if queue && !queue.locked?
  end
end