lib/celluloid/io/mailbox.rb in celluloid-io-0.8.0 vs lib/celluloid/io/mailbox.rb in celluloid-io-0.9.0
- old
+ new
@@ -2,36 +2,37 @@
module IO
# An alternative implementation of Celluloid::Mailbox using Reactor
class Mailbox < Celluloid::Mailbox
attr_reader :reactor
- def initialize
+ def initialize(reactor = nil)
@messages = []
- @lock = Mutex.new
- @reactor = Reactor.new
+ @mutex = Mutex.new
+ @reactor = reactor || Reactor.new
end
# Add a message to the Mailbox
def <<(message)
- @lock.synchronize do
+ @mutex.lock
+ begin
@messages << message
@reactor.wakeup
+ rescue IOError
+ raise MailboxError, "dead recipient"
+ ensure @mutex.unlock
end
nil
- rescue IOError
- raise MailboxError, "dead recipient"
end
# Add a high-priority system event to the Mailbox
def system_event(event)
- @lock.synchronize do
+ @mutex.lock
+ begin
@messages.unshift event
-
- begin
- @reactor.wakeup
- rescue IOError
- # Silently fail if messages are sent to dead actors
- end
+ @reactor.wakeup
+ rescue IOError
+ # Silently fail if messages are sent to dead actors
+ ensure @mutex.unlock
end
nil
end
# Receive a message from the Mailbox