ctx.hpp 7.9 KB
Newer Older
Martin Sustrik's avatar
Martin Sustrik committed
1
/*
2
    Copyright (c) 2007-2016 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

    class object_t;
    class io_thread_t;
    class socket_base_t;
    class reaper_t;
54
    class pipe_t;
55

56 57 58 59 60
    //  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
    {
Martin Hurton's avatar
Martin Hurton committed
61
        socket_base_t *socket;
62 63
        options_t options;
    };
64 65 66

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

68
    class ctx_t
Martin Sustrik's avatar
Martin Sustrik committed
69 70 71
    {
    public:

Martin Hurton's avatar
Martin Hurton committed
72
        //  Create the context object.
73
        ctx_t ();
Martin Sustrik's avatar
Martin Sustrik committed
74

75 76 77
        //  Returns false if object is not a context.
        bool check_tag ();

78
        //  This function is called when user invokes zmq_ctx_term. If there are
79 80 81
        //  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.
82
        int terminate ();
Martin Sustrik's avatar
Martin Sustrik committed
83

84 85 86 87 88
        // 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.
89
        // This function is optional, terminate will unblock any current
90 91 92
        // operations as well.
        int shutdown();

Martin Hurton's avatar
Martin Hurton committed
93
        //  Set and get context properties.
94 95
        int set (int option_, int optval_);
        int get (int option_);
Martin Hurton's avatar
Martin Hurton committed
96

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

101
        //  Start a new thread with proper scheduling parameters.
102 103
        void start_thread (thread_t &thread_, thread_fn *tfn_, void *arg_) const;

Martin Sustrik's avatar
Martin Sustrik committed
104 105
        //  Send command to the destination thread.
        void send_command (uint32_t tid_, const command_t &command_);
106

Martin Sustrik's avatar
Martin Sustrik committed
107
        //  Returns the I/O thread that is the least busy at the moment.
108
        //  Affinity specifies which I/O threads are eligible (0 = all).
Martin Hurton's avatar
Martin Hurton committed
109
        //  Returns NULL if no I/O thread is available.
110
        zmq::io_thread_t *choose_io_thread (uint64_t affinity_);
Martin Sustrik's avatar
Martin Sustrik committed
111

112
        //  Returns reaper thread object.
113
        zmq::object_t *get_reaper ();
114

115
        //  Management of inproc endpoints.
116
        int register_endpoint (const char *addr_, const endpoint_t &endpoint_);
Martin Hurton's avatar
Martin Hurton committed
117
        int unregister_endpoint (const std::string &addr_, socket_base_t *socket_);
118
        void unregister_endpoints (zmq::socket_base_t *socket_);
119
        endpoint_t find_endpoint (const char *addr_);
Martin Hurton's avatar
Martin Hurton committed
120 121
        void pend_connection (const std::string &addr_,
                const endpoint_t &endpoint_, pipe_t **pipes_);
122
        void connect_pending (const char *addr_, zmq::socket_base_t *bind_socket_);
123

Ilya Kulakov's avatar
Ilya Kulakov committed
124 125 126 127 128
#ifdef ZMQ_HAVE_VMCI
        // Return family for the VMCI socket or -1 if it's not available.
        int get_vmci_socket_family ();
#endif

129 130 131 132 133
        enum {
            term_tid = 0,
            reaper_tid = 1
        };

134
        ~ctx_t ();
135

Martin Sustrik's avatar
Martin Sustrik committed
136 137
    private:

Martin Hurton's avatar
Martin Hurton committed
138 139 140 141 142 143
        struct pending_connection_t
        {
            endpoint_t endpoint;
            pipe_t* connect_pipe;
            pipe_t* bind_pipe;
        };
144

145 146 147
        //  Used to check whether the object is a context.
        uint32_t tag;

148
        //  Sockets belonging to this context. We need the list so that
149 150
        //  we can notify the sockets when zmq_ctx_term() is called.
        //  The sockets will return ETERM then.
151
        typedef array_t <socket_base_t> sockets_t;
152 153
        sockets_t sockets;

Martin Sustrik's avatar
Martin Sustrik committed
154
        //  List of unused thread slots.
155 156
        typedef std::vector <uint32_t> empty_slots_t;
        empty_slots_t empty_slots;
157

Martin Hurton's avatar
Martin Hurton committed
158 159
        //  If true, zmq_init has been called but no socket has been created
        //  yet. Launching of I/O threads is delayed.
160 161
        bool starting;

162
        //  If true, zmq_ctx_term was already called.
163
        bool terminating;
Martin Sustrik's avatar
Martin Sustrik committed
164

165
        //  Synchronisation of accesses to global slot-related data:
166
        //  sockets, empty_slots, terminating. It also synchronises
167
        //  access to zombie sockets as such (as opposed to slots) and provides
168 169
        //  a memory barrier to ensure that all CPU cores see the same data.
        mutex_t slot_sync;
Martin Sustrik's avatar
Martin Sustrik committed
170

171
        //  The reaper thread.
172
        zmq::reaper_t *reaper;
173

Martin Sustrik's avatar
Martin Sustrik committed
174
        //  I/O threads.
175
        typedef std::vector <zmq::io_thread_t*> io_threads_t;
Martin Sustrik's avatar
Martin Sustrik committed
176 177
        io_threads_t io_threads;

178
        //  Array of pointers to mailboxes for both application and I/O threads.
179
        uint32_t slot_count;
somdoron's avatar
somdoron committed
180
        i_mailbox **slots;
181

182
        //  Mailbox for zmq_ctx_term thread.
183 184
        mailbox_t term_mailbox;

185
        //  List of inproc endpoints within this context.
186
        typedef std::map <std::string, endpoint_t> endpoints_t;
187 188
        endpoints_t endpoints;

189
        // List of inproc connection endpoints pending a bind
190
        typedef std::multimap <std::string, pending_connection_t> pending_connections_t;
191 192
        pending_connections_t pending_connections;

193 194 195
        //  Synchronisation of access to the list of inproc endpoints.
        mutex_t endpoints_sync;

196 197 198 199 200 201
        //  Maximum socket ID.
        static atomic_counter_t max_socket_id;

        //  Maximum number of sockets that can be opened at the same time.
        int max_sockets;

202 203 204
        //  Maximum allowed message size
        int max_msgsz;

205 206 207
        //  Number of I/O threads to launch.
        int io_thread_count;

208 209 210
        //  Does context wait (possibly forever) on termination?
        bool blocky;

Pieter Hintjens's avatar
Pieter Hintjens committed
211 212 213
        //  Is IPv6 enabled on this context?
        bool ipv6;

214
        //  Thread scheduling parameters.
215 216 217
        int thread_priority;
        int thread_sched_policy;

218 219
        //  Synchronisation of access to context options.
        mutex_t opt_sync;
Martin Hurton's avatar
Martin Hurton committed
220

221
        ctx_t (const ctx_t&);
222
        const ctx_t &operator = (const ctx_t&);
223 224 225 226 227

#ifdef HAVE_FORK
        // the process that created this context. Used to detect forking.
        pid_t pid;
#endif
228
        enum side { connect_side, bind_side };
Martin Hurton's avatar
Martin Hurton committed
229
        void connect_inproc_sockets(zmq::socket_base_t *bind_socket_, options_t& bind_options, const pending_connection_t &pending_connection_, side side_);
Ilya Kulakov's avatar
Ilya Kulakov committed
230 231 232 233 234 235

#ifdef ZMQ_HAVE_VMCI
        int vmci_fd;
        int vmci_family;
        mutex_t vmci_sync;
#endif
Martin Sustrik's avatar
Martin Sustrik committed
236
    };
Martin Hurton's avatar
Martin Hurton committed
237

Martin Sustrik's avatar
Martin Sustrik committed
238 239 240
}

#endif