ctx.hpp 5.54 KB
Newer Older
Martin Sustrik's avatar
Martin Sustrik committed
1
/*
2
    Copyright (c) 2007-2012 iMatix Corporation
Martin Sustrik's avatar
Martin Sustrik committed
3
    Copyright (c) 2009-2011 250bpm s.r.o.
4
    Copyright (c) 2007-2011 Other contributors as noted in the AUTHORS file
Martin Sustrik's avatar
Martin Sustrik committed
5 6 7 8

    This file is part of 0MQ.

    0MQ is free software; you can redistribute it and/or modify it under
9
    the terms of the GNU Lesser General Public License as published by
Martin Sustrik's avatar
Martin Sustrik committed
10 11 12 13 14 15
    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
16
    GNU Lesser General Public License for more details.
Martin Sustrik's avatar
Martin Sustrik committed
17

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

22 23
#ifndef __ZMQ_CTX_HPP_INCLUDED__
#define __ZMQ_CTX_HPP_INCLUDED__
Martin Sustrik's avatar
Martin Sustrik committed
24 25

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

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

Martin Sustrik's avatar
Martin Sustrik committed
38
namespace zmq
Martin Sustrik's avatar
Martin Sustrik committed
39
{
40 41 42 43 44 45

    class object_t;
    class io_thread_t;
    class socket_base_t;
    class reaper_t;

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

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

58
    class ctx_t
Martin Sustrik's avatar
Martin Sustrik committed
59 60 61
    {
    public:

Martin Hurton's avatar
Martin Hurton committed
62
        //  Create the context object.
63
        ctx_t ();
Martin Sustrik's avatar
Martin Sustrik committed
64

65 66 67
        //  Returns false if object is not a context.
        bool check_tag ();

68 69 70 71
        //  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.
72
        int terminate ();
Martin Sustrik's avatar
Martin Sustrik committed
73

Martin Hurton's avatar
Martin Hurton committed
74
        //  Set and get context properties.
75 76
        int set (int option_, int optval_);
        int get (int option_);
Martin Hurton's avatar
Martin Hurton committed
77

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

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

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

90
        //  Returns reaper thread object.
91
        zmq::object_t *get_reaper ();
92

93
        //  Management of inproc endpoints.
94
        int register_endpoint (const char *addr_, endpoint_t &endpoint_);
95
        void unregister_endpoints (zmq::socket_base_t *socket_);
96
        endpoint_t find_endpoint (const char *addr_);
97

98 99
        // Monitoring specific
        int monitor (zmq_monitor_fn *monitor_);
100 101
        void monitor_event (zmq::socket_base_t *socket_, int event_, ...);
        void va_monitor_event (zmq::socket_base_t *socket_, int event_, va_list args_);
102

103 104 105 106 107
        enum {
            term_tid = 0,
            reaper_tid = 1
        };

108
        ~ctx_t ();
109

Martin Sustrik's avatar
Martin Sustrik committed
110 111
    private:

112

113 114 115
        //  Used to check whether the object is a context.
        uint32_t tag;

116 117 118
        //  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.
119
        typedef array_t <socket_base_t> sockets_t;
120 121
        sockets_t sockets;

Martin Sustrik's avatar
Martin Sustrik committed
122
        //  List of unused thread slots.
123 124
        typedef std::vector <uint32_t> emtpy_slots_t;
        emtpy_slots_t empty_slots;
125

Martin Hurton's avatar
Martin Hurton committed
126 127
        //  If true, zmq_init has been called but no socket has been created
        //  yet. Launching of I/O threads is delayed.
128 129
        bool starting;

130 131
        //  If true, zmq_term was already called.
        bool terminating;
Martin Sustrik's avatar
Martin Sustrik committed
132

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

139
        //  The reaper thread.
140
        zmq::reaper_t *reaper;
141

Martin Sustrik's avatar
Martin Sustrik committed
142
        //  I/O threads.
143
        typedef std::vector <zmq::io_thread_t*> io_threads_t;
Martin Sustrik's avatar
Martin Sustrik committed
144 145
        io_threads_t io_threads;

146
        //  Array of pointers to mailboxes for both application and I/O threads.
147
        uint32_t slot_count;
148
        mailbox_t **slots;
149

150 151 152
        //  Mailbox for zmq_term thread.
        mailbox_t term_mailbox;

153
        //  List of inproc endpoints within this context.
154
        typedef std::map <std::string, endpoint_t> endpoints_t;
155 156 157 158 159
        endpoints_t endpoints;

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

160 161 162 163 164 165 166 167 168 169 170
        //  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;

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

172 173 174
        // Monitoring callback
        zmq_monitor_fn *monitor_fn;

175
        ctx_t (const ctx_t&);
176
        const ctx_t &operator = (const ctx_t&);
Martin Sustrik's avatar
Martin Sustrik committed
177
    };
Martin Hurton's avatar
Martin Hurton committed
178

Martin Sustrik's avatar
Martin Sustrik committed
179 180 181
}

#endif