ctx.hpp 8.19 KB
Newer Older
Martin Sustrik's avatar
Martin Sustrik committed
1
/*
2
    Copyright (c) 2007-2017 Contributors as noted in the AUTHORS file
Martin Sustrik's avatar
Martin Sustrik committed
3

4
    This file is part of libzmq, the ZeroMQ core engine in C++.
Martin Sustrik's avatar
Martin Sustrik committed
5

6 7 8
    libzmq is free software; you can redistribute it and/or modify it under
    the terms of the GNU Lesser General Public License (LGPL) as published
    by the Free Software Foundation; either version 3 of the License, or
Martin Sustrik's avatar
Martin Sustrik committed
9 10
    (at your option) any later version.

11 12 13 14 15 16 17 18 19 20 21 22 23 24
    As a special exception, the Contributors give you permission to link
    this library with independent modules to produce an executable,
    regardless of the license terms of these independent modules, and to
    copy and distribute the resulting executable under terms of your choice,
    provided that you also meet, for each linked independent module, the
    terms and conditions of the license of that module. An independent
    module is a module which is not derived from or based on this library.
    If you modify this library, you must extend this exception to your
    version of the library.

    libzmq is distributed in the hope that it will be useful, but WITHOUT
    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
    License for more details.
Martin Sustrik's avatar
Martin Sustrik committed
25

26
    You should have received a copy of the GNU Lesser General Public License
Martin Sustrik's avatar
Martin Sustrik committed
27 28 29
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

30 31
#ifndef __ZMQ_CTX_HPP_INCLUDED__
#define __ZMQ_CTX_HPP_INCLUDED__
Martin Sustrik's avatar
Martin Sustrik committed
32 33

#include <map>
34
#include <vector>
Martin Sustrik's avatar
Martin Sustrik committed
35
#include <string>
36
#include <stdarg.h>
Martin Sustrik's avatar
Martin Sustrik committed
37

38
#include "mailbox.hpp"
39
#include "array.hpp"
Martin Sustrik's avatar
Martin Sustrik committed
40 41 42
#include "config.hpp"
#include "mutex.hpp"
#include "stdint.hpp"
43
#include "options.hpp"
44
#include "atomic_counter.hpp"
45
#include "thread.hpp"
Martin Sustrik's avatar
Martin Sustrik committed
46

Martin Sustrik's avatar
Martin Sustrik committed
47
namespace zmq
Martin Sustrik's avatar
Martin Sustrik committed
48
{
49 50 51 52 53 54 55 56 57 58 59 60 61 62
class object_t;
class io_thread_t;
class socket_base_t;
class reaper_t;
class pipe_t;

//  Information associated with inproc endpoint. Note that endpoint options
//  are registered as well so that the peer can access them without a need
//  for synchronisation, handshaking or similar.
struct endpoint_t
{
    socket_base_t *socket;
    options_t options;
};
63

64 65 66 67 68 69
class thread_ctx_t
{
  public:
    thread_ctx_t ();

    //  Start a new thread with proper scheduling parameters.
70 71 72 73
    void start_thread (thread_t &thread_,
                       thread_fn *tfn_,
                       void *arg_,
                       const char *name_ = NULL) const;
74

75
    int set (int option_, const void *optval_, size_t optvallen_);
76
    int get (int option_, void *optval_, const size_t *optvallen_);
77 78 79

  protected:
    //  Synchronisation of access to context options.
80
    mutex_t _opt_sync;
81 82 83

  private:
    //  Thread parameters.
84 85 86 87
    int _thread_priority;
    int _thread_sched_policy;
    std::set<int> _thread_affinity_cpus;
    std::string _thread_name_prefix;
88 89
};

90 91
//  Context object encapsulates all the global state associated with
//  the library.
Martin Hurton's avatar
Martin Hurton committed
92

93
class ctx_t ZMQ_FINAL : public thread_ctx_t
94 95 96 97 98 99
{
  public:
    //  Create the context object.
    ctx_t ();

    //  Returns false if object is not a context.
100
    bool check_tag () const;
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117

    //  This function is called when user invokes zmq_ctx_term. If there are
    //  no more sockets open it'll cause all the infrastructure to be shut
    //  down. If there are open sockets still, the deallocation happens
    //  after the last one is closed.
    int terminate ();

    // This function starts the terminate process by unblocking any blocking
    // operations currently in progress and stopping any more socket activity
    // (except zmq_close).
    // This function is non-blocking.
    // terminate must still be called afterwards.
    // This function is optional, terminate will unblock any current
    // operations as well.
    int shutdown ();

    //  Set and get context properties.
118
    int set (int option_, const void *optval_, size_t optvallen_);
119
    int get (int option_, void *optval_, const size_t *optvallen_);
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
    int get (int option_);

    //  Create and destroy a socket.
    zmq::socket_base_t *create_socket (int type_);
    void destroy_socket (zmq::socket_base_t *socket_);

    //  Send command to the destination thread.
    void send_command (uint32_t tid_, const command_t &command_);

    //  Returns the I/O thread that is the least busy at the moment.
    //  Affinity specifies which I/O threads are eligible (0 = all).
    //  Returns NULL if no I/O thread is available.
    zmq::io_thread_t *choose_io_thread (uint64_t affinity_);

    //  Returns reaper thread object.
135
    zmq::object_t *get_reaper () const;
136 137 138

    //  Management of inproc endpoints.
    int register_endpoint (const char *addr_, const endpoint_t &endpoint_);
139 140 141
    int unregister_endpoint (const std::string &addr_,
                             const socket_base_t *socket_);
    void unregister_endpoints (const zmq::socket_base_t *socket_);
142 143 144 145 146
    endpoint_t find_endpoint (const char *addr_);
    void pend_connection (const std::string &addr_,
                          const endpoint_t &endpoint_,
                          pipe_t **pipes_);
    void connect_pending (const char *addr_, zmq::socket_base_t *bind_socket_);
147

Ilya Kulakov's avatar
Ilya Kulakov committed
148
#ifdef ZMQ_HAVE_VMCI
149 150
    // Return family for the VMCI socket or -1 if it's not available.
    int get_vmci_socket_family ();
Ilya Kulakov's avatar
Ilya Kulakov committed
151 152
#endif

153 154 155 156 157
    enum
    {
        term_tid = 0,
        reaper_tid = 1
    };
158

159
    ~ctx_t ();
160

161
    bool valid () const;
162

163 164
  private:
    bool start ();
Martin Sustrik's avatar
Martin Sustrik committed
165

166 167 168 169 170 171
    struct pending_connection_t
    {
        endpoint_t endpoint;
        pipe_t *connect_pipe;
        pipe_t *bind_pipe;
    };
172

173
    //  Used to check whether the object is a context.
174
    uint32_t _tag;
175

176 177 178 179
    //  Sockets belonging to this context. We need the list so that
    //  we can notify the sockets when zmq_ctx_term() is called.
    //  The sockets will return ETERM then.
    typedef array_t<socket_base_t> sockets_t;
180
    sockets_t _sockets;
181

182 183
    //  List of unused thread slots.
    typedef std::vector<uint32_t> empty_slots_t;
184
    empty_slots_t _empty_slots;
185

186 187
    //  If true, zmq_init has been called but no socket has been created
    //  yet. Launching of I/O threads is delayed.
188
    bool _starting;
189

190
    //  If true, zmq_ctx_term was already called.
191
    bool _terminating;
Martin Sustrik's avatar
Martin Sustrik committed
192

193 194 195 196
    //  Synchronisation of accesses to global slot-related data:
    //  sockets, empty_slots, terminating. It also synchronises
    //  access to zombie sockets as such (as opposed to slots) and provides
    //  a memory barrier to ensure that all CPU cores see the same data.
197
    mutex_t _slot_sync;
Martin Sustrik's avatar
Martin Sustrik committed
198

199
    //  The reaper thread.
200
    zmq::reaper_t *_reaper;
201

202 203
    //  I/O threads.
    typedef std::vector<zmq::io_thread_t *> io_threads_t;
204
    io_threads_t _io_threads;
Martin Sustrik's avatar
Martin Sustrik committed
205

206
    //  Array of pointers to mailboxes for both application and I/O threads.
207
    std::vector<i_mailbox *> _slots;
208

209
    //  Mailbox for zmq_ctx_term thread.
210
    mailbox_t _term_mailbox;
211

212 213
    //  List of inproc endpoints within this context.
    typedef std::map<std::string, endpoint_t> endpoints_t;
214
    endpoints_t _endpoints;
215

216 217 218
    // List of inproc connection endpoints pending a bind
    typedef std::multimap<std::string, pending_connection_t>
      pending_connections_t;
219
    pending_connections_t _pending_connections;
220

221
    //  Synchronisation of access to the list of inproc endpoints.
222
    mutex_t _endpoints_sync;
223

224 225
    //  Maximum socket ID.
    static atomic_counter_t max_socket_id;
226

227
    //  Maximum number of sockets that can be opened at the same time.
228
    int _max_sockets;
229

230
    //  Maximum allowed message size
231
    int _max_msgsz;
232

233
    //  Number of I/O threads to launch.
234
    int _io_thread_count;
235

236
    //  Does context wait (possibly forever) on termination?
237
    bool _blocky;
238

239
    //  Is IPv6 enabled on this context?
240
    bool _ipv6;
Pieter Hintjens's avatar
Pieter Hintjens committed
241

242
    // Should we use zero copy message decoding in this context?
243
    bool _zero_copy;
244

245
    ZMQ_NON_COPYABLE_NOR_MOVABLE (ctx_t)
246 247

#ifdef HAVE_FORK
248
    // the process that created this context. Used to detect forking.
249
    pid_t _pid;
250
#endif
251 252 253 254 255
    enum side
    {
        connect_side,
        bind_side
    };
256 257

    static void
258
    connect_inproc_sockets (zmq::socket_base_t *bind_socket_,
259
                            const options_t &bind_options_,
260 261
                            const pending_connection_t &pending_connection_,
                            side side_);
Ilya Kulakov's avatar
Ilya Kulakov committed
262 263

#ifdef ZMQ_HAVE_VMCI
264 265 266
    int _vmci_fd;
    int _vmci_family;
    mutex_t _vmci_sync;
Ilya Kulakov's avatar
Ilya Kulakov committed
267
#endif
268
};
Martin Sustrik's avatar
Martin Sustrik committed
269 270 271
}

#endif