197 void *zmq_init (int io_threads_)
198 {
199 if (io_threads_ >= 0) {
200 void *ctx = zmq_ctx_new ();
201 zmq_ctx_set (ctx, ZMQ_IO_THREADS, io_threads_);
202 return ctx;
203 }
204 errno = EINVAL;
205 return NULL;
206 }
128 // New context API
129
130 void *zmq_ctx_new (void)
131 {
132 // We do this before the ctx constructor since its embedded mailbox_t
133 // object needs the network to be up and running (at least on Windows).
134 if (!zmq::initialize_network ()) {
135 return NULL;
136 }
137
138 // Create 0MQ context.
139 zmq::ctx_t *ctx = new (std::nothrow) zmq::ctx_t; /* 这里使用的是不抛出异常的new,因为zmq作为库来说,要与不支持异常的调用者兼容,如C。*/
140 if (ctx) {
141 if (!ctx->valid ()) {
142 delete ctx;
143 return NULL;
144 }
145 }
146 return ctx;
147 }
148
68 zmq::ctx_t::ctx_t () :
69 _tag (ZMQ_CTX_TAG_VALUE_GOOD),
70 _starting (true),
71 _terminating (false),
72 _reaper (NULL),
73 _max_sockets (clipped_maxsocket (ZMQ_MAX_SOCKETS_DFLT)),
74 _max_msgsz (INT_MAX),
75 _io_thread_count (ZMQ_IO_THREADS_DFLT),
76 _blocky (true),
77 _ipv6 (false),
78 _zero_copy (true)
79 {
80 #ifdef HAVE_FORK
81 _pid = getpid ();
82 #endif
83 #ifdef ZMQ_HAVE_VMCI
84 _vmci_fd = -1;
85 _vmci_family = -1;
86 #endif
87
88 // Initialise crypto library, if needed.
89 zmq::random_open ();
90 }
36 zmq::reaper_t::reaper_t (class ctx_t *ctx_, uint32_t tid_) :
37 object_t (ctx_, tid_),
38 _mailbox_handle (static_cast<poller_t::handle_t> (NULL)),
39 _poller (NULL),
40 _sockets (0),
41 _terminating (false)
42 {
43 if (!_mailbox.valid ())
44 return;
45 /* Linux下,poller_t实际上即为epoll_t*/
46 _poller = new (std::nothrow) poller_t (*ctx_);
47 alloc_assert (_poller);
48 //这里的mailbox.get_fd()得到该reaper_t拥有的mailbox的读描述符
49 if (_mailbox.get_fd () != retired_fd) {
50 _mailbox_handle = _poller->add_fd (_mailbox.get_fd (), this);
51 _poller->set_pollin (_mailbox_handle);
52 }
53
54 #ifdef HAVE_FORK
55 _pid = getpid ();
56 #endif
57 }