Commit d90b4071 authored by Martin Sustrik's avatar Martin Sustrik

refactoring of pipe/swap interaction

parent 42000d2c
...@@ -27,6 +27,7 @@ ...@@ -27,6 +27,7 @@
zmq::reader_t::reader_t (object_t *parent_, pipe_t *pipe_, zmq::reader_t::reader_t (object_t *parent_, pipe_t *pipe_,
uint64_t lwm_) : uint64_t lwm_) :
object_t (parent_), object_t (parent_),
active (true),
pipe (pipe_), pipe (pipe_),
writer (NULL), writer (NULL),
lwm (lwm_), lwm (lwm_),
...@@ -76,12 +77,14 @@ bool zmq::reader_t::is_delimiter (zmq_msg_t &msg_) ...@@ -76,12 +77,14 @@ bool zmq::reader_t::is_delimiter (zmq_msg_t &msg_)
bool zmq::reader_t::check_read () bool zmq::reader_t::check_read ()
{ {
if (unlikely (terminating)) if (!active)
return false; return false;
// Check if there's an item in the pipe. // Check if there's an item in the pipe.
if (!pipe->check_read ()) if (!pipe->check_read ()) {
active = false;
return false; return false;
}
// If the next item in the pipe is message delimiter, // If the next item in the pipe is message delimiter,
// initiate its termination. // initiate its termination.
...@@ -95,11 +98,13 @@ bool zmq::reader_t::check_read () ...@@ -95,11 +98,13 @@ bool zmq::reader_t::check_read ()
bool zmq::reader_t::read (zmq_msg_t *msg_) bool zmq::reader_t::read (zmq_msg_t *msg_)
{ {
if (unlikely (terminating)) if (!active)
return false; return false;
if (!pipe->read (msg_)) if (!pipe->read (msg_)) {
active = false;
return false; return false;
}
// If delimiter was read, start termination process of the pipe. // If delimiter was read, start termination process of the pipe.
unsigned char *offset = 0; unsigned char *offset = 0;
...@@ -123,6 +128,7 @@ void zmq::reader_t::terminate () ...@@ -123,6 +128,7 @@ void zmq::reader_t::terminate ()
if (terminating) if (terminating)
return; return;
active = false;
terminating = true; terminating = true;
send_pipe_term (writer); send_pipe_term (writer);
} }
...@@ -130,6 +136,7 @@ void zmq::reader_t::terminate () ...@@ -130,6 +136,7 @@ void zmq::reader_t::terminate ()
void zmq::reader_t::process_activate_reader () void zmq::reader_t::process_activate_reader ()
{ {
// Forward the event to the sink (either socket or session). // Forward the event to the sink (either socket or session).
active = true;
sink->activated (this); sink->activated (this);
} }
...@@ -150,38 +157,34 @@ void zmq::reader_t::process_pipe_term_ack () ...@@ -150,38 +157,34 @@ void zmq::reader_t::process_pipe_term_ack ()
zmq::writer_t::writer_t (object_t *parent_, pipe_t *pipe_, reader_t *reader_, zmq::writer_t::writer_t (object_t *parent_, pipe_t *pipe_, reader_t *reader_,
uint64_t hwm_, int64_t swap_size_) : uint64_t hwm_, int64_t swap_size_) :
object_t (parent_), object_t (parent_),
active (true),
pipe (pipe_), pipe (pipe_),
reader (reader_), reader (reader_),
hwm (hwm_), hwm (hwm_),
msgs_read (0), msgs_read (0),
msgs_written (0), msgs_written (0),
msg_store (NULL), swap (NULL),
extra_msg_flag (false),
stalled (false),
sink (NULL), sink (NULL),
terminating (false), swapping (false),
pending_close (false) pending_delimiter (false),
terminating (false)
{ {
// Inform reader about the writer. // Inform reader about the writer.
reader->set_writer (this); reader->set_writer (this);
// Open the swap file, if required.
if (swap_size_ > 0) { if (swap_size_ > 0) {
msg_store = new (std::nothrow) msg_store_t (swap_size_); swap = new (std::nothrow) msg_store_t (swap_size_);
if (msg_store != NULL) { zmq_assert (swap);
if (msg_store->init () < 0) { int rc = swap->init ();
delete msg_store; zmq_assert (rc == 0);
msg_store = NULL;
}
}
} }
} }
zmq::writer_t::~writer_t () zmq::writer_t::~writer_t ()
{ {
if (extra_msg_flag) if (swap)
zmq_msg_close (&extra_msg); delete swap;
delete msg_store;
} }
void zmq::writer_t::set_event_sink (i_writer_events *sink_) void zmq::writer_t::set_event_sink (i_writer_events *sink_)
...@@ -192,72 +195,71 @@ void zmq::writer_t::set_event_sink (i_writer_events *sink_) ...@@ -192,72 +195,71 @@ void zmq::writer_t::set_event_sink (i_writer_events *sink_)
bool zmq::writer_t::check_write () bool zmq::writer_t::check_write ()
{ {
if (terminating) // We've already checked and there's no space free for the new message.
// There's no point in checking once again.
if (unlikely (!active))
return false; return false;
if (pipe_full () && (msg_store == NULL || msg_store->full () || if (unlikely (swapping)) {
extra_msg_flag)) { if (unlikely (swap->full ())) {
stalled = true; active = false;
return false; return false;
} }
}
else {
if (unlikely (pipe_full ())) {
if (swap)
swapping = true;
else {
active = false;
return false;
}
}
}
return true; return true;
} }
bool zmq::writer_t::write (zmq_msg_t *msg_) bool zmq::writer_t::write (zmq_msg_t *msg_)
{ {
if (terminating) if (unlikely (!check_write ()))
return false;
if (!check_write ())
return false; return false;
if (pipe_full ()) { if (unlikely (swapping)) {
if (msg_store->store (msg_)) { bool stored = swap->store (msg_);
zmq_assert (stored);
if (!(msg_->flags & ZMQ_MSG_MORE)) if (!(msg_->flags & ZMQ_MSG_MORE))
msg_store->commit (); swap->commit ();
} else { return true;
extra_msg = *msg_;
extra_msg_flag = true;
}
} }
else {
pipe->write (*msg_, msg_->flags & ZMQ_MSG_MORE); pipe->write (*msg_, msg_->flags & ZMQ_MSG_MORE);
if (!(msg_->flags & ZMQ_MSG_MORE)) if (!(msg_->flags & ZMQ_MSG_MORE))
msgs_written++; msgs_written++;
}
return true; return true;
} }
void zmq::writer_t::rollback () void zmq::writer_t::rollback ()
{ {
if (extra_msg_flag && extra_msg.flags & ZMQ_MSG_MORE) { // Remove incomplete message from the swap.
zmq_msg_close (&extra_msg); if (unlikely (swapping)) {
extra_msg_flag = false; swap->rollback ();
return;
} }
if (msg_store != NULL) // Remove incomplete message from the pipe.
msg_store->rollback ();
zmq_msg_t msg; zmq_msg_t msg;
// Remove all incomplete messages from the pipe.
while (pipe->unwrite (&msg)) { while (pipe->unwrite (&msg)) {
zmq_assert (msg.flags & ZMQ_MSG_MORE); zmq_assert (msg.flags & ZMQ_MSG_MORE);
zmq_msg_close (&msg); zmq_msg_close (&msg);
msgs_written--;
}
if (stalled && check_write ()) {
stalled = false;
zmq_assert (sink);
sink->activated (this);
} }
} }
void zmq::writer_t::flush () void zmq::writer_t::flush ()
{ {
if (!pipe->flush ()) // In the swapping mode, flushing is automatically handled by swap object.
if (!swapping && !pipe->flush ())
send_activate_reader (reader); send_activate_reader (reader);
} }
...@@ -267,19 +269,20 @@ void zmq::writer_t::terminate () ...@@ -267,19 +269,20 @@ void zmq::writer_t::terminate ()
if (terminating) if (terminating)
return; return;
if (msg_store == NULL || (msg_store->empty () && !extra_msg_flag)) // Mark the pipe as not available for writing.
write_delimiter (); active = false;
else
pending_close = true;
}
void zmq::writer_t::write_delimiter ()
{
// Rollback any unfinished messages. // Rollback any unfinished messages.
rollback (); rollback ();
// Push delimiter into the pipe. if (swapping) {
// Trick the compiler to belive that the tag is a valid pointer. pending_delimiter = true;
return;
}
// Push delimiter into the pipe. Trick the compiler to belive that
// the tag is a valid pointer. Note that watermarks are not checked
// thus the delimiter can be written even though the pipe is full.
zmq_msg_t msg; zmq_msg_t msg;
const unsigned char *offset = 0; const unsigned char *offset = 0;
msg.content = (void*) (offset + ZMQ_DELIMITER); msg.content = (void*) (offset + ZMQ_DELIMITER);
...@@ -290,44 +293,47 @@ void zmq::writer_t::write_delimiter () ...@@ -290,44 +293,47 @@ void zmq::writer_t::write_delimiter ()
void zmq::writer_t::process_activate_writer (uint64_t msgs_read_) void zmq::writer_t::process_activate_writer (uint64_t msgs_read_)
{ {
zmq_msg_t msg; // Store the reader's message sequence number.
msgs_read = msgs_read_; msgs_read = msgs_read_;
if (msg_store) {
// Move messages from backing store into pipe. // If we are in the swapping mode, we have some messages in the swap.
while (!pipe_full () && !msg_store->empty ()) { // Given that pipe is now ready for writing we can move part of the
msg_store->fetch(&msg); // swap into the pipe.
// Write message into the pipe. if (swapping) {
zmq_msg_t msg;
while (!pipe_full () && !swap->empty ()) {
swap->fetch(&msg);
pipe->write (msg, msg.flags & ZMQ_MSG_MORE); pipe->write (msg, msg.flags & ZMQ_MSG_MORE);
if (!(msg.flags & ZMQ_MSG_MORE)) if (!(msg.flags & ZMQ_MSG_MORE))
msgs_written++; msgs_written++;
} }
if (!pipe->flush ())
if (extra_msg_flag) { send_activate_reader (reader);
if (!pipe_full ()) {
pipe->write (extra_msg, extra_msg.flags & ZMQ_MSG_MORE);
if (!(extra_msg.flags & ZMQ_MSG_MORE))
msgs_written++;
extra_msg_flag = false;
}
else if (msg_store->store (&extra_msg)) {
if (!(extra_msg.flags & ZMQ_MSG_MORE))
msg_store->commit ();
extra_msg_flag = false;
}
} }
if (pending_close && msg_store->empty () && !extra_msg_flag) { // There are no more messages in the swap. We can switch into
write_delimiter (); // standard in-memory mode.
pending_close = false; if (swap->empty ()) {
} swapping = false;
// Push delimiter into the pipe. Trick the compiler to belive that
// the tag is a valid pointer. Note that watermarks are not checked
// thus the delimiter can be written even though the pipe is full.
if (pending_delimiter) {
zmq_msg_t msg;
const unsigned char *offset = 0;
msg.content = (void*) (offset + ZMQ_DELIMITER);
msg.flags = 0;
pipe->write (msg, false);
flush (); flush ();
return;
}
} }
if (stalled) { // If the writer was non-active before, let's make it active
stalled = false; // (available for writing messages to).
if (!active) {
active = true;
zmq_assert (sink); zmq_assert (sink);
sink->activated (this); sink->activated (this);
} }
......
...@@ -87,6 +87,9 @@ namespace zmq ...@@ -87,6 +87,9 @@ namespace zmq
// Returns true if the message is delimiter; false otherwise. // Returns true if the message is delimiter; false otherwise.
static bool is_delimiter (zmq_msg_t &msg_); static bool is_delimiter (zmq_msg_t &msg_);
// True, if pipe can be read from.
bool active;
// The underlying pipe. // The underlying pipe.
pipe_t *pipe; pipe_t *pipe;
...@@ -127,8 +130,8 @@ namespace zmq ...@@ -127,8 +130,8 @@ namespace zmq
void set_event_sink (i_writer_events *endpoint_); void set_event_sink (i_writer_events *endpoint_);
// Checks whether a message can be written to the pipe. // Checks whether a message can be written to the pipe.
// If writing the message would cause high watermark to be // If writing the message would cause high watermark and (optionally)
// exceeded, the function returns false. // swap to be exceeded, the function returns false.
bool check_write (); bool check_write ();
// Writes a message to the underlying pipe. Returns false if the // Writes a message to the underlying pipe. Returns false if the
...@@ -150,17 +153,17 @@ namespace zmq ...@@ -150,17 +153,17 @@ namespace zmq
uint64_t hwm_, int64_t swap_size_); uint64_t hwm_, int64_t swap_size_);
~writer_t (); ~writer_t ();
void process_activate_writer (uint64_t msgs_read_);
// Command handlers. // Command handlers.
void process_activate_writer (uint64_t msgs_read_);
void process_pipe_term (); void process_pipe_term ();
// Tests whether the pipe is already full. // Tests whether underlying pipe is already full. The swap is not
// taken into account.
bool pipe_full (); bool pipe_full ();
// Write special message to the pipe so that the reader // True, if this object can be written to. Undelying ypipe may be full
// can find out we are finished. // but as long as there's swap space available, this flag is true.
void write_delimiter (); bool active;
// The underlying pipe. // The underlying pipe.
pipe_t *pipe; pipe_t *pipe;
...@@ -178,26 +181,24 @@ namespace zmq ...@@ -178,26 +181,24 @@ namespace zmq
// Number of messages we have written so far. // Number of messages we have written so far.
uint64_t msgs_written; uint64_t msgs_written;
// Pointer to backing store. If NULL, messages are always // Pointer to the message swap. If NULL, messages are always
// kept in main memory. // kept in main memory.
msg_store_t *msg_store; msg_store_t *swap;
bool extra_msg_flag;
zmq_msg_t extra_msg;
// True iff the last attempt to write a message has failed.
bool stalled;
// Sink for the events (either the socket or the session). // Sink for the events (either the socket or the session).
i_writer_events *sink; i_writer_events *sink;
// If true, swap is active. New messages are to be written to the swap.
bool swapping;
// If true, there's a delimiter to be written to the pipe after the
// swap is empied.
bool pending_delimiter;
// True is 'terminate' method was called of 'pipe_term' command // True is 'terminate' method was called of 'pipe_term' command
// arrived from the reader. // arrived from the reader.
bool terminating; bool terminating;
bool pending_close;
writer_t (const writer_t&); writer_t (const writer_t&);
void operator = (const writer_t&); void operator = (const writer_t&);
}; };
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment