Class: Celluloid::IO::Mailbox
- Inherits:
-
Mailbox
- Object
- Mailbox
- Celluloid::IO::Mailbox
- Defined in:
- lib/celluloid/io/mailbox.rb
Overview
An alternative implementation of Celluloid::Mailbox using Reactor
Instance Attribute Summary collapse
-
#reactor ⇒ Object
readonly
Returns the value of attribute reactor.
Instance Method Summary collapse
-
#<<(message) ⇒ Object
Add a message to the Mailbox.
-
#initialize(reactor = nil) ⇒ Mailbox
constructor
A new instance of Mailbox.
-
#next_message(block) ⇒ Object
Obtain the next message from the mailbox that matches the given block.
-
#receive(timeout = nil, &block) ⇒ Object
Receive a message from the Mailbox.
-
#shutdown ⇒ Object
Cleanup any IO objects this Mailbox may be using.
-
#system_event(event) ⇒ Object
Add a high-priority system event to the Mailbox.
Constructor Details
Instance Attribute Details
#reactor ⇒ Object (readonly)
Returns the value of attribute reactor.
5 6 7 |
# File 'lib/celluloid/io/mailbox.rb', line 5 def reactor @reactor end |
Instance Method Details
#<<(message) ⇒ Object
Add a message to the Mailbox
14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/celluloid/io/mailbox.rb', line 14 def <<() @mutex.lock begin @messages << current_actor = Thread.current[:actor] @reactor.wakeup unless current_actor && current_actor.mailbox == self rescue IOError raise MailboxError, "dead recipient" ensure @mutex.unlock rescue nil end nil end |
#next_message(block) ⇒ Object
Obtain the next message from the mailbox that matches the given block
68 69 70 71 72 73 74 75 |
# File 'lib/celluloid/io/mailbox.rb', line 68 def (block) @mutex.lock begin super(&block) ensure @mutex.unlock rescue nil end end |
#receive(timeout = nil, &block) ⇒ Object
Receive a message from the Mailbox
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/celluloid/io/mailbox.rb', line 44 def receive(timeout = nil, &block) = (block) until 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) = (block) end rescue IOError shutdown # force shutdown of the mailbox raise MailboxShutdown, "mailbox shutdown called during receive" end |
#shutdown ⇒ Object
Cleanup any IO objects this Mailbox may be using
78 79 80 81 |
# File 'lib/celluloid/io/mailbox.rb', line 78 def shutdown @reactor.shutdown super end |
#system_event(event) ⇒ Object
Add a high-priority system event to the Mailbox
29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/celluloid/io/mailbox.rb', line 29 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 rescue nil end nil end |