Class: Celluloid::Mailbox

### Home Classes Methods

### In Files

- lib/celluloid/mailbox.rb

### Parent

Object

### Methods

- ::new
- #<<
- #alive?
- #check
- #each
- #inspect
- #receive
- #shutdown
- #size
- #to_a

### Included Modules

- Enumerable

### Class/Module Index

Quicksearch

- Celluloid
- Celluloid::AbortError
- Celluloid::AbstractProxy
- Celluloid::Actor
- Celluloid::Actor::Sleeper
- Celluloid::ActorProxy
- Celluloid::ActorSystem
- Celluloid::AsyncCall
- Celluloid::AsyncProxy
- Celluloid::BlockCall
- Celluloid::BlockProxy
- Celluloid::BlockResponse
- Celluloid::CPUCounter
- Celluloid::Call
- Celluloid::CallChain
- Celluloid::Cell
- Celluloid::Cell::ExitHandler
- Celluloid::CellProxy
- Celluloid::ClassMethods
- Celluloid::Condition
- Celluloid::Condition::Waiter
- Celluloid::ConditionError
- Celluloid::DeadTaskError
- Celluloid::ErrorResponse
- Celluloid::EventedMailbox
- Celluloid::ExitEvent
- Celluloid::FSM
- Celluloid::FSM::ClassMethods
- Celluloid::FSM::State
- Celluloid::FSM::UnattachedError
- Celluloid::FiberStackError
- Celluloid::Future
- Celluloid::Future::Result
- Celluloid::FutureProxy
- Celluloid::Handler
- Celluloid::Handlers
- Celluloid::Incident
- Celluloid::IncidentLogger
- Celluloid::IncidentLogger::Severity
- Celluloid::IncidentReporter
- Celluloid::IncidentReporter::Formatter
- Celluloid::InstanceMethods
- Celluloid::InternalPool
- Celluloid::LinkingRequest
- Celluloid::LinkingResponse
- Celluloid::Links
- Celluloid::LogEvent
- Celluloid::Logger
- Celluloid::Logger::WithBacktrace
- Celluloid::Mailbox
- Celluloid::MailboxDead
- Celluloid::MailboxShutdown
- Celluloid::Method
- Celluloid::NamingRequest
- Celluloid::NotTaskError
- Celluloid::Notifications
- Celluloid::Notifications::Fanout
- Celluloid::Notifications::Subscriber
- Celluloid::PoolManager
- Celluloid::Probe
- Celluloid::Properties
- Celluloid::Receiver
- Celluloid::Receivers
- Celluloid::Registry
- Celluloid::Response
- Celluloid::ResumableError
- Celluloid::RingBuffer
- Celluloid::SignalConditionRequest
- Celluloid::Signals
- Celluloid::StackDump
- Celluloid::StackDump::ActorState
- Celluloid::StackDump::CellState
- Celluloid::StackDump::DisplayBacktrace
- Celluloid::StackDump::TaskState
- Celluloid::StackDump::ThreadState
- Celluloid::SuccessResponse
- Celluloid::SupervisionGroup
- Celluloid::SupervisionGroup::Member
- Celluloid::Supervisor
- Celluloid::SyncCall
- Celluloid::SyncProxy
- Celluloid::SystemEvent
- Celluloid::Task
- Celluloid::Task::TerminatedError
- Celluloid::Task::TimeoutError
- Celluloid::TaskFiber
- Celluloid::TaskSet
- Celluloid::TaskThread
- Celluloid::TerminationRequest
- Celluloid::Thread
- Celluloid::ThreadHandle
- Celluloid::UUID
- org
- org::jruby
- org::jruby::ext
- org::jruby::ext::fiber
- org::jruby::ext::fiber::ThreadFiber
- Bar
- Baz
- Blocker
- ExampleActorClass
- ExampleCrash
- ExampleMailbox
- ExampleProxy
- ExampleTask
- Fiber
- Foo
- MockActor
- Object
- Thread

No matching classes.

# Celluloid::Mailbox

Actors communicate with asynchronous messages. Messages are buffered in Mailboxes until Actors can act upon them.

### Attributes

address[R]

A unique address at which this mailbox can be found

max_size[RW]

### Public Class Methods

new() click to toggle source

    # File lib/celluloid/mailbox.rb, line 16
    def initialize
      @address   = Celluloid.uuid
      @messages  = []
      @mutex     = Mutex.new
      @dead      = false
      @condition = ConditionVariable.new
      @max_size  = nil
    end

### Public Instance Methods

<<(message) click to toggle source

Add a message to the Mailbox

    # File lib/celluloid/mailbox.rb, line 26
    def <<(message)
      @mutex.lock
      begin
        if mailbox_full || @dead
          dead_letter(message)
          return
        end
        if message.is_a?(SystemEvent)
          # SystemEvents are high priority messages so they get added to the
          # head of our message queue instead of the end
          @messages.unshift message
        else
          @messages << message
        end

        @condition.signal
        nil
      ensure
        @mutex.unlock rescue nil
      end
    end

alive?() click to toggle source

Is the mailbox alive?

    # File lib/celluloid/mailbox.rb, line 105
    def alive?
      !@dead
    end

check(timeout = nil, &block) click to toggle source

Receive a message from the Mailbox. May return nil and may return before the specified timeout.

    # File lib/celluloid/mailbox.rb, line 50
    def check(timeout = nil, &block)
      message = nil

      @mutex.lock
      begin
        raise MailboxDead, "attempted to receive from a dead mailbox" if @dead

        Timers::Wait.for(timeout) do |remaining|
          message = next_message(&block)

          break message if message

          @condition.wait(@mutex, remaining)
        end
      ensure
        @mutex.unlock rescue nil
      end

      return message
    end

each(&block) click to toggle source

Iterate through the mailbox

    # File lib/celluloid/mailbox.rb, line 115
    def each(&block)
      to_a.each(&block)
    end

inspect() click to toggle source

Inspect the contents of the Mailbox

    # File lib/celluloid/mailbox.rb, line 120
    def inspect
      "#<#{self.class}:#{object_id.to_s(16)} @messages=[#{map { |m| m.inspect }.join(', ')}]>"
    end

receive(timeout = nil, &block) click to toggle source

Receive a letter from the mailbox. Guaranteed to return a message. If timeout is exceeded, raise a TimeoutError.

    # File lib/celluloid/mailbox.rb, line 73
    def receive(timeout = nil, &block)
      Timers::Wait.for(timeout) do |remaining|
        if message = check(timeout, &block)
          return message
        end
      end

      raise TimeoutError.new("receive timeout exceeded")
    end

shutdown() click to toggle source

Shut down this mailbox and clean up its contents

    # File lib/celluloid/mailbox.rb, line 84
    def shutdown
      raise MailboxDead, "mailbox already shutdown" if @dead

      @mutex.lock
      begin
        yield if block_given?
        messages = @messages
        @messages = []
        @dead = true
      ensure
        @mutex.unlock rescue nil
      end

      messages.each do |msg|
        dead_letter msg
        msg.cleanup if msg.respond_to? :cleanup
      end
      true
    end

size() click to toggle source

Number of messages in the Mailbox

    # File lib/celluloid/mailbox.rb, line 125
    def size
      @mutex.synchronize { @messages.size }
    end

to_a() click to toggle source

Cast to an array

    # File lib/celluloid/mailbox.rb, line 110
    def to_a
      @mutex.synchronize { @messages.dup }
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.