Class: Celluloid::ZMQ::Waker
- Inherits:
-
Object
- Object
- Celluloid::ZMQ::Waker
- Extended by:
- Forwardable
- Defined in:
- lib/celluloid/zmq/waker.rb
Overview
Wakes up sleepy threads so that they can check their mailbox Works like a ConditionVariable, except it’s implemented as a ZMQ socket so that it can be multiplexed alongside other ZMQ sockets
Constant Summary collapse
- PAYLOAD =
the payload doesn’t matter, it’s just a signal
"\0"
Instance Method Summary collapse
-
#cleanup ⇒ Object
(also: #shutdown)
Clean up the IO objects associated with this waker.
-
#initialize ⇒ Waker
constructor
A new instance of Waker.
-
#signal ⇒ Object
Wakes up the thread that is waiting for this Waker.
-
#socket ⇒ Object
0MQ socket to wait for messages on.
-
#wait ⇒ Object
Wait for another thread to signal this Waker.
Constructor Details
#initialize ⇒ Waker
Returns a new instance of Waker.
14 15 16 17 18 19 20 21 22 23 |
# File 'lib/celluloid/zmq/waker.rb', line 14 def initialize @sender = ZMQ.context.socket(::ZMQ::PAIR) @receiver = ZMQ.context.socket(::ZMQ::PAIR) @addr = "inproc://waker-#{object_id}" @sender.bind @addr @receiver.connect @addr @sender_lock = Mutex.new end |
Instance Method Details
#cleanup ⇒ Object Also known as: shutdown
Clean up the IO objects associated with this waker
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/celluloid/zmq/waker.rb', line 50 def cleanup @sender_lock.synchronize do begin @sender.close rescue nil end end begin @receiver.close rescue nil end nil end |
#signal ⇒ Object
Wakes up the thread that is waiting for this Waker
26 27 28 29 30 31 32 |
# File 'lib/celluloid/zmq/waker.rb', line 26 def signal @sender_lock.synchronize do unless result_ok? @sender.send_string PAYLOAD fail DeadWakerError, "error sending 0MQ message: #{::ZMQ::Util.error_string}" end end end |
#socket ⇒ Object
0MQ socket to wait for messages on
35 36 37 |
# File 'lib/celluloid/zmq/waker.rb', line 35 def socket @receiver end |
#wait ⇒ Object
Wait for another thread to signal this Waker
40 41 42 43 44 45 46 47 |
# File 'lib/celluloid/zmq/waker.rb', line 40 def wait = "" rc = @receiver.recv_string unless result_ok?(rc) && == PAYLOAD fail DeadWakerError, "error receiving ZMQ string: #{::ZMQ::Util.error_string}" end end |