Class: Celluloid::Mailbox
- Inherits:
-
Object
- Object
- Celluloid::Mailbox
- 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
-
#<<(message) ⇒ Object
Add a message to the Mailbox.
-
#alive? ⇒ Boolean
Is the mailbox alive?.
-
#each(&block) ⇒ Object
Iterate through the mailbox.
-
#initialize ⇒ Mailbox
constructor
A new instance of Mailbox.
-
#inspect ⇒ Object
Inspect the contents of the Mailbox.
-
#next_message ⇒ Object
Retrieve the next message in the mailbox.
-
#receive(timeout = nil, &block) ⇒ Object
Receive a message from the Mailbox.
-
#shutdown ⇒ Object
Shut down this mailbox and clean up its contents.
-
#system_event(event) ⇒ Object
Add a high-priority system event to the Mailbox.
-
#to_a ⇒ Object
Cast to an array.
Constructor Details
#initialize ⇒ Mailbox
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 <<() @mutex.lock begin raise MailboxError, "dead recipient" if @dead @messages << @condition.signal nil ensure @mutex.unlock rescue nil end end |
#alive? ⇒ Boolean
Is the mailbox alive?
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 |
#inspect ⇒ Object
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_message ⇒ Object
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 = nil if block_given? index = @messages.index do |msg| yield(msg) || msg.is_a?(SystemEvent) end = @messages.slice!(index, 1).first if index else = @messages.shift end raise if .is_a? SystemEvent 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) = nil @mutex.lock begin raise MailboxError, "attempted to receive from a dead mailbox" if @dead begin = (&block) unless 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 ensure @mutex.unlock rescue nil end end |
#shutdown ⇒ Object
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 = [] @dead = true ensure @mutex.unlock rescue nil end .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_a ⇒ Object
Cast to an array
120 121 122 |
# File 'lib/celluloid/mailbox.rb', line 120 def to_a @mutex.synchronize { @messages.dup } end |