class 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 [Integer] max Maximum number of threads which are allowed to enter the critical section
# File lib/moneta/synchronize.rb, line 99 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 107 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 116 def leave_primitive @store.decrement(@counter) end