Files

Class/Module Index [+]

Quicksearch

Moneta::Semaphore

Distributed/shared store-wide semaphore

@example Use `Moneta::Semaphore`

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

@api public

Public Class Methods

new(store, counter, max = 1) click to toggle source

@param [Moneta store] store The store we want to lock @param [Object] counter Key of the counter entry @param [Fixnum] max Maximum number of threads which are allowed to enter the critical section

# File lib/moneta/synchronize.rb, line 106
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

Protected Instance Methods

enter_primitive() click to toggle source
# File lib/moneta/synchronize.rb, line 114
def enter_primitive
  if @store.increment(@counter, 1) <= @max
    true
  else
    @store.decrement(@counter)
    false
  end
end
leave_primitive() click to toggle source
# File lib/moneta/synchronize.rb, line 123
def leave_primitive
  @store.decrement(@counter)
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.