module Celluloid - celluloid-0.15.2 Documentation

# module Celluloid

### Constants

BARE_OBJECT_WARNING_MESSAGE

  Warning message added to Celluloid objects accessed outside their actors

Error
LINKING_TIMEOUT
OWNER_IVAR
TIMER_QUANTUM

  Timer accuracy enforced by the tests (50ms)

TaskSet

  Assume we're on MRI, where we have the GIL. But what about IronRuby? Or MacRuby. Do people care? This will break Celluloid::StackDumps

VERSION

### Attributes

internal_pool[RW]
logger[RW]
shutdown_timeout[RW]
task_class[RW]

### Public Instance Methods

actor?() click to toggle source

Are we currently inside of an actor?

    # File lib/celluloid.rb, line 49
    def actor?
      !!Thread.current[:celluloid_actor]
    end

boot() click to toggle source

    # File lib/celluloid.rb, line 103
    def boot
      init
      start
    end

cores() click to toggle source

Obtain the number of CPUs in the system

    # File lib/celluloid.rb, line 64
    def cores
     CPUCounter.cores
    end

detect_recursion() click to toggle source

Detect if a particular call is recursing through multiple actors

    # File lib/celluloid.rb, line 77
    def detect_recursion
      actor = Thread.current[:celluloid_actor]
      return unless actor

      task = Thread.current[:celluloid_task]
      return unless task

      chain_id = CallChain.current_id
      actor.tasks.to_a.any? { |t| t != task && t.chain_id == chain_id }
    end

exception_handler(&block) click to toggle source

Define an exception handler for actor crashes

    # File lib/celluloid.rb, line 89
    def exception_handler(&block)
      Logger.exception_handler(&block)
    end

included(klass) click to toggle source

    # File lib/celluloid.rb, line 25
    def included(klass)
      klass.send :extend,  ClassMethods
      klass.send :include, InstanceMethods

      klass.send :extend, Properties

      klass.property :mailbox_class, :default => Celluloid::Mailbox
      klass.property :proxy_class,   :default => Celluloid::ActorProxy
      klass.property :task_class,    :default => Celluloid.task_class
      klass.property :mailbox_size

      klass.property :execute_block_on_receiver,
        :default => [:after, :every, :receive],
        :multi   => true

      klass.property :finalizer
      klass.property :exit_handler

      klass.send(:define_singleton_method, :trap_exit) do |*args|
        exit_handler(*args)
      end
    end

init() click to toggle source

    # File lib/celluloid.rb, line 108
    def init
      self.internal_pool = InternalPool.new
    end

mailbox() click to toggle source

Retrieve the mailbox for the current thread or lazily initialize it

    # File lib/celluloid.rb, line 54
    def mailbox
      Thread.current[:celluloid_mailbox] ||= Celluloid::Mailbox.new
    end

register_shutdown() click to toggle source

    # File lib/celluloid.rb, line 123
    def register_shutdown
      return if @shutdown_registered
      # Terminate all actors at exit
      at_exit do
        if defined?(RUBY_ENGINE) && RUBY_ENGINE == "ruby" && RUBY_VERSION >= "1.9"
          # workaround for MRI bug losing exit status in at_exit block
          # http://bugs.ruby-lang.org/issues/5218
          exit_status = $!.status if $!.is_a?(SystemExit)
          Celluloid.shutdown
          exit exit_status if exit_status
        else
          Celluloid.shutdown
        end
      end
      @shutdown_registered = true
    end

running?() click to toggle source

    # File lib/celluloid.rb, line 119
    def running?
      internal_pool
    end

shutdown() click to toggle source

Shut down all running actors

    # File lib/celluloid.rb, line 141
    def shutdown
      actors = Actor.all

      Timeout.timeout(shutdown_timeout) do
        internal_pool.shutdown

        Logger.debug "Terminating #{actors.size} #{(actors.size > 1) ? 'actors' : 'actor'}..." if actors.size > 0

        # Attempt to shut down the supervision tree, if available
        Supervisor.root.terminate if Supervisor.root

        # Actors cannot self-terminate, you must do it for them
        actors.each do |actor|
          begin
            actor.terminate!
          rescue DeadActorError
          end
        end

        actors.each do |actor|
          begin
            Actor.join(actor)
          rescue DeadActorError
          end
        end
      end
    rescue Timeout::Error
      Logger.error("Couldn't cleanly terminate all actors in #{shutdown_timeout} seconds!")
      actors.each do |actor|
        begin
          Actor.kill(actor)
        rescue DeadActorError, MailboxDead
        end
      end
    ensure
      internal_pool.kill
    end

stack_dump(output = STDERR) click to toggle source

Perform a stack dump of all actors to the given output object

    # File lib/celluloid.rb, line 71
    def stack_dump(output = STDERR)
      Celluloid::StackDump.new.dump(output)
    end

start() click to toggle source

Launch default services FIXME: We should set up the supervision hierarchy here

    # File lib/celluloid.rb, line 114
    def start
      Celluloid::Notifications::Fanout.supervise_as :notifications_fanout
      Celluloid::IncidentReporter.supervise_as :default_incident_reporter, STDERR
    end

suspend(status, waiter) click to toggle source

    # File lib/celluloid.rb, line 93
    def suspend(status, waiter)
      task = Thread.current[:celluloid_task]
      if task && !Celluloid.exclusive?
        waiter.before_suspend(task) if waiter.respond_to?(:before_suspend)
        Task.suspend(status)
      else
        waiter.wait
      end
    end

uuid() click to toggle source

Generate a Universally Unique Identifier

    # File lib/celluloid.rb, line 59
    def uuid
      UUID.generate
    end

version() click to toggle source

    # File lib/celluloid.rb, line 179
    def version
      VERSION
    end