Skip to content

Commit

Permalink
Size of inproc hwm and swap is sum of peers' hwms and swaps
Browse files Browse the repository at this point in the history
The meat of the patch was contributed by Douglas Creager.
Martin Sustrik implemented storing peer options in inproc
endpoint repository.

Signed-off-by: Martin Sustrik <[email protected]>
  • Loading branch information
sustrik committed Jan 10, 2011
1 parent babdf48 commit bd0ba6e
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 26 deletions.
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ Conrad D. Steenberg <[email protected]>
Dhammika Pathirana <[email protected]>
Dhruva Krishnamurthy <[email protected]>
Dirk O. Kaar <[email protected]>
Douglas Creager <[email protected]>
Erich Heine <[email protected]>
Erik Rigtorp <[email protected]>
Frank Denis <[email protected]>
Expand Down
18 changes: 9 additions & 9 deletions src/ctx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -242,13 +242,12 @@ zmq::io_thread_t *zmq::ctx_t::choose_io_thread (uint64_t affinity_)
return io_threads [result];
}

int zmq::ctx_t::register_endpoint (const char *addr_,
socket_base_t *socket_)
int zmq::ctx_t::register_endpoint (const char *addr_, endpoint_t &endpoint_)
{
endpoints_sync.lock ();

bool inserted = endpoints.insert (endpoints_t::value_type (
std::string (addr_), socket_)).second;
std::string (addr_), endpoint_)).second;
if (!inserted) {
errno = EADDRINUSE;
endpoints_sync.unlock ();
Expand All @@ -265,7 +264,7 @@ void zmq::ctx_t::unregister_endpoints (socket_base_t *socket_)

endpoints_t::iterator it = endpoints.begin ();
while (it != endpoints.end ()) {
if (it->second == socket_) {
if (it->second.socket == socket_) {
endpoints_t::iterator to_erase = it;
it++;
endpoints.erase (to_erase);
Expand All @@ -277,26 +276,27 @@ void zmq::ctx_t::unregister_endpoints (socket_base_t *socket_)
endpoints_sync.unlock ();
}

zmq::socket_base_t *zmq::ctx_t::find_endpoint (const char *addr_)
zmq::endpoint_t zmq::ctx_t::find_endpoint (const char *addr_)
{
endpoints_sync.lock ();

endpoints_t::iterator it = endpoints.find (addr_);
if (it == endpoints.end ()) {
endpoints_sync.unlock ();
errno = ECONNREFUSED;
return NULL;
endpoint_t empty = {NULL, options_t()};
return empty;
}
socket_base_t *endpoint = it->second;
endpoint_t *endpoint = &it->second;

// Increment the command sequence number of the peer so that it won't
// get deallocated until "bind" command is issued by the caller.
// The subsequent 'bind' has to be called with inc_seqnum parameter
// set to false, so that the seqnum isn't incremented twice.
endpoint->inc_seqnum ();
endpoint->socket->inc_seqnum ();

endpoints_sync.unlock ();
return endpoint;
return *endpoint;
}

void zmq::ctx_t::log (zmq_msg_t *msg_)
Expand Down
15 changes: 12 additions & 3 deletions src/ctx.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,18 @@
#include "mutex.hpp"
#include "stdint.hpp"
#include "thread.hpp"
#include "options.hpp"

namespace zmq
{
// 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
{
class socket_base_t *socket;
options_t options;
};

// Context object encapsulates all the global state associated with
// the library.
Expand Down Expand Up @@ -70,9 +79,9 @@ namespace zmq
class io_thread_t *choose_io_thread (uint64_t affinity_);

// Management of inproc endpoints.
int register_endpoint (const char *addr_, class socket_base_t *socket_);
int register_endpoint (const char *addr_, endpoint_t &endpoint_);
void unregister_endpoints (class socket_base_t *socket_);
class socket_base_t *find_endpoint (const char *addr_);
endpoint_t find_endpoint (const char *addr_);

// Logging.
void log (zmq_msg_t *msg_);
Expand Down Expand Up @@ -122,7 +131,7 @@ namespace zmq
mailbox_t **slots;

// List of inproc endpoints within this context.
typedef std::map <std::string, class socket_base_t*> endpoints_t;
typedef std::map <std::string, endpoint_t> endpoints_t;
endpoints_t endpoints;

// Synchronisation of access to the list of inproc endpoints.
Expand Down
6 changes: 3 additions & 3 deletions src/object.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,17 +123,17 @@ void zmq::object_t::process_command (command_t &cmd_)
deallocate_command (&cmd_);
}

int zmq::object_t::register_endpoint (const char *addr_, socket_base_t *socket_)
int zmq::object_t::register_endpoint (const char *addr_, endpoint_t &endpoint_)
{
return ctx->register_endpoint (addr_, socket_);
return ctx->register_endpoint (addr_, endpoint_);
}

void zmq::object_t::unregister_endpoints (socket_base_t *socket_)
{
return ctx->unregister_endpoints (socket_);
}

zmq::socket_base_t *zmq::object_t::find_endpoint (const char *addr_)
zmq::endpoint_t zmq::object_t::find_endpoint (const char *addr_)
{
return ctx->find_endpoint (addr_);
}
Expand Down
4 changes: 2 additions & 2 deletions src/object.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ namespace zmq

// Using following function, socket is able to access global
// repository of inproc endpoints.
int register_endpoint (const char *addr_, class socket_base_t *socket_);
int register_endpoint (const char *addr_, struct endpoint_t &endpoint_);
void unregister_endpoints (class socket_base_t *socket_);
class socket_base_t *find_endpoint (const char *addr_);
struct endpoint_t find_endpoint (const char *addr_);

// Logs an message.
void log (zmq_msg_t *msg_);
Expand Down
34 changes: 25 additions & 9 deletions src/socket_base.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -300,8 +300,10 @@ int zmq::socket_base_t::bind (const char *addr_)
if (rc != 0)
return -1;

if (protocol == "inproc" || protocol == "sys")
return register_endpoint (addr_, this);
if (protocol == "inproc" || protocol == "sys") {
endpoint_t endpoint = {this, options};
return register_endpoint (addr_, endpoint);
}

if (protocol == "tcp" || protocol == "ipc") {

Expand Down Expand Up @@ -361,24 +363,38 @@ int zmq::socket_base_t::connect (const char *addr_)
// as there's no 'reconnect' functionality implemented. Once that
// is in place we should follow generic pipe creation algorithm.

// Find the peer socket.
socket_base_t *peer = find_endpoint (addr_);
if (!peer)
// Find the peer endpoint.
endpoint_t peer = find_endpoint (addr_);
if (!peer.socket)
return -1;

reader_t *inpipe_reader = NULL;
writer_t *inpipe_writer = NULL;
reader_t *outpipe_reader = NULL;
writer_t *outpipe_writer = NULL;


// The total HWM for an inproc connection should be the sum of
// the binder's HWM and the connector's HWM. (Similarly for the
// SWAP.)
int64_t hwm;
if (options.hwm == 0 || peer.options.hwm == 0)
hwm = 0;
else
hwm = options.hwm + peer.options.hwm;
int64_t swap;
if (options.swap == 0 && peer.options.swap == 0)
swap = 0;
else
swap = options.swap + peer.options.swap;

// Create inbound pipe, if required.
if (options.requires_in)
create_pipe (this, peer, options.hwm, options.swap,
create_pipe (this, peer.socket, hwm, swap,
&inpipe_reader, &inpipe_writer);

// Create outbound pipe, if required.
if (options.requires_out)
create_pipe (peer, this, options.hwm, options.swap,
create_pipe (peer.socket, this, hwm, swap,
&outpipe_reader, &outpipe_writer);

// Attach the pipes to this socket object.
Expand All @@ -387,7 +403,7 @@ int zmq::socket_base_t::connect (const char *addr_)
// Attach the pipes to the peer socket. Note that peer's seqnum
// was incremented in find_endpoint function. We don't need it
// increased here.
send_bind (peer, outpipe_reader, inpipe_writer,
send_bind (peer.socket, outpipe_reader, inpipe_writer,
options.identity, false);

return 0;
Expand Down

0 comments on commit bd0ba6e

Please sign in to comment.