class Celluloid::Condition - celluloid-0.15.2 Documentation

# class Celluloid::Condition

ConditionVariable-like signaling between tasks and threads

### Public Class Methods

new() click to toggle source

    # File lib/celluloid/condition.rb, line 26
    def initialize
      @mutex = Mutex.new
      @waiters = []
    end

### Public Instance Methods

broadcast(value = nil) click to toggle source

Broadcast a value to all waiting tasks and threads

    # File lib/celluloid/condition.rb, line 63
    def broadcast(value = nil)
      @mutex.synchronize do
        @waiters.each { |waiter| waiter << SignalConditionRequest.new(waiter.task, value) }
        @waiters.clear
      end
    end

signal(value = nil) click to toggle source

Send a signal to the first task waiting on this condition

    # File lib/celluloid/condition.rb, line 52
    def signal(value = nil)
      @mutex.synchronize do
        if waiter = @waiters.shift
          waiter << SignalConditionRequest.new(waiter.task, value)
        else
          Logger.debug("Celluloid::Condition signaled spuriously")
        end
      end
    end

wait() click to toggle source

Wait for the given signal and return the associated value

    # File lib/celluloid/condition.rb, line 32
    def wait
      raise ConditionError, "cannot wait for signals while exclusive" if Celluloid.exclusive?

      if Thread.current[:celluloid_actor]
        task = Task.current
      else
        task = Thread.current
      end
      waiter = Waiter.new(self, task, Celluloid.mailbox)

      @mutex.synchronize do
        @waiters << waiter
      end

      result = Celluloid.suspend :condwait, waiter
      raise result if result.is_a? ConditionError
      result
    end