Class: Moneta::Semaphore

Inherits:
SynchronizePrimitive show all
Defined in:
lib/moneta/synchronize.rb

Overview

Distributed/shared store-wide semaphore

Examples:

Use `Moneta::Semaphore`

semaphore = Moneta::Semaphore.new(store, 'semaphore', 2)
semaphore.synchronize do
  # Synchronized access
  # ...
end

Instance Method Summary collapse

Methods inherited from SynchronizePrimitive

#enter, #leave, #locked?, #synchronize, #try_enter

Constructor Details

#initialize(store, counter, max = 1) ⇒ Semaphore

Returns a new instance of Semaphore

Parameters:

  • store (Moneta store)

    The store we want to lock

  • counter (Object)

    Key of the counter entry

  • max (Fixnum) (defaults to: 1)

    Maximum number of threads which are allowed to enter the critical section



105
106
107
108
109
# File 'lib/moneta/synchronize.rb', line 105

def initialize(store, counter, max = 1)
  raise 'Store must support feature :increment' unless store.supports?(:increment)
  @store, @counter, @max = store, counter, max
  @store.increment(@counter, 0, expires: false) # Ensure that counter exists
end