libMesh
reference_counter.h
Go to the documentation of this file.
1 // The libMesh Finite Element Library.
2 // Copyright (C) 2002-2019 Benjamin S. Kirk, John W. Peterson, Roy H. Stogner
3 
4 // This library is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU Lesser General Public
6 // License as published by the Free Software Foundation; either
7 // version 2.1 of the License, or (at your option) any later version.
8 
9 // This library is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 // Lesser General Public License for more details.
13 
14 // You should have received a copy of the GNU Lesser General Public
15 // License along with this library; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 
18 
19 
20 #ifndef LIBMESH_REFERENCE_COUNTER_H
21 #define LIBMESH_REFERENCE_COUNTER_H
22 
23 // Local includes
24 #include "libmesh/libmesh_config.h"
25 #include "libmesh/threads.h"
26 #include "libmesh/libmesh.h" // libMesh::on_command_line
27 
28 // C++ includes
29 #include <iostream>
30 #include <string>
31 #include <map>
32 
33 namespace libMesh
34 {
35 
45 {
46 protected:
47 
55 
56 
60  ReferenceCounter(ReferenceCounter && other) noexcept;
61 
62 public:
63 
68 
72  static std::string get_info ();
73 
77  static void print_info (std::ostream & out = libMesh::out);
78 
83  static unsigned int n_objects ()
84  { return _n_objects; }
85 
90  static void enable_print_counter_info();
91  static void disable_print_counter_info();
92 
93 
94 protected:
95 
96 #if defined(LIBMESH_ENABLE_REFERENCE_COUNTING) && defined(DEBUG)
97 
103  void increment_constructor_count (const std::string & name);
104 
110  void increment_destructor_count (const std::string & name);
111 
116  typedef std::map<std::string, std::pair<unsigned int,
117  unsigned int>> Counts;
118 
122  static Counts _counts;
123 
124 #endif
125 
131 
136 
142 };
143 
144 
145 
146 // ------------------------------------------------------------
147 // ReferenceCounter class inline methods
149 {
150  ++_n_objects;
151 }
152 
153 
154 
156 {
157  ++_n_objects;
158 }
159 
160 
161 
163 {
164  ++_n_objects;
165 }
166 
167 
168 
170 {
171  --_n_objects;
172 }
173 
174 
175 
176 
177 
178 
179 #if defined(LIBMESH_ENABLE_REFERENCE_COUNTING) && defined(DEBUG)
180 inline
182 {
183  Threads::spin_mutex::scoped_lock lock(Threads::spin_mtx);
184  std::pair<unsigned int, unsigned int> & p = _counts[name];
185 
186  p.first++;
187 }
188 #endif
189 
190 
191 
192 #if defined(LIBMESH_ENABLE_REFERENCE_COUNTING) && defined(DEBUG)
193 inline
195 {
196  Threads::spin_mutex::scoped_lock lock(Threads::spin_mtx);
197  std::pair<unsigned int, unsigned int> & p = _counts[name];
198 
199  p.second++;
200 }
201 #endif
202 
203 
204 } // namespace libMesh
205 
206 
207 #endif // LIBMESH_REFERENCE_COUNTER_H
std::string name(const ElemQuality q)
static void disable_print_counter_info()
static unsigned int n_objects()
Prints the number of outstanding (created, but not yet destroyed) objects.
void increment_constructor_count(const std::string &name)
Increments the construction counter.
The libMesh namespace provides an interface to certain functionality in the library.
std::map< std::string, std::pair< unsigned int, unsigned int > > Counts
Data structure to log the information.
This is the base class for enabling reference counting.
static Threads::spin_mutex _mutex
Mutual exclusion object to enable thread-safe reference counting.
void increment_destructor_count(const std::string &name)
Increments the destruction counter.
static bool _enable_print_counter
Flag to control whether reference count information is printed when print_info is called...
static void print_info(std::ostream &out=libMesh::out)
Prints the reference information, by default to libMesh::out.
ReferenceCounter()
Constructors.
OStreamProxy out
static void enable_print_counter_info()
Methods to enable/disable the reference counter output from print_info()
static Threads::atomic< unsigned int > _n_objects
The number of objects.
static Counts _counts
Actually holds the data.
static std::string get_info()
Gets a string containing the reference information.
spin_mutex spin_mtx
A convenient spin mutex object which can be used for obtaining locks.