Sha256: 60083dc023f77960a366d5e469a6c6e0ebb965615ba515cfe87d64239921fedf

Contents?: true

Size: 1.17 KB

Versions: 27

Compression:

Stored size: 1.17 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)
      raise "cannot wait for signals while exclusive" if Celluloid.exclusive?

      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

27 entries across 27 versions & 3 rubygems

Version Path
celluloid-0.14.1 lib/celluloid/signals.rb
celluloid-0.14.1.pre lib/celluloid/signals.rb
sidekiq-statsd-0.1.1 vendor/ruby/1.9.1/gems/celluloid-0.14.0/lib/celluloid/signals.rb
sidekiq-statsd-0.1.0 vendor/ruby/1.9.1/gems/celluloid-0.14.0/lib/celluloid/signals.rb
celluloid-0.14.0 lib/celluloid/signals.rb
celluloid-0.14.0.pre lib/celluloid/signals.rb
celluloid-0.13.0 lib/celluloid/signals.rb
celluloid-0.13.0.pre2 lib/celluloid/signals.rb
celluloid-0.13.0.pre lib/celluloid/signals.rb
celluloid-0.12.4 lib/celluloid/signals.rb
celluloid-0.12.4.pre2 lib/celluloid/signals.rb
celluloid-0.12.4.pre lib/celluloid/signals.rb
celluloid-0.12.3 lib/celluloid/signals.rb
celluloid-0.12.2 lib/celluloid/signals.rb
celluloid-0.12.1 lib/celluloid/signals.rb
celluloid-0.12.1.pre2 lib/celluloid/signals.rb
celluloid-0.12.1.pre lib/celluloid/signals.rb
celluloid-0.12.0 lib/celluloid/signals.rb
celluloid-0.12.0.pre3 lib/celluloid/signals.rb
celluloid-0.12.0.pre2 lib/celluloid/signals.rb