class ZMQ::Context
The context object encapsulates all the global state associated with the library.
Attributes
pointer[R]
The FFI pointer to the context.
Public Class Methods
finalizer(pointer, socket_pointers, pid)
click to toggle source
Create a safe finalizer for the context pointer to terminate on GC
# File lib/0mq/context.rb, line 56 def self.finalizer(pointer, socket_pointers, pid) Proc.new do if Process.pid == pid # Close all socket pointers associated with this context. # # If any of these sockets are still open when zmq_ctx_term is called, # the call will hang. This is problematic, as the execution of # finalizers is not multithreaded, and the order of finalizers is not # guaranteed. Even when the Sockets each hold a reference to the # Context, the Context could still be GCed first, causing lockup. socket_pointers.each { |ptr| LibZMQ.zmq_close ptr } socket_pointers.clear LibZMQ.respond_to?(:zmq_ctx_term) ? LibZMQ.zmq_ctx_term(pointer) : LibZMQ.zmq_term(pointer) end end end
new()
click to toggle source
# File lib/0mq/context.rb, line 11 def initialize # FFI socket pointer for this context @pointer = LibZMQ.zmq_ctx_new # List of FFI socket pointers associated with this context. # Each Socket is responsible for registering and unregistering # its pointer from the Context it is associated with. # See #register_socket_pointer and #unregister_socket_pointer, # as well as #terminate and self.finalizer (where they get closed) @socket_pointers = Array.new @socket_pointers_mutex = Mutex.new ObjectSpace.define_finalizer self, self.class.finalizer(@pointer, @socket_pointers, Process.pid) end
Public Instance Methods
register_socket_pointer(pointer)
click to toggle source
@api private
# File lib/0mq/context.rb, line 28 def register_socket_pointer pointer @socket_pointers_mutex.synchronize do @socket_pointers.push pointer end end
socket(type, opts={})
click to toggle source
Create a Socket
within this context.
# File lib/0mq/context.rb, line 77 def socket(type, opts={}) opts[:context] = self ZMQ::Socket.new type, opts end
terminate()
click to toggle source
Destroy the ØMQ context.
# File lib/0mq/context.rb, line 42 def terminate if @pointer ObjectSpace.undefine_finalizer self rc = LibZMQ.respond_to?(:zmq_ctx_term) ? LibZMQ.zmq_ctx_term(pointer) : LibZMQ.zmq_term(pointer) ZMQ.error_check true if rc==-1 @pointer = nil end end
to_ptr()
click to toggle source
Returns the context's FFI pointer.
# File lib/0mq/context.rb, line 83 def to_ptr @pointer end
unregister_socket_pointer(pointer)
click to toggle source
@api private
# File lib/0mq/context.rb, line 35 def unregister_socket_pointer pointer @socket_pointers_mutex.synchronize do @socket_pointers.delete pointer end end