Class: Celluloid::ZMQ::Waker

Inherits:
Object
  • Object
show all
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

Constructor Details

#initializeWaker

Returns a new instance of Waker.



12
13
14
15
16
17
18
19
20
21
# File 'lib/celluloid/zmq/waker.rb', line 12

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

#cleanupObject Also known as: shutdown

Clean up the IO objects associated with this waker



48
49
50
51
52
# File 'lib/celluloid/zmq/waker.rb', line 48

def cleanup
  @sender_lock.synchronize { @sender.close rescue nil }
  @receiver.close rescue nil
  nil
end

#signalObject

Wakes up the thread that is waiting for this Waker



24
25
26
27
28
29
30
# File 'lib/celluloid/zmq/waker.rb', line 24

def signal
  @sender_lock.synchronize do
    unless ::ZMQ::Util.resultcode_ok? @sender.send_string PAYLOAD
      raise DeadWakerError, "error sending 0MQ message: #{::ZMQ::Util.error_string}"
    end
  end
end

#socketObject

0MQ socket to wait for messages on



33
34
35
# File 'lib/celluloid/zmq/waker.rb', line 33

def socket
  @receiver
end

#waitObject

Wait for another thread to signal this Waker



38
39
40
41
42
43
44
45
# File 'lib/celluloid/zmq/waker.rb', line 38

def wait
  message = ''
  rc = @receiver.recv_string message

  unless ::ZMQ::Util.resultcode_ok? rc and message == PAYLOAD
    raise DeadWakerError, "error receiving ZMQ string: #{::ZMQ::Util.error_string}"
  end
end