Sha256: 1b0734f9134b66338c0b2a3037847f804a07d6a3dab54a74487875750abec3e5

Contents?: true

Size: 1.09 KB

Versions: 1

Compression:

Stored size: 1.09 KB

Contents

module Celluloid
  # Event signaling between methods of the same object
  class Signals
    attr_reader :waiting

    def initialize
      @waiting = {}
    end

    # Wait for the given signal and return the associated value
    def wait(signal)
      tasks = @waiting[signal]

      case tasks
      when Array
        tasks << Task.current
      when NilClass
        @waiting[signal] = Task.current
      else
        @waiting[signal] = [tasks, Task.current]
      end

      Task.suspend :sigwait
    end

    # Send a signal to all method calls waiting for the given name
    # Returns true if any calls were signaled, or false otherwise
    def send(name, value = nil)
      tasks = @waiting.delete name

      case tasks
      when Array
        tasks.each { |task| run_task task, value }
        true if tasks.size > 0
      when NilClass
        false
      else
        run_task tasks, value
        true
      end
    end

    # Run the given task, reporting errors that occur
    def run_task(task, value)
      task.resume(value)
    rescue => ex
      Logger.crash("signaling error", ex)
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
celluloid-0.8.0 lib/celluloid/signals.rb