ctx.hpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. /*
  2. Copyright (c) 2007-2017 Contributors as noted in the AUTHORS file
  3. This file is part of libzmq, the ZeroMQ core engine in C++.
  4. libzmq is free software; you can redistribute it and/or modify it under
  5. the terms of the GNU Lesser General Public License (LGPL) as published
  6. by the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. As a special exception, the Contributors give you permission to link
  9. this library with independent modules to produce an executable,
  10. regardless of the license terms of these independent modules, and to
  11. copy and distribute the resulting executable under terms of your choice,
  12. provided that you also meet, for each linked independent module, the
  13. terms and conditions of the license of that module. An independent
  14. module is a module which is not derived from or based on this library.
  15. If you modify this library, you must extend this exception to your
  16. version of the library.
  17. libzmq is distributed in the hope that it will be useful, but WITHOUT
  18. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  19. FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
  20. License for more details.
  21. You should have received a copy of the GNU Lesser General Public License
  22. along with this program. If not, see <http://www.gnu.org/licenses/>.
  23. */
  24. #ifndef __ZMQ_CTX_HPP_INCLUDED__
  25. #define __ZMQ_CTX_HPP_INCLUDED__
  26. #include <map>
  27. #include <vector>
  28. #include <string>
  29. #include <stdarg.h>
  30. #include "mailbox.hpp"
  31. #include "array.hpp"
  32. #include "config.hpp"
  33. #include "mutex.hpp"
  34. #include "stdint.hpp"
  35. #include "options.hpp"
  36. #include "atomic_counter.hpp"
  37. #include "thread.hpp"
  38. namespace zmq
  39. {
  40. class object_t;
  41. class io_thread_t;
  42. class socket_base_t;
  43. class reaper_t;
  44. class pipe_t;
  45. // Information associated with inproc endpoint. Note that endpoint options
  46. // are registered as well so that the peer can access them without a need
  47. // for synchronisation, handshaking or similar.
  48. struct endpoint_t
  49. {
  50. socket_base_t *socket;
  51. options_t options;
  52. };
  53. class thread_ctx_t
  54. {
  55. public:
  56. thread_ctx_t ();
  57. // Start a new thread with proper scheduling parameters.
  58. void start_thread (thread_t &thread_,
  59. thread_fn *tfn_,
  60. void *arg_,
  61. const char *name_ = NULL) const;
  62. int set (int option_, const void *optval_, size_t optvallen_);
  63. int get (int option_, void *optval_, const size_t *optvallen_);
  64. protected:
  65. // Synchronisation of access to context options.
  66. mutex_t _opt_sync;
  67. private:
  68. // Thread parameters.
  69. int _thread_priority;
  70. int _thread_sched_policy;
  71. std::set<int> _thread_affinity_cpus;
  72. std::string _thread_name_prefix;
  73. };
  74. // Context object encapsulates all the global state associated with
  75. // the library.
  76. class ctx_t ZMQ_FINAL : public thread_ctx_t
  77. {
  78. public:
  79. // Create the context object.
  80. ctx_t ();
  81. // Returns false if object is not a context.
  82. bool check_tag () const;
  83. // This function is called when user invokes zmq_ctx_term. If there are
  84. // no more sockets open it'll cause all the infrastructure to be shut
  85. // down. If there are open sockets still, the deallocation happens
  86. // after the last one is closed.
  87. int terminate ();
  88. // This function starts the terminate process by unblocking any blocking
  89. // operations currently in progress and stopping any more socket activity
  90. // (except zmq_close).
  91. // This function is non-blocking.
  92. // terminate must still be called afterwards.
  93. // This function is optional, terminate will unblock any current
  94. // operations as well.
  95. int shutdown ();
  96. // Set and get context properties.
  97. int set (int option_, const void *optval_, size_t optvallen_);
  98. int get (int option_, void *optval_, const size_t *optvallen_);
  99. int get (int option_);
  100. // Create and destroy a socket.
  101. zmq::socket_base_t *create_socket (int type_);
  102. void destroy_socket (zmq::socket_base_t *socket_);
  103. // Send command to the destination thread.
  104. void send_command (uint32_t tid_, const command_t &command_);
  105. // Returns the I/O thread that is the least busy at the moment.
  106. // Affinity specifies which I/O threads are eligible (0 = all).
  107. // Returns NULL if no I/O thread is available.
  108. zmq::io_thread_t *choose_io_thread (uint64_t affinity_);
  109. // Returns reaper thread object.
  110. zmq::object_t *get_reaper () const;
  111. // Management of inproc endpoints.
  112. int register_endpoint (const char *addr_, const endpoint_t &endpoint_);
  113. int unregister_endpoint (const std::string &addr_,
  114. const socket_base_t *socket_);
  115. void unregister_endpoints (const zmq::socket_base_t *socket_);
  116. endpoint_t find_endpoint (const char *addr_);
  117. void pend_connection (const std::string &addr_,
  118. const endpoint_t &endpoint_,
  119. pipe_t **pipes_);
  120. void connect_pending (const char *addr_, zmq::socket_base_t *bind_socket_);
  121. #ifdef ZMQ_HAVE_VMCI
  122. // Return family for the VMCI socket or -1 if it's not available.
  123. int get_vmci_socket_family ();
  124. #endif
  125. enum
  126. {
  127. term_tid = 0,
  128. reaper_tid = 1
  129. };
  130. ~ctx_t ();
  131. bool valid () const;
  132. private:
  133. bool start ();
  134. struct pending_connection_t
  135. {
  136. endpoint_t endpoint;
  137. pipe_t *connect_pipe;
  138. pipe_t *bind_pipe;
  139. };
  140. // Used to check whether the object is a context.
  141. uint32_t _tag;
  142. // Sockets belonging to this context. We need the list so that
  143. // we can notify the sockets when zmq_ctx_term() is called.
  144. // The sockets will return ETERM then.
  145. typedef array_t<socket_base_t> sockets_t;
  146. sockets_t _sockets;
  147. // List of unused thread slots.
  148. typedef std::vector<uint32_t> empty_slots_t;
  149. empty_slots_t _empty_slots;
  150. // If true, zmq_init has been called but no socket has been created
  151. // yet. Launching of I/O threads is delayed.
  152. bool _starting;
  153. // If true, zmq_ctx_term was already called.
  154. bool _terminating;
  155. // Synchronisation of accesses to global slot-related data:
  156. // sockets, empty_slots, terminating. It also synchronises
  157. // access to zombie sockets as such (as opposed to slots) and provides
  158. // a memory barrier to ensure that all CPU cores see the same data.
  159. mutex_t _slot_sync;
  160. // The reaper thread.
  161. zmq::reaper_t *_reaper;
  162. // I/O threads.
  163. typedef std::vector<zmq::io_thread_t *> io_threads_t;
  164. io_threads_t _io_threads;
  165. // Array of pointers to mailboxes for both application and I/O threads.
  166. std::vector<i_mailbox *> _slots;
  167. // Mailbox for zmq_ctx_term thread.
  168. mailbox_t _term_mailbox;
  169. // List of inproc endpoints within this context.
  170. typedef std::map<std::string, endpoint_t> endpoints_t;
  171. endpoints_t _endpoints;
  172. // List of inproc connection endpoints pending a bind
  173. typedef std::multimap<std::string, pending_connection_t>
  174. pending_connections_t;
  175. pending_connections_t _pending_connections;
  176. // Synchronisation of access to the list of inproc endpoints.
  177. mutex_t _endpoints_sync;
  178. // Maximum socket ID.
  179. static atomic_counter_t max_socket_id;
  180. // Maximum number of sockets that can be opened at the same time.
  181. int _max_sockets;
  182. // Maximum allowed message size
  183. int _max_msgsz;
  184. // Number of I/O threads to launch.
  185. int _io_thread_count;
  186. // Does context wait (possibly forever) on termination?
  187. bool _blocky;
  188. // Is IPv6 enabled on this context?
  189. bool _ipv6;
  190. // Should we use zero copy message decoding in this context?
  191. bool _zero_copy;
  192. ZMQ_NON_COPYABLE_NOR_MOVABLE (ctx_t)
  193. #ifdef HAVE_FORK
  194. // the process that created this context. Used to detect forking.
  195. pid_t _pid;
  196. #endif
  197. enum side
  198. {
  199. connect_side,
  200. bind_side
  201. };
  202. static void
  203. connect_inproc_sockets (zmq::socket_base_t *bind_socket_,
  204. const options_t &bind_options_,
  205. const pending_connection_t &pending_connection_,
  206. side side_);
  207. #ifdef ZMQ_HAVE_VMCI
  208. int _vmci_fd;
  209. int _vmci_family;
  210. mutex_t _vmci_sync;
  211. #endif
  212. };
  213. }
  214. #endif