This class implements semaphore for threads synchronization.
Initialize new semaphore
[Source]
# File lib/xmpp4r/semaphore.rb, line 14 14: def initialize(val=0) 15: @tickets = val 16: @lock = Mutex.new 17: @cond = ConditionVariable.new 18: end
Unlocks guarded section, increments number of free tickets
# File lib/xmpp4r/semaphore.rb, line 31 31: def run 32: @lock.synchronize { 33: @tickets += 1 34: @cond.signal 35: } 36: end
Waits until are available some free tickets
# File lib/xmpp4r/semaphore.rb, line 22 22: def wait 23: @lock.synchronize { 24: @cond.wait(@lock) while !(@tickets > 0) 25: @tickets -= 1 26: } 27: end
[Validate]