class Moneta::SynchronizePrimitive
Base class for {Mutex} and {Semaphore} @abstract
Public Instance Methods
enter(timeout = nil, wait = 0.01)
click to toggle source
Enter critical section (blocking)
@param [Number] timeout Maximum time to wait @param [Number] wait Sleep time between tries to acquire lock @return [Boolean] true if the lock was aquired
# File lib/moneta/synchronize.rb, line 31 def enter(timeout = nil, wait = 0.01) time_at_timeout = Time.now + timeout if timeout while !timeout || Time.now < time_at_timeout return true if try_enter sleep(wait) end false end
Also aliased as: lock
leave()
click to toggle source
Leave critical section
# File lib/moneta/synchronize.rb, line 42 def leave raise 'Not locked' unless @locked leave_primitive @locked = false nil end
Also aliased as: unlock
locked?()
click to toggle source
Is the lock acquired?
# File lib/moneta/synchronize.rb, line 51 def locked? @locked end
synchronize() { || ... }
click to toggle source
Synchronize block
@api public @yieldparam Synchronized block @return [Object] result of block
# File lib/moneta/synchronize.rb, line 10 def synchronize enter yield ensure leave end
try_enter()
click to toggle source
Try to enter critical section (nonblocking)
@return [Boolean] true if the lock was acquired
# File lib/moneta/synchronize.rb, line 20 def try_enter raise 'Already locked' if @locked enter_primitive ? @locked = true : false end
Also aliased as: try_lock