Class: Crimson::Tools::FileMutationQueue
- Inherits:
-
Object
- Object
- Crimson::Tools::FileMutationQueue
- 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
-
#initialize ⇒ FileMutationQueue
constructor
A new instance of FileMutationQueue.
-
#with_file(path) { ... } ⇒ Object
Execute a block with exclusive access to the given file.
Constructor Details
#initialize ⇒ FileMutationQueue
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.
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.(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 |