class Jabber::Semaphore

This class implements semaphore for threads synchronization.

Public Class Methods

new(val=0) click to toggle source

Initialize new semaphore

val
Integer

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

Public Instance Methods

run() click to toggle source

Unlocks guarded section, increments number of free tickets

# File lib/xmpp4r/semaphore.rb, line 31
def run
  @lock.synchronize {
    @tickets += 1
    @cond.signal
  }
end
wait() click to toggle source

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