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.



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

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

Clean up the IO objects associated with this waker



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

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



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

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



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

def socket
  @receiver
end

#waitObject

Wait for another thread to signal this Waker



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

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