From 4e4803e07faad51b17fe05bddad300e863ed1afa Mon Sep 17 00:00:00 2001 From: Martin Hurton Date: Tue, 28 May 2013 13:18:19 +0200 Subject: [PATCH] Rename pipe states to make it more mnemonic --- src/pipe.cpp | 76 +++++++++++++++++++++++++++------------------------- src/pipe.hpp | 30 +++++++++++---------- 2 files changed, 56 insertions(+), 50 deletions(-) diff --git a/src/pipe.cpp b/src/pipe.cpp index 7af9cc45..3b4c8f43 100644 --- a/src/pipe.cpp +++ b/src/pipe.cpp @@ -96,7 +96,9 @@ zmq::blob_t zmq::pipe_t::get_identity () bool zmq::pipe_t::check_read () { - if (unlikely (!in_active || (state != active && state != pending))) + if (unlikely (!in_active)) + return false; + if (unlikely (state != active && state != waiting_for_delimiter)) return false; // Check if there's an item in the pipe. @@ -120,7 +122,9 @@ bool zmq::pipe_t::check_read () bool zmq::pipe_t::read (msg_t *msg_) { - if (unlikely (!in_active || (state != active && state != pending))) + if (unlikely (!in_active)) + return false; + if (unlikely (state != active && state != waiting_for_delimiter)) return false; if (!inpipe->read (msg_)) { @@ -187,7 +191,7 @@ void zmq::pipe_t::rollback () void zmq::pipe_t::flush () { // The peer does not exist anymore at this point. - if (state == terminating) + if (state == term_ack_sent) return; if (outpipe && !outpipe->flush ()) @@ -196,7 +200,7 @@ void zmq::pipe_t::flush () void zmq::pipe_t::process_activate_read () { - if (!in_active && (state == active || state == pending)) { + if (!in_active && (state == active || state == waiting_for_delimiter)) { in_active = true; sink->read_activated (this); } @@ -240,24 +244,24 @@ void zmq::pipe_t::process_pipe_term () { // This is the simple case of peer-induced termination. If there are no // more pending messages to read, or if the pipe was configured to drop - // pending messages, we can move directly to the terminating state. - // Otherwise we'll hang up in pending state till all the pending messages - // are sent. + // pending messages, we can move directly to the term_ack_sent state. + // Otherwise we'll hang up in waiting_for_delimiter state till all + // pending messages are read. if (state == active) { if (!delay) { - state = terminating; + state = term_ack_sent; outpipe = NULL; send_pipe_term_ack (peer); } else - state = pending; + state = waiting_for_delimiter; return; } // Delimiter happened to arrive before the term command. Now we have the - // term command as well, so we can move straight to terminating state. - if (state == delimited) { - state = terminating; + // term command as well, so we can move straight to term_ack_sent state. + if (state == delimiter_received) { + state = term_ack_sent; outpipe = NULL; send_pipe_term_ack (peer); return; @@ -266,8 +270,8 @@ void zmq::pipe_t::process_pipe_term () // This is the case where both ends of the pipe are closed in parallel. // We simply reply to the request by ack and continue waiting for our // own ack. - if (state == terminated) { - state = double_terminated; + if (state == term_req_sent1) { + state = term_req_sent2; outpipe = NULL; send_pipe_term_ack (peer); return; @@ -283,16 +287,16 @@ void zmq::pipe_t::process_pipe_term_ack () zmq_assert (sink); sink->terminated (this); - // In terminating and double_terminated states there's nothing to do. - // Simply deallocate the pipe. In terminated state we have to ack the - // peer before deallocating this side of the pipe. All the other states - // are invalid. - if (state == terminated) { + // In term_ack_sent and term_req_sent2 states there's nothing to do. + // Simply deallocate the pipe. In term_req_sent1 state we have to ack + // the peer before deallocating this side of the pipe. + // All the other states are invalid. + if (state == term_req_sent1) { outpipe = NULL; send_pipe_term_ack (peer); } else - zmq_assert (state == terminating || state == double_terminated); + zmq_assert (state == term_ack_sent || state == term_req_sent2); // We'll deallocate the inbound pipe, the peer will deallocate the outbound // pipe (which is an inbound pipe from its point of view). @@ -316,44 +320,44 @@ void zmq::pipe_t::terminate (bool delay_) delay = delay_; // If terminate was already called, we can ignore the duplicit invocation. - if (state == terminated || state == double_terminated) + if (state == term_req_sent1 || state == term_req_sent2) return; // If the pipe is in the final phase of async termination, it's going to // closed anyway. No need to do anything special here. - else - if (state == terminating) + else + if (state == term_ack_sent) return; // The simple sync termination case. Ask the peer to terminate and wait // for the ack. - else + else if (state == active) { send_pipe_term (peer); - state = terminated; + state = term_req_sent1; } // There are still pending messages available, but the user calls // 'terminate'. We can act as if all the pending messages were read. - else - if (state == pending && !delay) { + else + if (state == waiting_for_delimiter && delay == 0) { outpipe = NULL; send_pipe_term_ack (peer); - state = terminating; + state = term_ack_sent; } // If there are pending messages still availabe, do nothing. - else - if (state == pending) { + else + if (state == waiting_for_delimiter) { } // We've already got delimiter, but not term command yet. We can ignore // the delimiter and ack synchronously terminate as if we were in // active state. - else - if (state == delimited) { + else + if (state == delimiter_received) { send_pipe_term (peer); - state = terminated; + state = term_req_sent1; } // There are no other states. @@ -413,14 +417,14 @@ int zmq::pipe_t::compute_lwm (int hwm_) void zmq::pipe_t::delimit () { if (state == active) { - state = delimited; + state = delimiter_received; return; } - if (state == pending) { + if (state == waiting_for_delimiter) { outpipe = NULL; send_pipe_term_ack (peer); - state = terminating; + state = term_ack_sent; return; } diff --git a/src/pipe.hpp b/src/pipe.hpp index 1611718a..be8ea420 100644 --- a/src/pipe.hpp +++ b/src/pipe.hpp @@ -162,22 +162,24 @@ namespace zmq // Sink to send events to. i_pipe_events *sink; - // State of the pipe endpoint. Active is common state before any - // termination begins. Delimited means that delimiter was read from - // pipe before term command was received. Pending means that term - // command was already received from the peer but there are still - // pending messages to read. Terminating means that all pending - // messages were already read and all we are waiting for is ack from - // the peer. Terminated means that 'terminate' was explicitly called - // by the user. Double_terminated means that user called 'terminate' - // and then we've got term command from the peer as well. + // States of the pipe endpoint: + // active: common state before any termination begins, + // delimiter_received: delimiter was read from pipe before + // term command was received, + // waiting_fo_delimiter: term command was already received + // from the peer but there are still pending messages to read, + // term_ack_sent: all pending messages were already read and + // all we are waiting for is ack from the peer, + // term_req_sent1: 'terminate' was explicitly called by the user, + // term_req_sent2: user called 'terminate' and then we've got + // term command from the peer as well. enum { active, - delimited, - pending, - terminating, - terminated, - double_terminated + delimiter_received, + waiting_for_delimiter, + term_ack_sent, + term_req_sent1, + term_req_sent2 } state; // If true, we receive all the pending inbound messages before