libzmq  master
ZeroMQ C++ Core Engine (LIBZMQ)
own.hpp
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 #ifndef __ZMQ_OWN_HPP_INCLUDED__
31 #define __ZMQ_OWN_HPP_INCLUDED__
32 
33 #include <set>
34 #include <algorithm>
35 
36 #include "object.hpp"
37 #include "options.hpp"
38 #include "atomic_counter.hpp"
39 #include "stdint.hpp"
40 
41 namespace zmq
42 {
43 
44  class ctx_t;
45  class io_thread_t;
46 
47  // Base class for objects forming a part of ownership hierarchy.
48  // It handles initialisation and destruction of such objects.
49 
50  class own_t : public object_t
51  {
52  public:
53 
54  // Note that the owner is unspecified in the constructor.
55  // It'll be supplied later on when the object is plugged in.
56 
57  // The object is not living within an I/O thread. It has it's own
58  // thread outside of 0MQ infrastructure.
59  own_t (zmq::ctx_t *parent_, uint32_t tid_);
60 
61  // The object is living within I/O thread.
62  own_t (zmq::io_thread_t *io_thread_, const options_t &options_);
63 
64  // When another owned object wants to send command to this object
65  // it calls this function to let it know it should not shut down
66  // before the command is delivered.
67  void inc_seqnum ();
68 
69  // Use following two functions to wait for arbitrary events before
70  // terminating. Just add number of events to wait for using
71  // register_tem_acks functions. When event occurs, call
72  // remove_term_ack. When number of pending acks reaches zero
73  // object will be deallocated.
74  void register_term_acks (int count_);
75  void unregister_term_ack ();
76 
77  protected:
78 
79  // Launch the supplied object and become its owner.
80  void launch_child (own_t *object_);
81 
82  // Terminate owned object
83  void term_child (own_t *object_);
84 
85  // Ask owner object to terminate this object. It may take a while
86  // while actual termination is started. This function should not be
87  // called more than once.
88  void terminate ();
89 
90  // Returns true if the object is in process of termination.
91  bool is_terminating ();
92 
93  // Derived object destroys own_t. There's no point in allowing
94  // others to invoke the destructor. At the same time, it has to be
95  // virtual so that generic own_t deallocation mechanism destroys
96  // specific type of the owned object correctly.
97  virtual ~own_t ();
98 
99  // Term handler is protected rather than private so that it can
100  // be intercepted by the derived class. This is useful to add custom
101  // steps to the beginning of the termination process.
102  void process_term (int linger_);
103 
104  // A place to hook in when physical destruction of the object
105  // is to be delayed.
106  virtual void process_destroy ();
107 
108  // Socket options associated with this object.
110 
111  private:
112 
113  // Set owner of the object
114  void set_owner (own_t *owner_);
115 
116  // Handlers for incoming commands.
117  void process_own (own_t *object_);
118  void process_term_req (own_t *object_);
119  void process_term_ack ();
120  void process_seqnum ();
121 
122  // Check whether all the pending term acks were delivered.
123  // If so, deallocate this object.
124  void check_term_acks ();
125 
126  // True if termination was already initiated. If so, we can destroy
127  // the object if there are no more child objects or pending term acks.
129 
130  // Sequence number of the last command sent to this object.
132 
133  // Sequence number of the last command processed by this object.
135 
136  // Socket owning this object. It's responsible for shutting down
137  // this object.
139 
140  // List of all objects owned by this socket. We are responsible
141  // for deallocating them before we quit.
142  typedef std::set <own_t*> owned_t;
143  owned_t owned;
144 
145  // Number of events we have to get before we can destroy the object.
147 
148  own_t (const own_t&);
149  const own_t &operator = (const own_t&);
150  };
151 
152 }
153 
154 #endif
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
virtual void process_destroy()
Definition: own.cpp:212
void register_term_acks(int count_)
Definition: own.cpp:175
bool is_terminating()
Definition: own.cpp:153
void terminate()
Definition: own.cpp:135
std::set< own_t * > owned_t
Definition: own.hpp:142
owned_t owned
Definition: own.hpp:143
void inc_seqnum()
Definition: own.cpp:66
void process_term_ack()
Definition: own.cpp:189
bool terminating
Definition: own.hpp:128
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
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
const own_t & operator=(const own_t &)
options_t options
Definition: own.hpp:109
Definition: address.hpp:35
void unregister_term_ack()
Definition: own.cpp:180