Class: Celluloid::Mailbox

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/celluloid/mailbox.rb

Overview

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

Instance Method Summary collapse

Constructor Details

#initializeMailbox

Returns a new instance of Mailbox.



15
16
17
18
19
20
# File 'lib/celluloid/mailbox.rb', line 15

def initialize
  @messages = []
  @mutex = Mutex.new
  @dead = false
  @condition = ConditionVariable.new
end

Instance Method Details

#<<(message) ⇒ Object

Add a message to the Mailbox



23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/celluloid/mailbox.rb', line 23

def <<(message)
  @mutex.lock
  begin
    raise MailboxError, "dead recipient" if @dead

    @messages << message
    @condition.signal
    nil
  ensure
    @mutex.unlock rescue nil
  end
end

#alive?Boolean

Is the mailbox alive?

Returns:

  • (Boolean)


115
116
117
# File 'lib/celluloid/mailbox.rb', line 115

def alive?
  !@dead
end

#each(&block) ⇒ Object

Iterate through the mailbox



125
126
127
# File 'lib/celluloid/mailbox.rb', line 125

def each(&block)
  to_a.each(&block)
end

#inspectObject

Inspect the contents of the Mailbox



130
131
132
# File 'lib/celluloid/mailbox.rb', line 130

def inspect
  "#<#{self.class}:#{object_id.to_s(16)} @messages=[#{map { |m| m.inspect }.join(', ')}]>"
end

#next_messageObject

Retrieve the next message in the mailbox



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/celluloid/mailbox.rb', line 82

def next_message
  message = nil

  if block_given?
    index = @messages.index do |msg|
      yield(msg) || msg.is_a?(SystemEvent)
    end

    message = @messages.slice!(index, 1).first if index
  else
    message = @messages.shift
  end

  raise message if message.is_a? SystemEvent
  message
end

#receive(timeout = nil, &block) ⇒ Object

Receive a message from the Mailbox



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/celluloid/mailbox.rb', line 51

def receive(timeout = nil, &block)
  message = nil

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

    begin
      message = next_message(&block)

      unless 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

        @condition.wait(@mutex, wait_interval)
      end
    end until message

    message
  ensure
    @mutex.unlock rescue nil
  end
end

#shutdownObject

Shut down this mailbox and clean up its contents



100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/celluloid/mailbox.rb', line 100

def shutdown
  @mutex.lock
  begin
    messages = @messages
    @messages = []
    @dead = true
  ensure
    @mutex.unlock rescue nil
  end

  messages.each { |msg| msg.cleanup if msg.respond_to? :cleanup }
  true
end

#system_event(event) ⇒ Object

Add a high-priority system event to the Mailbox



37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/celluloid/mailbox.rb', line 37

def system_event(event)
  @mutex.lock
  begin
    unless @dead # Silently fail if messages are sent to dead actors
      @messages.unshift event
      @condition.signal
    end
    nil
  ensure
    @mutex.unlock rescue nil
  end
end

#to_aObject

Cast to an array



120
121
122
# File 'lib/celluloid/mailbox.rb', line 120

def to_a
  @mutex.synchronize { @messages.dup }
end