2009-07-29 12:07:54 +02:00
|
|
|
/*
|
2010-01-05 08:29:35 +01:00
|
|
|
Copyright (c) 2007-2010 iMatix Corporation
|
2009-07-29 12:07:54 +02:00
|
|
|
|
|
|
|
This file is part of 0MQ.
|
|
|
|
|
|
|
|
0MQ is free software; you can redistribute it and/or modify it under
|
2010-10-30 15:08:28 +02:00
|
|
|
the terms of the GNU Lesser General Public License as published by
|
2009-07-29 12:07:54 +02:00
|
|
|
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
|
2010-10-30 15:08:28 +02:00
|
|
|
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>
|
|
|
|
|
2010-09-01 07:57:38 +02:00
|
|
|
#include "../include/zmq.h"
|
|
|
|
|
2010-04-29 17:31:57 +02:00
|
|
|
#include "signaler.hpp"
|
2010-08-06 17:49:37 +02:00
|
|
|
#include "semaphore.hpp"
|
2009-07-29 12:07:54 +02:00
|
|
|
#include "ypipe.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"
|
2010-02-08 18:37:48 +01: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
|
|
|
{
|
2010-05-05 14:24:54 +02:00
|
|
|
|
|
|
|
// Context object encapsulates all the global state associated with
|
|
|
|
// the library.
|
2009-07-29 12:07:54 +02:00
|
|
|
|
2010-05-05 14:24:54 +02:00
|
|
|
class ctx_t
|
2009-07-29 12:07:54 +02:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
2010-05-05 14:24:54 +02:00
|
|
|
// Create the context object. The argument specifies the size
|
2010-05-05 13:03:26 +02:00
|
|
|
// of I/O thread pool to create.
|
2010-05-05 14:24:54 +02:00
|
|
|
ctx_t (uint32_t io_threads_);
|
2009-07-29 12:07:54 +02:00
|
|
|
|
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
|
|
|
|
2009-08-06 12:51:32 +02:00
|
|
|
// Create a socket.
|
2009-08-09 16:30:22 +02:00
|
|
|
class socket_base_t *create_socket (int type_);
|
2009-07-29 12:07:54 +02:00
|
|
|
|
2010-08-06 17:49:37 +02:00
|
|
|
// Make socket a zombie.
|
2010-08-11 14:09:56 +02:00
|
|
|
void zombify_socket (socket_base_t *socket_);
|
2009-09-04 16:02:41 +02:00
|
|
|
|
2010-08-06 17:49:37 +02:00
|
|
|
// Send command to the destination slot.
|
|
|
|
void send_command (uint32_t slot_, 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).
|
|
|
|
// Returns NULL is no I/O thread is available.
|
|
|
|
class io_thread_t *choose_io_thread (uint64_t affinity_);
|
2009-07-29 12:07:54 +02:00
|
|
|
|
2009-11-21 20:59:55 +01:00
|
|
|
// 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_);
|
|
|
|
|
2010-09-01 07:57:38 +02:00
|
|
|
// Logging.
|
|
|
|
void log (zmq_msg_t *msg_);
|
|
|
|
|
2009-07-29 12:07:54 +02:00
|
|
|
private:
|
|
|
|
|
2010-05-05 14:24:54 +02:00
|
|
|
~ctx_t ();
|
2009-09-04 16:02:41 +02:00
|
|
|
|
2010-08-06 17:49:37 +02:00
|
|
|
// Sockets belonging to this context.
|
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-08-12 08:16:18 +02:00
|
|
|
// List of sockets that were already closed but not yet deallocated.
|
2010-08-06 17:49:37 +02:00
|
|
|
// These sockets still have some pipes and I/O objects attached.
|
2010-08-12 15:03:51 +02:00
|
|
|
typedef std::vector <socket_base_t*> zombies_t;
|
2010-08-06 17:49:37 +02:00
|
|
|
zombies_t zombies;
|
|
|
|
|
|
|
|
// List of unused slots.
|
|
|
|
typedef std::vector <uint32_t> emtpy_slots_t;
|
|
|
|
emtpy_slots_t empty_slots;
|
2010-02-08 18:37:48 +01:00
|
|
|
|
2010-08-06 17:49:37 +02:00
|
|
|
// 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;
|
2010-02-08 18:37:48 +01:00
|
|
|
|
2010-08-06 17:49:37 +02:00
|
|
|
// Object used by zmq_term to wait while all the sockets are closed
|
|
|
|
// by different application threads.
|
|
|
|
semaphore_t no_sockets_sync;
|
2009-07-29 12:07:54 +02:00
|
|
|
|
2010-08-06 17:49:37 +02:00
|
|
|
// 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;
|
2009-07-29 12:07:54 +02:00
|
|
|
|
2010-08-06 17:49:37 +02:00
|
|
|
// This function attempts to deallocate as many zombie sockets as
|
|
|
|
// possible. It must be called within a slot_sync critical section.
|
|
|
|
void dezombify ();
|
2010-02-08 18:37:48 +01:00
|
|
|
|
2009-07-29 12:07:54 +02:00
|
|
|
// I/O threads.
|
|
|
|
typedef std::vector <class io_thread_t*> io_threads_t;
|
|
|
|
io_threads_t io_threads;
|
|
|
|
|
2010-05-04 10:22:16 +02:00
|
|
|
// Array of pointers to signalers for both application and I/O threads.
|
2010-08-06 17:49:37 +02:00
|
|
|
uint32_t slot_count;
|
|
|
|
signaler_t **slots;
|
2009-09-04 16:02:41 +02:00
|
|
|
|
2009-11-21 20:59:55 +01:00
|
|
|
// 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;
|
|
|
|
|
2010-09-01 07:57:38 +02:00
|
|
|
// 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;
|
|
|
|
|
2010-05-05 14:24:54 +02:00
|
|
|
ctx_t (const ctx_t&);
|
|
|
|
void operator = (const ctx_t&);
|
2009-07-29 12:07:54 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|