class Bunny::Concurrent::Condition

Akin to java.util.concurrent.Condition and intrinsic object monitors (Object#wait, Object#notify, Object#notifyAll) in Java: threads can wait (block until notified) on a condition other threads notify them about. Unlike the j.u.c. version, this one has a single waiting set.

Conditions can optionally be annotated with a description string for ease of debugging. @private

Attributes

description[R]
waiting_threads[R]

Public Class Methods

new(description = nil) click to toggle source
# File lib/bunny/concurrent/condition.rb, line 17
def initialize(description = nil)
  @mutex           = Monitor.new
  @waiting_threads = []
  @description     = description
end

Public Instance Methods

any_threads_waiting?() click to toggle source
# File lib/bunny/concurrent/condition.rb, line 57
def any_threads_waiting?
  @mutex.synchronize { !@waiting_threads.empty? }
end
none_threads_waiting?() click to toggle source
# File lib/bunny/concurrent/condition.rb, line 61
def none_threads_waiting?
  @mutex.synchronize { @waiting_threads.empty? }
end
notify() click to toggle source
# File lib/bunny/concurrent/condition.rb, line 32
def notify
  @mutex.synchronize do
    t = @waiting_threads.shift
    begin
      t.run if t
    rescue ThreadError
      retry
    end
  end
end
notify_all() click to toggle source
# File lib/bunny/concurrent/condition.rb, line 43
def notify_all
  @mutex.synchronize do
    @waiting_threads.each do |t|
      t.run
    end

    @waiting_threads.clear
  end
end
wait() click to toggle source
# File lib/bunny/concurrent/condition.rb, line 23
def wait
  @mutex.synchronize do
    t = Thread.current
    @waiting_threads.push(t)
  end

  Thread.stop
end
waiting_set_size() click to toggle source
# File lib/bunny/concurrent/condition.rb, line 53
def waiting_set_size
  @mutex.synchronize { @waiting_threads.size }
end