ctx.hpp 4.71 KB
Newer Older
Martin Sustrik's avatar
Martin Sustrik committed
1
/*
2
    Copyright (c) 2007-2010 iMatix Corporation
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 26
#include <string>

27 28
#include "../include/zmq.h"

29
#include "mailbox.hpp"
30
#include "semaphore.hpp"
Martin Sustrik's avatar
Martin Sustrik committed
31
#include "ypipe.hpp"
32
#include "array.hpp"
Martin Sustrik's avatar
Martin Sustrik committed
33 34 35
#include "config.hpp"
#include "mutex.hpp"
#include "stdint.hpp"
36
#include "thread.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

    //  Context object encapsulates all the global state associated with
    //  the library.
Martin Sustrik's avatar
Martin Sustrik committed
43
    
44
    class ctx_t
Martin Sustrik's avatar
Martin Sustrik committed
45 46 47
    {
    public:

48
        //  Create the context object. The argument specifies the size
49
        //  of I/O thread pool to create.
50
        ctx_t (uint32_t io_threads_);
Martin Sustrik's avatar
Martin Sustrik committed
51

52 53 54 55
        //  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.
56
        int terminate ();
Martin Sustrik's avatar
Martin Sustrik committed
57

58
        //  Create a socket.
59
        class socket_base_t *create_socket (int type_);
Martin Sustrik's avatar
Martin Sustrik committed
60

61
        //  Make socket a zombie.
62
        void zombify_socket (socket_base_t *socket_);
63

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

Martin Sustrik's avatar
Martin Sustrik committed
67
        //  Returns the I/O thread that is the least busy at the moment.
68 69 70
        //  Affinity specifies which I/O threads are eligible (0 = all).
        //  Returns NULL is no I/O thread is available.
        class io_thread_t *choose_io_thread (uint64_t affinity_);
Martin Sustrik's avatar
Martin Sustrik committed
71

72 73 74 75 76
        //  Management of inproc endpoints.
        int register_endpoint (const char *addr_, class socket_base_t *socket_);
        void unregister_endpoints (class socket_base_t *socket_);
        class socket_base_t *find_endpoint (const char *addr_);

77 78 79
        //  Logging.
        void log (zmq_msg_t *msg_);

Martin Sustrik's avatar
Martin Sustrik committed
80 81
    private:

82
        ~ctx_t ();
83

84
        //  Sockets belonging to this context.
85
        typedef array_t <socket_base_t> sockets_t;
86 87
        sockets_t sockets;

88
        //  List of sockets that were already closed but not yet deallocated.
89
        //  These sockets still have some pipes and I/O objects attached.
90
        typedef std::vector <socket_base_t*> zombies_t;
91 92
        zombies_t zombies;

Martin Sustrik's avatar
Martin Sustrik committed
93
        //  List of unused thread slots.
94 95
        typedef std::vector <uint32_t> emtpy_slots_t;
        emtpy_slots_t empty_slots;
96

97 98 99 100
        //  If true, shutdown thread wants to be informed when there are no
        //  more open sockets. Do so by posting no_sockets_sync semaphore.
        //  Note that this variable is synchronised by slot_sync mutex.
        bool no_sockets_notify;
101

102 103 104
        //  Object used by zmq_term to wait while all the sockets are closed
        //  by different application threads.
        semaphore_t no_sockets_sync;
Martin Sustrik's avatar
Martin Sustrik committed
105

106 107 108 109 110
        //  Synchronisation of accesses to global slot-related data:
        //  sockets, zombies, empty_slots, terminated. It also synchronises
        //  access to zombie sockets as such (as oposed to slots) and provides
        //  a memory barrier to ensure that all CPU cores see the same data.
        mutex_t slot_sync;
Martin Sustrik's avatar
Martin Sustrik committed
111

112 113 114
        //  This function attempts to deallocate as many zombie sockets as
        //  possible. It must be called within a slot_sync critical section.
        void dezombify ();
115

Martin Sustrik's avatar
Martin Sustrik committed
116 117 118 119
        //  I/O threads.
        typedef std::vector <class io_thread_t*> io_threads_t;
        io_threads_t io_threads;

120
        //  Array of pointers to mailboxes for both application and I/O threads.
121
        uint32_t slot_count;
122
        mailbox_t **slots;
123

124 125 126 127 128 129 130
        //  List of inproc endpoints within this context.
        typedef std::map <std::string, class socket_base_t*> endpoints_t;
        endpoints_t endpoints;

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

131 132 133 134 135
        //  PUB socket for logging. The socket is shared among all the threads,
        //  thus it is synchronised by a mutex.
        class socket_base_t *log_socket;
        mutex_t log_sync;

136 137
        ctx_t (const ctx_t&);
        void operator = (const ctx_t&);
Martin Sustrik's avatar
Martin Sustrik committed
138 139 140 141 142 143
    };
    
}

#endif