2009-08-27 10:54:28 +02:00
|
|
|
/*
|
2010-01-05 08:29:35 +01:00
|
|
|
Copyright (c) 2007-2010 iMatix Corporation
|
2009-08-27 10:54:28 +02:00
|
|
|
|
|
|
|
This file is part of 0MQ.
|
|
|
|
|
|
|
|
0MQ is free software; you can redistribute it and/or modify it under
|
|
|
|
the terms of the Lesser GNU General Public License as published by
|
|
|
|
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
|
|
|
|
Lesser GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the Lesser GNU General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2010-03-11 20:33:27 +01:00
|
|
|
#include "../include/zmq.h"
|
2009-08-28 16:51:46 +02:00
|
|
|
|
2009-08-27 10:54:28 +02:00
|
|
|
#include "pipe.hpp"
|
|
|
|
|
2009-12-01 14:58:00 +01:00
|
|
|
zmq::reader_t::reader_t (object_t *parent_,
|
2009-08-27 10:54:28 +02:00
|
|
|
uint64_t hwm_, uint64_t lwm_) :
|
|
|
|
object_t (parent_),
|
2009-12-01 14:58:00 +01:00
|
|
|
pipe (NULL),
|
|
|
|
peer (NULL),
|
2009-08-27 10:54:28 +02:00
|
|
|
hwm (hwm_),
|
|
|
|
lwm (lwm_),
|
2010-03-01 10:13:26 +01:00
|
|
|
msgs_read (0),
|
2009-08-27 10:54:28 +02:00
|
|
|
endpoint (NULL)
|
|
|
|
{
|
2010-03-01 10:13:26 +01:00
|
|
|
// Adjust lwm and hwm.
|
|
|
|
if (lwm == 0 || lwm > hwm)
|
|
|
|
lwm = hwm;
|
2009-08-27 10:54:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
zmq::reader_t::~reader_t ()
|
|
|
|
{
|
2009-12-01 15:12:42 +01:00
|
|
|
if (pipe)
|
|
|
|
unregister_pipe (pipe);
|
2009-08-27 10:54:28 +02:00
|
|
|
}
|
|
|
|
|
2009-12-01 14:58:00 +01:00
|
|
|
void zmq::reader_t::set_pipe (pipe_t *pipe_)
|
|
|
|
{
|
|
|
|
zmq_assert (!pipe);
|
|
|
|
pipe = pipe_;
|
2009-12-01 15:12:42 +01:00
|
|
|
peer = &pipe->writer;
|
|
|
|
register_pipe (pipe);
|
2009-12-01 14:58:00 +01:00
|
|
|
}
|
|
|
|
|
2009-09-30 10:08:35 +02:00
|
|
|
bool zmq::reader_t::check_read ()
|
|
|
|
{
|
|
|
|
// Check if there's an item in the pipe.
|
|
|
|
if (pipe->check_read ())
|
|
|
|
return true;
|
|
|
|
|
|
|
|
// If not, deactivate the pipe.
|
|
|
|
endpoint->kill (this);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-08-27 10:54:28 +02:00
|
|
|
bool zmq::reader_t::read (zmq_msg_t *msg_)
|
|
|
|
{
|
2009-09-21 14:39:59 +02:00
|
|
|
if (!pipe->read (msg_)) {
|
|
|
|
endpoint->kill (this);
|
2009-08-28 16:51:46 +02:00
|
|
|
return false;
|
2009-09-21 14:39:59 +02:00
|
|
|
}
|
2009-08-28 16:51:46 +02:00
|
|
|
|
|
|
|
// If delimiter was read, start termination process of the pipe.
|
|
|
|
unsigned char *offset = 0;
|
|
|
|
if (msg_->content == (void*) (offset + ZMQ_DELIMITER)) {
|
|
|
|
if (endpoint)
|
|
|
|
endpoint->detach_inpipe (this);
|
|
|
|
term ();
|
|
|
|
return false;
|
|
|
|
}
|
2009-08-27 10:54:28 +02:00
|
|
|
|
2010-03-27 21:25:40 +01:00
|
|
|
if (!(msg_->flags & ZMQ_MSG_MORE))
|
2010-03-27 09:24:38 +01:00
|
|
|
msgs_read++;
|
|
|
|
|
2010-03-01 10:13:26 +01:00
|
|
|
if (lwm > 0 && msgs_read % lwm == 0)
|
|
|
|
send_reader_info (peer, msgs_read);
|
2009-08-28 16:51:46 +02:00
|
|
|
|
|
|
|
return true;
|
2009-08-27 10:54:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void zmq::reader_t::set_endpoint (i_endpoint *endpoint_)
|
|
|
|
{
|
|
|
|
endpoint = endpoint_;
|
|
|
|
}
|
|
|
|
|
2009-08-28 16:51:46 +02:00
|
|
|
void zmq::reader_t::term ()
|
|
|
|
{
|
|
|
|
endpoint = NULL;
|
|
|
|
send_pipe_term (peer);
|
|
|
|
}
|
|
|
|
|
2009-08-27 10:54:28 +02:00
|
|
|
void zmq::reader_t::process_revive ()
|
|
|
|
{
|
2009-11-19 09:53:49 +01:00
|
|
|
// Beacuse of command throttling mechanism, incoming termination request
|
|
|
|
// may not have been processed before subsequent send.
|
|
|
|
// In that case endpoint is NULL.
|
|
|
|
if (endpoint)
|
|
|
|
endpoint->revive (this);
|
2009-08-27 10:54:28 +02:00
|
|
|
}
|
|
|
|
|
2009-08-28 16:51:46 +02:00
|
|
|
void zmq::reader_t::process_pipe_term_ack ()
|
|
|
|
{
|
|
|
|
peer = NULL;
|
|
|
|
delete pipe;
|
|
|
|
}
|
|
|
|
|
2009-12-01 14:58:00 +01:00
|
|
|
zmq::writer_t::writer_t (object_t *parent_,
|
2009-08-27 10:54:28 +02:00
|
|
|
uint64_t hwm_, uint64_t lwm_) :
|
|
|
|
object_t (parent_),
|
2009-12-01 14:58:00 +01:00
|
|
|
pipe (NULL),
|
|
|
|
peer (NULL),
|
2009-08-27 10:54:28 +02:00
|
|
|
hwm (hwm_),
|
2009-08-28 16:51:46 +02:00
|
|
|
lwm (lwm_),
|
2010-03-01 10:13:26 +01:00
|
|
|
msgs_read (0),
|
|
|
|
msgs_written (0),
|
|
|
|
stalled (false),
|
2009-08-28 16:51:46 +02:00
|
|
|
endpoint (NULL)
|
|
|
|
{
|
2010-03-01 10:13:26 +01:00
|
|
|
// Adjust lwm and hwm.
|
|
|
|
if (lwm == 0 || lwm > hwm)
|
|
|
|
lwm = hwm;
|
2009-08-28 16:51:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void zmq::writer_t::set_endpoint (i_endpoint *endpoint_)
|
|
|
|
{
|
|
|
|
endpoint = endpoint_;
|
|
|
|
}
|
|
|
|
|
2009-08-27 10:54:28 +02:00
|
|
|
zmq::writer_t::~writer_t ()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2009-12-01 14:58:00 +01:00
|
|
|
void zmq::writer_t::set_pipe (pipe_t *pipe_)
|
|
|
|
{
|
|
|
|
zmq_assert (!pipe);
|
|
|
|
pipe = pipe_;
|
2009-12-01 15:12:42 +01:00
|
|
|
peer = &pipe->reader;
|
2009-12-01 14:58:00 +01:00
|
|
|
}
|
|
|
|
|
2010-03-01 10:13:26 +01:00
|
|
|
bool zmq::writer_t::check_write ()
|
2009-08-27 10:54:28 +02:00
|
|
|
{
|
2010-03-01 10:13:26 +01:00
|
|
|
if (pipe_full ()) {
|
|
|
|
stalled = true;
|
|
|
|
return false;
|
|
|
|
}
|
2009-08-27 10:54:28 +02:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2009-09-23 10:22:54 +02:00
|
|
|
bool zmq::writer_t::write (zmq_msg_t *msg_)
|
2009-08-27 10:54:28 +02:00
|
|
|
{
|
2010-03-01 10:13:26 +01:00
|
|
|
if (pipe_full ()) {
|
|
|
|
stalled = true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-05-19 06:31:57 +02:00
|
|
|
pipe->write (*msg_, msg_->flags & ZMQ_MSG_MORE);
|
2010-03-27 21:25:40 +01:00
|
|
|
if (!(msg_->flags & ZMQ_MSG_MORE))
|
2010-03-27 09:24:38 +01:00
|
|
|
msgs_written++;
|
2009-08-27 10:54:28 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-03-09 08:43:20 +01:00
|
|
|
void zmq::writer_t::rollback ()
|
|
|
|
{
|
2010-03-01 10:13:26 +01:00
|
|
|
zmq_msg_t msg;
|
|
|
|
|
2010-05-19 06:31:57 +02:00
|
|
|
// Remove all incomplete messages from the pipe.
|
2010-03-01 10:13:26 +01:00
|
|
|
while (pipe->unwrite (&msg)) {
|
2010-05-19 06:31:57 +02:00
|
|
|
zmq_assert (msg.flags & ZMQ_MSG_MORE);
|
2010-03-09 08:43:20 +01:00
|
|
|
zmq_msg_close (&msg);
|
|
|
|
}
|
|
|
|
|
2010-03-01 10:13:26 +01:00
|
|
|
if (stalled && endpoint != NULL && !pipe_full()) {
|
|
|
|
stalled = false;
|
|
|
|
endpoint->revive (this);
|
|
|
|
}
|
2010-03-09 08:43:20 +01:00
|
|
|
}
|
|
|
|
|
2009-08-27 10:54:28 +02:00
|
|
|
void zmq::writer_t::flush ()
|
|
|
|
{
|
|
|
|
if (!pipe->flush ())
|
|
|
|
send_revive (peer);
|
|
|
|
}
|
|
|
|
|
2009-08-28 16:51:46 +02:00
|
|
|
void zmq::writer_t::term ()
|
|
|
|
{
|
|
|
|
endpoint = NULL;
|
|
|
|
|
2010-03-27 09:24:38 +01:00
|
|
|
// Rollback any unfinished messages.
|
|
|
|
rollback ();
|
|
|
|
|
2009-08-28 16:51:46 +02:00
|
|
|
// Push delimiter into the pipe.
|
|
|
|
// Trick the compiler to belive that the tag is a valid pointer.
|
|
|
|
zmq_msg_t msg;
|
|
|
|
const unsigned char *offset = 0;
|
|
|
|
msg.content = (void*) (offset + ZMQ_DELIMITER);
|
2010-03-09 16:56:53 +01:00
|
|
|
msg.flags = 0;
|
2010-05-19 06:31:57 +02:00
|
|
|
pipe->write (msg, false);
|
2010-06-19 20:46:16 +02:00
|
|
|
flush ();
|
2009-08-28 16:51:46 +02:00
|
|
|
}
|
|
|
|
|
2010-03-01 10:13:26 +01:00
|
|
|
void zmq::writer_t::process_reader_info (uint64_t msgs_read_)
|
|
|
|
{
|
|
|
|
msgs_read = msgs_read_;
|
|
|
|
if (stalled && endpoint != NULL) {
|
|
|
|
stalled = false;
|
|
|
|
endpoint->revive (this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-08-28 16:51:46 +02:00
|
|
|
void zmq::writer_t::process_pipe_term ()
|
|
|
|
{
|
|
|
|
if (endpoint)
|
|
|
|
endpoint->detach_outpipe (this);
|
|
|
|
|
|
|
|
reader_t *p = peer;
|
|
|
|
peer = NULL;
|
|
|
|
send_pipe_term_ack (p);
|
|
|
|
}
|
|
|
|
|
2010-03-01 10:13:26 +01:00
|
|
|
bool zmq::writer_t::pipe_full ()
|
|
|
|
{
|
|
|
|
return hwm > 0 && msgs_written - msgs_read == hwm;
|
|
|
|
}
|
|
|
|
|
2009-08-27 10:54:28 +02:00
|
|
|
zmq::pipe_t::pipe_t (object_t *reader_parent_, object_t *writer_parent_,
|
2010-05-25 15:03:57 +02:00
|
|
|
uint64_t hwm_) :
|
|
|
|
reader (reader_parent_, hwm_, compute_lwm (hwm_)),
|
|
|
|
writer (writer_parent_, hwm_, compute_lwm (hwm_))
|
2009-08-27 10:54:28 +02:00
|
|
|
{
|
2009-12-01 14:58:00 +01:00
|
|
|
reader.set_pipe (this);
|
|
|
|
writer.set_pipe (this);
|
2009-08-27 10:54:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
zmq::pipe_t::~pipe_t ()
|
|
|
|
{
|
2009-08-28 16:51:46 +02:00
|
|
|
// Deallocate all the unread messages in the pipe. We have to do it by
|
|
|
|
// hand because zmq_msg_t is a POD, not a class, so there's no associated
|
|
|
|
// destructor.
|
|
|
|
zmq_msg_t msg;
|
|
|
|
while (read (&msg))
|
|
|
|
zmq_msg_close (&msg);
|
2009-08-27 10:54:28 +02:00
|
|
|
}
|
2010-05-25 15:03:57 +02:00
|
|
|
|
|
|
|
uint64_t zmq::pipe_t::compute_lwm (uint64_t hwm_)
|
|
|
|
{
|
|
|
|
// Following point should be taken into consideration when computing
|
|
|
|
// low watermark:
|
|
|
|
//
|
|
|
|
// 1. LWM has to be less than HWM.
|
|
|
|
// 2. LWM cannot be set to very low value (such as zero) as after filling
|
|
|
|
// the queue it would start to refill only after all the messages are
|
|
|
|
// read from it and thus unnecessarily hold the progress back.
|
|
|
|
// 3. LWM cannot be set to very high value (such as HWM-1) as it would
|
|
|
|
// result in lock-step filling of the queue - if a single message is read
|
|
|
|
// from a full queue, writer thread is resumed to write exactly one
|
|
|
|
// message to the queue and go back to sleep immediately. This would
|
|
|
|
// result in low performance.
|
|
|
|
//
|
|
|
|
// Given the 3. it would be good to keep HWM and LWM as far apart as
|
|
|
|
// possible to reduce the thread switching overhead to almost zero,
|
|
|
|
// say HWM-LWM should be 500 (max_wm_delta).
|
|
|
|
//
|
|
|
|
// That done, we still we have to account for the cases where HWM<500 thus
|
|
|
|
// driving LWM to negative numbers. Let's make LWM 1/2 of HWM in such cases.
|
|
|
|
|
|
|
|
if (hwm_ > max_wm_delta * 2)
|
|
|
|
return hwm_ - max_wm_delta;
|
|
|
|
else
|
|
|
|
return hwm_ / 2;
|
|
|
|
}
|
|
|
|
|