2009-07-29 12:07:54 +02:00
|
|
|
/*
|
2015-01-22 10:32:06 +01:00
|
|
|
Copyright (c) 2007-2015 Contributors as noted in the AUTHORS file
|
2009-07-29 12:07:54 +02:00
|
|
|
|
2015-06-02 22:33:55 +02:00
|
|
|
This file is part of libzmq, the ZeroMQ core engine in C++.
|
2009-07-29 12:07:54 +02:00
|
|
|
|
2015-06-02 22:33:55 +02:00
|
|
|
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
|
2009-07-29 12:07:54 +02:00
|
|
|
(at your option) any later version.
|
|
|
|
|
2015-06-02 22:33:55 +02:00
|
|
|
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.
|
2009-07-29 12:07:54 +02:00
|
|
|
|
2010-10-30 15:08:28 +02:00
|
|
|
You should have received a copy of the GNU Lesser General Public License
|
2009-07-29 12:07:54 +02:00
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2010-05-05 14:24:54 +02:00
|
|
|
#ifndef __ZMQ_CTX_HPP_INCLUDED__
|
|
|
|
#define __ZMQ_CTX_HPP_INCLUDED__
|
2009-07-29 12:07:54 +02:00
|
|
|
|
|
|
|
#include <map>
|
2010-08-12 08:16:18 +02:00
|
|
|
#include <vector>
|
2009-07-29 12:07:54 +02:00
|
|
|
#include <string>
|
2011-02-18 14:15:10 +01:00
|
|
|
#include <stdarg.h>
|
2009-07-29 12:07:54 +02:00
|
|
|
|
2010-11-05 17:39:51 +01:00
|
|
|
#include "mailbox.hpp"
|
2010-08-31 21:03:34 +02:00
|
|
|
#include "array.hpp"
|
2009-07-29 12:07:54 +02:00
|
|
|
#include "config.hpp"
|
|
|
|
#include "mutex.hpp"
|
|
|
|
#include "stdint.hpp"
|
2011-01-10 13:53:30 +01:00
|
|
|
#include "options.hpp"
|
2012-03-19 19:41:20 -05:00
|
|
|
#include "atomic_counter.hpp"
|
2014-07-02 12:07:35 +02:00
|
|
|
#include "thread.hpp"
|
2009-07-29 12:07:54 +02:00
|
|
|
|
2009-08-03 11:30:13 +02:00
|
|
|
namespace zmq
|
2009-07-29 12:07:54 +02:00
|
|
|
{
|
2011-11-09 15:22:20 +01:00
|
|
|
|
|
|
|
class object_t;
|
|
|
|
class io_thread_t;
|
|
|
|
class socket_base_t;
|
|
|
|
class reaper_t;
|
2013-09-12 14:44:44 +01:00
|
|
|
class pipe_t;
|
2011-11-09 15:22:20 +01:00
|
|
|
|
2011-01-10 13:53:30 +01:00
|
|
|
// 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
|
|
|
|
{
|
2012-03-22 07:06:17 +01:00
|
|
|
socket_base_t *socket;
|
2011-01-10 13:53:30 +01:00
|
|
|
options_t options;
|
|
|
|
};
|
2010-05-05 14:24:54 +02:00
|
|
|
|
|
|
|
// Context object encapsulates all the global state associated with
|
|
|
|
// the library.
|
2012-03-22 07:06:17 +01:00
|
|
|
|
2010-05-05 14:24:54 +02:00
|
|
|
class ctx_t
|
2009-07-29 12:07:54 +02:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
2012-03-22 07:06:17 +01:00
|
|
|
// Create the context object.
|
2012-03-19 19:41:20 -05:00
|
|
|
ctx_t ();
|
2009-07-29 12:07:54 +02:00
|
|
|
|
2011-04-09 09:35:34 +02:00
|
|
|
// Returns false if object is not a context.
|
|
|
|
bool check_tag ();
|
|
|
|
|
2009-09-04 16:02:41 +02:00
|
|
|
// 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.
|
2010-08-12 08:16:18 +02:00
|
|
|
int terminate ();
|
2009-07-29 12:07:54 +02:00
|
|
|
|
2013-09-10 13:30:00 +01:00
|
|
|
// 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();
|
|
|
|
|
2012-03-22 07:06:17 +01:00
|
|
|
// Set and get context properties.
|
2012-03-19 19:41:20 -05:00
|
|
|
int set (int option_, int optval_);
|
|
|
|
int get (int option_);
|
2012-03-22 07:06:17 +01:00
|
|
|
|
2011-02-09 15:32:15 +01:00
|
|
|
// Create and destroy a socket.
|
2011-11-09 15:22:20 +01:00
|
|
|
zmq::socket_base_t *create_socket (int type_);
|
|
|
|
void destroy_socket (zmq::socket_base_t *socket_);
|
2009-09-04 16:02:41 +02:00
|
|
|
|
2014-07-02 12:07:35 +02:00
|
|
|
// Start a new thread with proper scheduling parameters.
|
|
|
|
void start_thread (thread_t &thread_, thread_fn *tfn_, void *arg_) const;
|
|
|
|
|
2010-11-05 16:38:52 +01:00
|
|
|
// Send command to the destination thread.
|
|
|
|
void send_command (uint32_t tid_, const command_t &command_);
|
2010-02-08 18:37:48 +01:00
|
|
|
|
2009-07-29 12:07:54 +02:00
|
|
|
// Returns the I/O thread that is the least busy at the moment.
|
2010-09-09 08:25:00 +02:00
|
|
|
// Affinity specifies which I/O threads are eligible (0 = all).
|
2012-03-22 07:06:17 +01:00
|
|
|
// Returns NULL if no I/O thread is available.
|
2011-11-09 15:22:20 +01:00
|
|
|
zmq::io_thread_t *choose_io_thread (uint64_t affinity_);
|
2009-07-29 12:07:54 +02:00
|
|
|
|
2011-02-09 15:32:15 +01:00
|
|
|
// Returns reaper thread object.
|
2011-11-09 15:22:20 +01:00
|
|
|
zmq::object_t *get_reaper ();
|
2011-02-09 15:32:15 +01:00
|
|
|
|
2009-11-21 20:59:55 +01:00
|
|
|
// Management of inproc endpoints.
|
2014-05-23 10:53:58 +02:00
|
|
|
int register_endpoint (const char *addr_, const endpoint_t &endpoint_);
|
2014-07-09 09:57:28 +02:00
|
|
|
int unregister_endpoint (const std::string &addr_, socket_base_t *socket_);
|
2011-11-09 15:22:20 +01:00
|
|
|
void unregister_endpoints (zmq::socket_base_t *socket_);
|
2011-01-10 13:53:30 +01:00
|
|
|
endpoint_t find_endpoint (const char *addr_);
|
2014-05-21 13:05:56 +02:00
|
|
|
void pend_connection (const std::string &addr_,
|
|
|
|
const endpoint_t &endpoint_, pipe_t **pipes_);
|
2013-09-12 18:09:37 +01:00
|
|
|
void connect_pending (const char *addr_, zmq::socket_base_t *bind_socket_);
|
2009-11-21 20:59:55 +01:00
|
|
|
|
2011-02-09 15:32:15 +01:00
|
|
|
enum {
|
|
|
|
term_tid = 0,
|
|
|
|
reaper_tid = 1
|
|
|
|
};
|
|
|
|
|
2012-01-27 12:23:32 -08:00
|
|
|
~ctx_t ();
|
2012-05-21 20:47:11 +01:00
|
|
|
|
2009-07-29 12:07:54 +02:00
|
|
|
private:
|
|
|
|
|
2014-05-21 13:05:56 +02:00
|
|
|
struct pending_connection_t
|
|
|
|
{
|
|
|
|
endpoint_t endpoint;
|
|
|
|
pipe_t* connect_pipe;
|
|
|
|
pipe_t* bind_pipe;
|
|
|
|
};
|
2009-09-04 16:02:41 +02:00
|
|
|
|
2011-04-09 09:35:34 +02:00
|
|
|
// Used to check whether the object is a context.
|
|
|
|
uint32_t tag;
|
|
|
|
|
2011-02-09 15:32:15 +01:00
|
|
|
// 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.
|
2010-08-31 21:03:34 +02:00
|
|
|
typedef array_t <socket_base_t> sockets_t;
|
2010-08-06 17:49:37 +02:00
|
|
|
sockets_t sockets;
|
|
|
|
|
2010-11-05 16:38:52 +01:00
|
|
|
// List of unused thread slots.
|
2013-10-08 00:40:30 +03:00
|
|
|
typedef std::vector <uint32_t> empty_slots_t;
|
|
|
|
empty_slots_t empty_slots;
|
2010-02-08 18:37:48 +01:00
|
|
|
|
2012-03-22 07:06:17 +01:00
|
|
|
// If true, zmq_init has been called but no socket has been created
|
|
|
|
// yet. Launching of I/O threads is delayed.
|
2012-03-19 19:41:20 -05:00
|
|
|
bool starting;
|
|
|
|
|
2011-02-09 15:32:15 +01:00
|
|
|
// If true, zmq_term was already called.
|
|
|
|
bool terminating;
|
2009-07-29 12:07:54 +02:00
|
|
|
|
2010-08-06 17:49:37 +02:00
|
|
|
// Synchronisation of accesses to global slot-related data:
|
2011-02-09 15:32:15 +01:00
|
|
|
// sockets, empty_slots, terminating. It also synchronises
|
2012-03-22 11:03:32 -05:00
|
|
|
// access to zombie sockets as such (as opposed to slots) and provides
|
2010-08-06 17:49:37 +02:00
|
|
|
// a memory barrier to ensure that all CPU cores see the same data.
|
|
|
|
mutex_t slot_sync;
|
2009-07-29 12:07:54 +02:00
|
|
|
|
2011-02-09 15:32:15 +01:00
|
|
|
// The reaper thread.
|
2011-11-09 15:22:20 +01:00
|
|
|
zmq::reaper_t *reaper;
|
2010-02-08 18:37:48 +01:00
|
|
|
|
2009-07-29 12:07:54 +02:00
|
|
|
// I/O threads.
|
2011-11-09 15:22:20 +01:00
|
|
|
typedef std::vector <zmq::io_thread_t*> io_threads_t;
|
2009-07-29 12:07:54 +02:00
|
|
|
io_threads_t io_threads;
|
|
|
|
|
2010-11-05 17:39:51 +01:00
|
|
|
// Array of pointers to mailboxes for both application and I/O threads.
|
2010-08-06 17:49:37 +02:00
|
|
|
uint32_t slot_count;
|
2015-02-11 00:01:50 +02:00
|
|
|
i_mailbox **slots;
|
2009-09-04 16:02:41 +02:00
|
|
|
|
2011-02-09 15:32:15 +01:00
|
|
|
// Mailbox for zmq_term thread.
|
|
|
|
mailbox_t term_mailbox;
|
|
|
|
|
2009-11-21 20:59:55 +01:00
|
|
|
// List of inproc endpoints within this context.
|
2011-01-10 13:53:30 +01:00
|
|
|
typedef std::map <std::string, endpoint_t> endpoints_t;
|
2009-11-21 20:59:55 +01:00
|
|
|
endpoints_t endpoints;
|
|
|
|
|
2013-09-12 14:44:44 +01:00
|
|
|
// List of inproc connection endpoints pending a bind
|
2013-09-12 18:09:37 +01:00
|
|
|
typedef std::multimap <std::string, pending_connection_t> pending_connections_t;
|
2013-09-12 14:44:44 +01:00
|
|
|
pending_connections_t pending_connections;
|
|
|
|
|
2009-11-21 20:59:55 +01:00
|
|
|
// Synchronisation of access to the list of inproc endpoints.
|
|
|
|
mutex_t endpoints_sync;
|
|
|
|
|
2012-03-19 19:41:20 -05:00
|
|
|
// 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;
|
|
|
|
|
2014-11-17 11:56:59 +01:00
|
|
|
// Does context wait (possibly forever) on termination?
|
|
|
|
bool blocky;
|
|
|
|
|
2013-01-31 21:52:30 +01:00
|
|
|
// Is IPv6 enabled on this context?
|
|
|
|
bool ipv6;
|
|
|
|
|
2014-07-02 12:07:35 +02:00
|
|
|
// Thread scheduling parameters.
|
|
|
|
int thread_priority;
|
|
|
|
int thread_sched_policy;
|
|
|
|
|
2012-03-19 19:41:20 -05:00
|
|
|
// Synchronisation of access to context options.
|
|
|
|
mutex_t opt_sync;
|
2012-03-22 07:06:17 +01:00
|
|
|
|
2010-05-05 14:24:54 +02:00
|
|
|
ctx_t (const ctx_t&);
|
2011-01-13 11:44:23 +01:00
|
|
|
const ctx_t &operator = (const ctx_t&);
|
2013-08-31 21:17:11 +10:00
|
|
|
|
|
|
|
#ifdef HAVE_FORK
|
|
|
|
// the process that created this context. Used to detect forking.
|
|
|
|
pid_t pid;
|
|
|
|
#endif
|
2013-09-14 17:27:18 +01:00
|
|
|
enum side { connect_side, bind_side };
|
2014-05-21 13:05:56 +02:00
|
|
|
void connect_inproc_sockets(zmq::socket_base_t *bind_socket_, options_t& bind_options, const pending_connection_t &pending_connection_, side side_);
|
2009-07-29 12:07:54 +02:00
|
|
|
};
|
2012-03-22 07:06:17 +01:00
|
|
|
|
2009-07-29 12:07:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|