2
Copyright (c) 2010-2011 250bpm s.r.o.
3
Copyright (c) 2010-2011 Other contributors as noted in the AUTHORS file
5
This file is part of 0MQ.
7
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
the Free Software Foundation; either version 3 of the License, or
10
(at your option) any later version.
12
0MQ is distributed in the hope that it will be useful,
13
but WITHOUT ANY WARRANTY; without even the implied warranty of
14
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
GNU Lesser General Public License for more details.
17
You should have received a copy of the GNU Lesser General Public License
18
along with this program. If not, see <http://www.gnu.org/licenses/>.
23
#include "io_thread.hpp"
25
zmq::own_t::own_t (class ctx_t *parent_, uint32_t tid_) :
26
object_t (parent_, tid_),
35
zmq::own_t::own_t (io_thread_t *io_thread_, const options_t &options_) :
36
object_t (io_thread_),
50
void zmq::own_t::set_owner (own_t *owner_)
56
void zmq::own_t::inc_seqnum ()
58
// This function may be called from a different thread!
62
void zmq::own_t::process_seqnum ()
64
// Catch up with counter of processed commands.
67
// We may have catched up and still have pending terms acks.
71
void zmq::own_t::launch_child (own_t *object_)
73
// Specify the owner of the object.
74
object_->set_owner (this);
76
// Plug the object into the I/O thread.
79
// Take ownership of the object.
80
send_own (this, object_);
83
void zmq::own_t::launch_sibling (own_t *object_)
85
// At this point it is important that object is plugged in before its
86
// owner has a chance to terminate it. Thus, 'plug' command is sent before
87
// the 'own' command. Given that the mailbox preserves ordering of
88
// commands, 'term' command from the owner cannot make it to the object
89
// before the already written 'plug' command.
91
// Specify the owner of the object.
92
object_->set_owner (owner);
94
// Plug the object into its I/O thread.
97
// Make parent own the object.
98
send_own (owner, object_);
101
void zmq::own_t::process_term_req (own_t *object_)
103
// When shutting down we can ignore termination requests from owned
104
// objects. The termination request was already sent to the object.
108
// If I/O object is well and alive let's ask it to terminate.
109
owned_t::iterator it = std::find (owned.begin (), owned.end (), object_);
111
// If not found, we assume that termination request was already sent to
112
// the object so we can safely ignore the request.
113
if (it == owned.end ())
117
register_term_acks (1);
119
// Note that this object is the root of the (partial shutdown) thus, its
120
// value of linger is used, rather than the value stored by the children.
121
send_term (object_, options.linger);
124
void zmq::own_t::process_own (own_t *object_)
126
// If the object is already being shut down, new owned objects are
127
// immediately asked to terminate. Note that linger is set to zero.
129
register_term_acks (1);
130
send_term (object_, 0);
134
// Store the reference to the owned object.
135
owned.insert (object_);
138
void zmq::own_t::terminate ()
140
// If termination is already underway, there's no point
141
// in starting it anew.
145
// As for the root of the ownership tree, there's noone to terminate it,
146
// so it has to terminate itself.
148
process_term (options.linger);
152
// If I am an owned object, I'll ask my owner to terminate me.
153
send_term_req (owner, this);
156
bool zmq::own_t::is_terminating ()
161
void zmq::own_t::process_term (int linger_)
163
// Double termination should never happen.
164
zmq_assert (!terminating);
166
// Send termination request to all owned objects.
167
for (owned_t::iterator it = owned.begin (); it != owned.end (); ++it)
168
send_term (*it, linger_);
169
register_term_acks ((int) owned.size ());
172
// Start termination process and check whether by chance we cannot
173
// terminate immediately.
178
void zmq::own_t::register_term_acks (int count_)
183
void zmq::own_t::unregister_term_ack ()
185
zmq_assert (term_acks > 0);
188
// This may be a last ack we are waiting for before termination...
192
void zmq::own_t::process_term_ack ()
194
unregister_term_ack ();
197
void zmq::own_t::check_term_acks ()
199
if (terminating && processed_seqnum == sent_seqnum.get () &&
202
// Sanity check. There should be no active children at this point.
203
zmq_assert (owned.empty ());
205
// The root object has nobody to confirm the termination to.
206
// Other nodes will confirm the termination to the owner.
208
send_term_ack (owner);
210
// Deallocate the resources.
215
void zmq::own_t::process_destroy ()