/usr/lib/ruby/vendor_ruby/ffi-rzmq/context.rb is in ruby-ffi-rzmq 2.0.4-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 | module ZMQ
# Recommended to use the default for +io_threads+
# since most programs will not saturate I/O.
#
# The rule of thumb is to make +io_threads+ equal to the number
# gigabits per second that the application will produce.
#
# The +io_threads+ number specifies the size of the thread pool
# allocated by 0mq for processing incoming/outgoing messages.
#
# Returns a context object when allocation succeeds. It's necessary
# for passing to the
# #Socket constructor when allocating new sockets. All sockets
# live within a context.
#
# Also, Sockets should *only* be accessed from the thread where they
# were first created. Do *not* pass sockets between threads; pass
# in the context and allocate a new socket per thread. If you must
# use threads, then make sure to execute a full memory barrier (e.g.
# mutex) as you pass a socket from one thread to the next.
#
# To connect sockets between contexts, use +inproc+ or +ipc+
# transport and set up a 0mq socket between them. This is also the
# recommended technique for allowing sockets to communicate between
# threads.
#
# context = ZMQ::Context.create
# if context
# socket = context.socket(ZMQ::REQ)
# if socket
# ...
# else
# STDERR.puts "Socket allocation failed"
# end
# else
# STDERR.puts "Context allocation failed"
# end
#
#
class Context
attr_reader :context, :io_threads, :max_sockets
alias :pointer :context
# Use the factory method Context#create to make contexts.
#
def self.create(opts = {})
new(opts) rescue nil
end
def initialize(opts = {})
if opts.respond_to?(:empty?)
@io_threads = opts[:io_threads] || IO_THREADS_DFLT
@max_sockets = opts[:max_sockets] || MAX_SOCKETS_DFLT
else
@io_threads = opts || 1
@max_sockets = MAX_SOCKETS_DFLT
end
@context = LibZMQ.zmq_ctx_new
ZMQ::Util.error_check 'zmq_ctx_new', (@context.nil? || @context.null?) ? -1 : 0
rc = LibZMQ.zmq_ctx_set(@context, ZMQ::IO_THREADS, @io_threads)
ZMQ::Util.error_check 'zmq_ctx_set', rc
rc = LibZMQ.zmq_ctx_set(@context, ZMQ::MAX_SOCKETS, @max_sockets)
ZMQ::Util.error_check 'zmq_ctx_set', rc
define_finalizer
end
# Call to release the context and any remaining data associated
# with past sockets. This will close any sockets that remain
# open; further calls to those sockets will return -1 to indicate
# the operation failed.
#
# Returns 0 for success, -1 for failure.
#
def terminate
unless @context.nil? || @context.null?
remove_finalizer
rc = LibZMQ.zmq_ctx_destroy(@context)
@context = nil
rc
else
0
end
end
# Short-cut to allocate a socket for a specific context.
#
# Takes several +type+ values:
# #ZMQ::REQ
# #ZMQ::REP
# #ZMQ::PUB
# #ZMQ::SUB
# #ZMQ::PAIR
# #ZMQ::PULL
# #ZMQ::PUSH
# #ZMQ::DEALER
# #ZMQ::ROUTER
#
# Returns a #ZMQ::Socket when the allocation succeeds, nil
# if it fails.
#
def socket type
sock = nil
begin
sock = Socket.new @context, type
rescue ContextError => e
sock = nil
end
sock
end
private
def define_finalizer
ObjectSpace.define_finalizer(self, self.class.close(@context, Process.pid))
end
def remove_finalizer
ObjectSpace.undefine_finalizer self
end
def self.close context, pid
Proc.new { LibZMQ.zmq_term context if !context.null? && Process.pid == pid }
end
end
end # module ZMQ
|