Class: Pecorino::Adapters::MemoryAdapter::KeyedLock

Inherits:
Object
  • Object
show all
Defined in:
lib/pecorino/adapters/memory_adapter.rb

Instance Method Summary collapse

Constructor Details

#initializeKeyedLock

Returns a new instance of KeyedLock.



6
7
8
9
# File 'lib/pecorino/adapters/memory_adapter.rb', line 6

def initialize
  @locked_keys = Set.new
  @lock_mutex = Mutex.new
end

Instance Method Details

#lock(key) ⇒ Object



11
12
13
14
15
16
17
18
19
# File 'lib/pecorino/adapters/memory_adapter.rb', line 11

def lock(key)
  loop do
    @lock_mutex.synchronize do
      next if @locked_keys.include?(key)
      @locked_keys << key
      return
    end
  end
end

#unlock(key) ⇒ Object



21
22
23
24
25
# File 'lib/pecorino/adapters/memory_adapter.rb', line 21

def unlock(key)
  @lock_mutex.synchronize do
    @locked_keys.delete(key)
  end
end

#with(key) ⇒ Object



27
28
29
30
31
32
# File 'lib/pecorino/adapters/memory_adapter.rb', line 27

def with(key)
  lock(key)
  yield
ensure
  unlock(key)
end