Base class for {Mutex} and {Semaphore} @api private
Enter critical section (blocking)
@api public @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 33 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
Leave critical section
@api public
# File lib/moneta/synchronize.rb, line 46 def leave raise 'Not locked' unless @locked leave_primitive @locked = false nil end
Is the lock acquired?
@api public
# File lib/moneta/synchronize.rb, line 57 def locked? @locked end
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 to enter critical section (nonblocking)
@api public @return [Boolean] true if the lock was acquired
# File lib/moneta/synchronize.rb, line 21 def try_enter raise 'Already locked' if @locked enter_primitive ? @locked = true : false end