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

    This file is part of 0MQ.

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

    0MQ 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
14
    GNU Lesser General Public License for more details.
Martin Sustrik's avatar
Martin Sustrik committed
15

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

20 21
#ifndef __ZMQ_CTX_HPP_INCLUDED__
#define __ZMQ_CTX_HPP_INCLUDED__
Martin Sustrik's avatar
Martin Sustrik committed
22 23

#include <map>
24
#include <vector>
Martin Sustrik's avatar
Martin Sustrik committed
25
#include <string>
26
#include <stdarg.h>
Martin Sustrik's avatar
Martin Sustrik committed
27

28
#include "mailbox.hpp"
29
#include "array.hpp"
Martin Sustrik's avatar
Martin Sustrik committed
30 31 32
#include "config.hpp"
#include "mutex.hpp"
#include "stdint.hpp"
33
#include "options.hpp"
34
#include "atomic_counter.hpp"
Martin Sustrik's avatar
Martin Sustrik committed
35

Martin Sustrik's avatar
Martin Sustrik committed
36
namespace zmq
Martin Sustrik's avatar
Martin Sustrik committed
37
{
38 39 40 41 42

    class object_t;
    class io_thread_t;
    class socket_base_t;
    class reaper_t;
43
    class pipe_t;
44

45 46 47 48 49
    //  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
50
        socket_base_t *socket;
51 52
        options_t options;
    };
53

54 55
    struct pending_connection_t
    {
56 57 58
        endpoint_t endpoint;
        pipe_t* connect_pipe;
        pipe_t* bind_pipe;
59 60
    };

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

64
    class ctx_t
Martin Sustrik's avatar
Martin Sustrik committed
65 66 67
    {
    public:

Martin Hurton's avatar
Martin Hurton committed
68
        //  Create the context object.
69
        ctx_t ();
Martin Sustrik's avatar
Martin Sustrik committed
70

71 72 73
        //  Returns false if object is not a context.
        bool check_tag ();

74 75 76 77
        //  This function is called when user invokes zmq_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.
78
        int terminate ();
Martin Sustrik's avatar
Martin Sustrik committed
79

80 81 82 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.
        // This function is optional, terminate will unblock any current 
        // operations as well.
        int shutdown();

Martin Hurton's avatar
Martin Hurton committed
89
        //  Set and get context properties.
90 91
        int set (int option_, int optval_);
        int get (int option_);
Martin Hurton's avatar
Martin Hurton committed
92

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

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

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

105
        //  Returns reaper thread object.
106
        zmq::object_t *get_reaper ();
107

108
        //  Management of inproc endpoints.
109
        int register_endpoint (const char *addr_, endpoint_t &endpoint_);
110
        void unregister_endpoints (zmq::socket_base_t *socket_);
111
        endpoint_t find_endpoint (const char *addr_);
112
        void pend_connection (const char *addr_, pending_connection_t &pending_connection_);
113
        void connect_pending (const char *addr_, zmq::socket_base_t *bind_socket_);
114

115 116 117 118 119
        enum {
            term_tid = 0,
            reaper_tid = 1
        };

120
        ~ctx_t ();
121

Martin Sustrik's avatar
Martin Sustrik committed
122 123
    private:

124

125 126 127
        //  Used to check whether the object is a context.
        uint32_t tag;

128 129 130
        //  Sockets belonging to this context. We need the list so that
        //  we can notify the sockets when zmq_term() is called. The sockets
        //  will return ETERM then.
131
        typedef array_t <socket_base_t> sockets_t;
132 133
        sockets_t sockets;

Martin Sustrik's avatar
Martin Sustrik committed
134
        //  List of unused thread slots.
135 136
        typedef std::vector <uint32_t> empty_slots_t;
        empty_slots_t empty_slots;
137

Martin Hurton's avatar
Martin Hurton committed
138 139
        //  If true, zmq_init has been called but no socket has been created
        //  yet. Launching of I/O threads is delayed.
140 141
        bool starting;

142 143
        //  If true, zmq_term was already called.
        bool terminating;
Martin Sustrik's avatar
Martin Sustrik committed
144

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

151
        //  The reaper thread.
152
        zmq::reaper_t *reaper;
153

Martin Sustrik's avatar
Martin Sustrik committed
154
        //  I/O threads.
155
        typedef std::vector <zmq::io_thread_t*> io_threads_t;
Martin Sustrik's avatar
Martin Sustrik committed
156 157
        io_threads_t io_threads;

158
        //  Array of pointers to mailboxes for both application and I/O threads.
159
        uint32_t slot_count;
160
        mailbox_t **slots;
161

162 163 164
        //  Mailbox for zmq_term thread.
        mailbox_t term_mailbox;

165
        //  List of inproc endpoints within this context.
166
        typedef std::map <std::string, endpoint_t> endpoints_t;
167 168
        endpoints_t endpoints;

169
        // List of inproc connection endpoints pending a bind
170
        typedef std::multimap <std::string, pending_connection_t> pending_connections_t;
171 172
        pending_connections_t pending_connections;

173 174 175
        //  Synchronisation of access to the list of inproc endpoints.
        mutex_t endpoints_sync;

176 177 178 179 180 181 182 183 184
        //  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;

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

Pieter Hintjens's avatar
Pieter Hintjens committed
185 186 187
        //  Is IPv6 enabled on this context?
        bool ipv6;

188 189
        //  Synchronisation of access to context options.
        mutex_t opt_sync;
Martin Hurton's avatar
Martin Hurton committed
190

191
        ctx_t (const ctx_t&);
192
        const ctx_t &operator = (const ctx_t&);
193 194 195 196 197

#ifdef HAVE_FORK
        // the process that created this context. Used to detect forking.
        pid_t pid;
#endif
198 199
        enum side { connect_side, bind_side };
        void connect_inproc_sockets(zmq::socket_base_t *bind_socket_, options_t& bind_options, pending_connection_t &pending_connection_, side side_);
Martin Sustrik's avatar
Martin Sustrik committed
200
    };
Martin Hurton's avatar
Martin Hurton committed
201

Martin Sustrik's avatar
Martin Sustrik committed
202 203 204
}

#endif