This class implements semaphore for threads synchronization.
Initialize new semaphore
number of threads, that can enter to section
# File lib/xmpp4r/semaphore.rb, line 14 def initialize(val=0) @tickets = val @lock = Mutex.new @cond = ConditionVariable.new end
Unlocks guarded section, increments number of free tickets
# File lib/xmpp4r/semaphore.rb, line 31 def run @lock.synchronize { @tickets += 1 @cond.signal } end
Waits until are available some free tickets
# File lib/xmpp4r/semaphore.rb, line 22 def wait @lock.synchronize { @cond.wait(@lock) while !(@tickets > 0) @tickets -= 1 } end