Class: Celluloid::Mailbox
- Includes:
- Enumerable
- Defined in:
- lib/vendor/celluloid/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.
Methods included from Enumerable
Constructor Details
#initialize ⇒ Mailbox
Returns a new instance of Mailbox.
15 16 17 18 19 20 |
# File 'lib/vendor/celluloid/lib/celluloid/mailbox.rb', line 15 def initialize @messages = [] @lock = 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 |
# File 'lib/vendor/celluloid/lib/celluloid/mailbox.rb', line 23 def <<() @lock.synchronize do raise MailboxError, "dead recipient" if @dead @messages << @condition.signal end nil end |
#alive? ⇒ Boolean
Is the mailbox alive?
105 106 107 |
# File 'lib/vendor/celluloid/lib/celluloid/mailbox.rb', line 105 def alive? !@dead end |
#each(&block) ⇒ Object
Iterate through the mailbox
115 116 117 |
# File 'lib/vendor/celluloid/lib/celluloid/mailbox.rb', line 115 def each(&block) to_a.each(&block) end |
#inspect ⇒ Object
Inspect the contents of the Mailbox
120 121 122 |
# File 'lib/vendor/celluloid/lib/celluloid/mailbox.rb', line 120 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
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/vendor/celluloid/lib/celluloid/mailbox.rb', line 73 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
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/vendor/celluloid/lib/celluloid/mailbox.rb', line 45 def receive(timeout = nil, &block) = nil @lock.synchronize do 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(@lock, wait_interval) end end until end end |
#shutdown ⇒ Object
Shut down this mailbox and clean up its contents
91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/vendor/celluloid/lib/celluloid/mailbox.rb', line 91 def shutdown = nil @lock.synchronize do = @messages @messages = [] @dead = true 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
34 35 36 37 38 39 40 41 42 |
# File 'lib/vendor/celluloid/lib/celluloid/mailbox.rb', line 34 def system_event(event) @lock.synchronize do unless @dead # Silently fail if messages are sent to dead actors @messages.unshift event @condition.signal end end nil end |
#to_a ⇒ Object
Cast to an array
110 111 112 |
# File 'lib/vendor/celluloid/lib/celluloid/mailbox.rb', line 110 def to_a @lock.synchronize { @messages.dup } end |