own.cpp 5.13 KB
Newer Older
1
/*
2 3
    Copyright (c) 2010-2011 250bpm s.r.o.
    Copyright (c) 2010-2011 Other contributors as noted in the AUTHORS file
4 5 6 7

    This file is part of 0MQ.

    0MQ is free software; you can redistribute it and/or modify it under
8
    the terms of the GNU Lesser General Public License as published by
9 10 11 12 13 14
    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
15
    GNU Lesser General Public License for more details.
16

17
    You should have received a copy of the GNU Lesser General Public License
18 19 20 21 22 23 24
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "own.hpp"
#include "err.hpp"
#include "io_thread.hpp"

Martin Sustrik's avatar
Martin Sustrik committed
25 26
zmq::own_t::own_t (class ctx_t *parent_, uint32_t tid_) :
    object_t (parent_, tid_),
27 28 29 30 31 32 33 34
    terminating (false),
    sent_seqnum (0),
    processed_seqnum (0),
    owner (NULL),
    term_acks (0)
{
}

35
zmq::own_t::own_t (io_thread_t *io_thread_, const options_t &options_) :
36
    object_t (io_thread_),
37
    options (options_),
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
    terminating (false),
    sent_seqnum (0),
    processed_seqnum (0),
    owner (NULL),
    term_acks (0)
{
}

zmq::own_t::~own_t ()
{
}

void zmq::own_t::set_owner (own_t *owner_)
{
    zmq_assert (!owner);
    owner = owner_;
}

void zmq::own_t::inc_seqnum ()
{
    //  This function may be called from a different thread!
    sent_seqnum.add (1);
}

void zmq::own_t::process_seqnum ()
{
    //  Catch up with counter of processed commands.
    processed_seqnum++;

    //  We may have catched up and still have pending terms acks.
    check_term_acks ();
}

void zmq::own_t::launch_child (own_t *object_)
{
    //  Specify the owner of the object.
    object_->set_owner (this);

    //  Plug the object into the I/O thread.
    send_plug (object_);

    //  Take ownership of the object.
    send_own (this, object_);
}

83 84 85 86 87
void zmq::own_t::term_child (own_t *object_)
{
    process_term_req (object_);
}

88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
void zmq::own_t::process_term_req (own_t *object_)
{
    //  When shutting down we can ignore termination requests from owned
    //  objects. The termination request was already sent to the object.
    if (terminating)
        return;

    //  If I/O object is well and alive let's ask it to terminate.
    owned_t::iterator it = std::find (owned.begin (), owned.end (), object_);

    //  If not found, we assume that termination request was already sent to
    //  the object so we can safely ignore the request.
    if (it == owned.end ())
        return;

    owned.erase (it);
    register_term_acks (1);
105 106 107 108

    //  Note that this object is the root of the (partial shutdown) thus, its
    //  value of linger is used, rather than the value stored by the children.
    send_term (object_, options.linger);
109 110 111 112 113
}

void zmq::own_t::process_own (own_t *object_)
{
    //  If the object is already being shut down, new owned objects are
114
    //  immediately asked to terminate. Note that linger is set to zero.
115 116
    if (terminating) {
        register_term_acks (1);
117
        send_term (object_, 0);
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
        return;
    }

    //  Store the reference to the owned object.
    owned.insert (object_);
}

void zmq::own_t::terminate ()
{
    //  If termination is already underway, there's no point
    //  in starting it anew.
    if (terminating)
        return;

    //  As for the root of the ownership tree, there's noone to terminate it,
    //  so it has to terminate itself.
    if (!owner) {
135
        process_term (options.linger);
136 137 138 139 140 141 142
        return;
    }

    //  If I am an owned object, I'll ask my owner to terminate me.
    send_term_req (owner, this);
}

143 144 145 146 147
bool zmq::own_t::is_terminating ()
{
    return terminating;
}

148
void zmq::own_t::process_term (int linger_)
149 150 151 152
{
    //  Double termination should never happen.
    zmq_assert (!terminating);

153
    //  Send termination request to all owned objects.
154
    for (owned_t::iterator it = owned.begin (); it != owned.end (); ++it)
155
        send_term (*it, linger_);
156
    register_term_acks ((int) owned.size ());
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
    owned.clear ();

    //  Start termination process and check whether by chance we cannot
    //  terminate immediately.
    terminating = true;
    check_term_acks ();
}

void zmq::own_t::register_term_acks (int count_)
{
    term_acks += count_;
}

void zmq::own_t::unregister_term_ack ()
{
    zmq_assert (term_acks > 0);
    term_acks--;

    //  This may be a last ack we are waiting for before termination...
    check_term_acks (); 
}

void zmq::own_t::process_term_ack ()
{
    unregister_term_ack ();
}

void zmq::own_t::check_term_acks ()
{
    if (terminating && processed_seqnum == sent_seqnum.get () &&
          term_acks == 0) {

        //  Sanity check. There should be no active children at this point.
        zmq_assert (owned.empty ());

        //  The root object has nobody to confirm the termination to.
        //  Other nodes will confirm the termination to the owner.
        if (owner)
            send_term_ack (owner);

        //  Deallocate the resources.
198
        process_destroy ();
199 200 201
    }
}

202 203 204 205 206
void zmq::own_t::process_destroy ()
{
    delete this;
}