libzmq  master
ZeroMQ C++ Core Engine (LIBZMQ)
own.cpp
Go to the documentation of this file.
1 /*
2  Copyright (c) 2007-2016 Contributors as noted in the AUTHORS file
3 
4  This file is part of libzmq, the ZeroMQ core engine in C++.
5 
6  libzmq is free software; you can redistribute it and/or modify it under
7  the terms of the GNU Lesser General Public License (LGPL) as published
8  by the Free Software Foundation; either version 3 of the License, or
9  (at your option) any later version.
10 
11  As a special exception, the Contributors give you permission to link
12  this library with independent modules to produce an executable,
13  regardless of the license terms of these independent modules, and to
14  copy and distribute the resulting executable under terms of your choice,
15  provided that you also meet, for each linked independent module, the
16  terms and conditions of the license of that module. An independent
17  module is a module which is not derived from or based on this library.
18  If you modify this library, you must extend this exception to your
19  version of the library.
20 
21  libzmq is distributed in the hope that it will be useful, but WITHOUT
22  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
23  FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
24  License for more details.
25 
26  You should have received a copy of the GNU Lesser General Public License
27  along with this program. If not, see <http://www.gnu.org/licenses/>.
28 */
29 
30 #include "precompiled.hpp"
31 #include "own.hpp"
32 #include "err.hpp"
33 #include "io_thread.hpp"
34 
35 zmq::own_t::own_t (class ctx_t *parent_, uint32_t tid_) :
36  object_t (parent_, tid_),
37  terminating (false),
38  sent_seqnum (0),
39  processed_seqnum (0),
40  owner (NULL),
41  term_acks (0)
42 {
43 }
44 
45 zmq::own_t::own_t (io_thread_t *io_thread_, const options_t &options_) :
46  object_t (io_thread_),
47  options (options_),
48  terminating (false),
49  sent_seqnum (0),
50  processed_seqnum (0),
51  owner (NULL),
52  term_acks (0)
53 {
54 }
55 
57 {
58 }
59 
61 {
62  zmq_assert (!owner);
63  owner = owner_;
64 }
65 
67 {
68  // This function may be called from a different thread!
69  sent_seqnum.add (1);
70 }
71 
73 {
74  // Catch up with counter of processed commands.
76 
77  // We may have catched up and still have pending terms acks.
78  check_term_acks ();
79 }
80 
82 {
83  // Specify the owner of the object.
84  object_->set_owner (this);
85 
86  // Plug the object into the I/O thread.
87  send_plug (object_);
88 
89  // Take ownership of the object.
90  send_own (this, object_);
91 }
92 
94 {
95  process_term_req (object_);
96 }
97 
99 {
100  // When shutting down we can ignore termination requests from owned
101  // objects. The termination request was already sent to the object.
102  if (terminating)
103  return;
104 
105  // If I/O object is well and alive let's ask it to terminate.
106  owned_t::iterator it = std::find (owned.begin (), owned.end (), object_);
107 
108  // If not found, we assume that termination request was already sent to
109  // the object so we can safely ignore the request.
110  if (it == owned.end ())
111  return;
112 
113  owned.erase (it);
114  register_term_acks (1);
115 
116  // Note that this object is the root of the (partial shutdown) thus, its
117  // value of linger is used, rather than the value stored by the children.
118  send_term (object_, options.linger);
119 }
120 
122 {
123  // If the object is already being shut down, new owned objects are
124  // immediately asked to terminate. Note that linger is set to zero.
125  if (terminating) {
126  register_term_acks (1);
127  send_term (object_, 0);
128  return;
129  }
130 
131  // Store the reference to the owned object.
132  owned.insert (object_);
133 }
134 
136 {
137  // If termination is already underway, there's no point
138  // in starting it anew.
139  if (terminating)
140  return;
141 
142  // As for the root of the ownership tree, there's no one to terminate it,
143  // so it has to terminate itself.
144  if (!owner) {
146  return;
147  }
148 
149  // If I am an owned object, I'll ask my owner to terminate me.
150  send_term_req (owner, this);
151 }
152 
154 {
155  return terminating;
156 }
157 
158 void zmq::own_t::process_term (int linger_)
159 {
160  // Double termination should never happen.
162 
163  // Send termination request to all owned objects.
164  for (owned_t::iterator it = owned.begin (); it != owned.end (); ++it)
165  send_term (*it, linger_);
166  register_term_acks ((int) owned.size ());
167  owned.clear ();
168 
169  // Start termination process and check whether by chance we cannot
170  // terminate immediately.
171  terminating = true;
172  check_term_acks ();
173 }
174 
176 {
177  term_acks += count_;
178 }
179 
181 {
182  zmq_assert (term_acks > 0);
183  term_acks--;
184 
185  // This may be a last ack we are waiting for before termination...
186  check_term_acks ();
187 }
188 
190 {
192 }
193 
195 {
197  term_acks == 0) {
198 
199  // Sanity check. There should be no active children at this point.
200  zmq_assert (owned.empty ());
201 
202  // The root object has nobody to confirm the termination to.
203  // Other nodes will confirm the termination to the owner.
204  if (owner)
206 
207  // Deallocate the resources.
208  process_destroy ();
209  }
210 }
211 
213 {
214  delete this;
215 }
216 
void check_term_acks()
Definition: own.cpp:194
void process_seqnum()
Definition: own.cpp:72
void process_own(own_t *object_)
Definition: own.cpp:121
void process_term(int linger_)
Definition: own.cpp:158
void process_term_req(own_t *object_)
Definition: own.cpp:98
own_t(zmq::ctx_t *parent_, uint32_t tid_)
Definition: own.cpp:35
virtual ~own_t()
Definition: own.cpp:56
int term_acks
Definition: own.hpp:146
#define zmq_assert(x)
Definition: err.hpp:119
virtual void process_destroy()
Definition: own.cpp:212
void send_own(zmq::own_t *destination_, zmq::own_t *object_)
Definition: object.cpp:215
void register_term_acks(int count_)
Definition: own.cpp:175
bool is_terminating()
Definition: own.cpp:153
void terminate()
Definition: own.cpp:135
owned_t owned
Definition: own.hpp:143
void send_plug(zmq::own_t *destination_, bool inc_seqnum_=true)
Definition: object.cpp:204
void inc_seqnum()
Definition: own.cpp:66
void process_term_ack()
Definition: own.cpp:189
void send_term_req(zmq::own_t *destination_, zmq::own_t *object_)
Definition: object.cpp:294
void send_term_ack(zmq::own_t *destination_)
Definition: object.cpp:313
bool terminating
Definition: own.hpp:128
void send_term(zmq::own_t *destination_, int linger_)
Definition: object.cpp:304
uint64_t processed_seqnum
Definition: own.hpp:134
atomic_counter_t sent_seqnum
Definition: own.hpp:131
void set_owner(own_t *owner_)
Definition: own.cpp:60
integer_t add(integer_t increment_)
void term_child(own_t *object_)
Definition: own.cpp:93
void launch_child(own_t *object_)
Definition: own.cpp:81
own_t * owner
Definition: own.hpp:138
options_t options
Definition: own.hpp:109
void unregister_term_ack()
Definition: own.cpp:180
integer_t get() const