Sha256: 86de02e9cd6ad4034a8b3a4bfa82dcc49afe46afa1b99f8c4fd46804cc030335

Contents?: true

Size: 1.88 KB

Versions: 1

Compression:

Stored size: 1.88 KB

Contents

module Celluloid
  module IO
    # An alternative implementation of Celluloid::Mailbox using Reactor
    class Mailbox < Celluloid::Mailbox
      attr_reader :reactor

      def initialize(reactor = nil)
        @messages = []
        @mutex = Mutex.new
        @reactor = reactor || Reactor.new
      end

      # Add a message to the Mailbox
      def <<(message)
        @mutex.lock
        begin
          @messages << message
          current_actor = Thread.current[:actor]
          @reactor.wakeup unless current_actor && current_actor.mailbox == self
        rescue IOError
          raise MailboxError, "dead recipient"
        ensure @mutex.unlock
        end
        nil
      end

      # Add a high-priority system event to the Mailbox
      def system_event(event)
        @mutex.lock
        begin
          @messages.unshift event
          current_actor = Thread.current[:actor]
          @reactor.wakeup unless current_actor && current_actor.mailbox == self
        rescue IOError
          # Silently fail if messages are sent to dead actors
        ensure @mutex.unlock
        end
        nil
      end

      # Receive a message from the Mailbox
      def receive(timeout = nil, &block)
        message = next_message(&block)

        until message
          if timeout
            now = Time.now
            wait_until ||= now + timeout
            wait_interval = wait_until - now
            return if wait_interval < 0
          else
            wait_interval = nil
          end
          
          @reactor.run_once(wait_interval)
          message = next_message(&block)
        end

        message
      rescue IOError
        shutdown # force shutdown of the mailbox
        raise MailboxShutdown, "mailbox shutdown called during receive"
      end

      # Cleanup any IO objects this Mailbox may be using
      def shutdown
        @reactor.shutdown
        super
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
celluloid-io-0.10.0 lib/celluloid/io/mailbox.rb