MADNESS  0.10.1
deferred_cleanup.h
Go to the documentation of this file.
1 /*
2  This file is part of MADNESS.
3 
4  Copyright (C) 2007,2010 Oak Ridge National Laboratory
5 
6  This program is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation; either version 2 of the License, or
9  (at your option) any later version.
10 
11  This program is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with this program; if not, write to the Free Software
18  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 
20  For more information please contact:
21 
22  Robert J. Harrison
23  Oak Ridge National Laboratory
24  One Bethel Valley Road
25  P.O. Box 2008, MS-6367
26 
27  email: harrisonrj@ornl.gov
28  tel: 865-241-3937
29  fax: 865-572-0680
30 
31 
32  $Id$
33 */
34 
35 #ifndef MADNESS_WORLD_DEFERRED_CLEANUP_H__INCLUDED
36 #define MADNESS_WORLD_DEFERRED_CLEANUP_H__INCLUDED
37 
38 #include <madness/world/worldmutex.h> // For Mutex
39 #include <list>
40 #include <memory>
41 
42 namespace madness {
43 
44  // Forward declarations
45  template <typename, typename>
47  class World;
48 
49  namespace detail {
50 
51  template <typename objT>
52  inline void deferred_cleanup(World& world, const std::shared_ptr<objT>& p, bool assume_p_is_unique = false);
53 
54  /// Deferred cleanup of shared_ptr's
55 
56  /// Holds dynamically allocated pointers until it is ready for cleanup.
57  /// \note This object is considered an implementation detail and should
58  /// not be used directly, instead use \c DeferredDeleter with a
59  /// \c std::shared_ptr.
61  public:
62  typedef std::shared_ptr<void> void_ptr; ///< input pointer type
63 
64  private:
65  typedef std::list<void_ptr> void_ptr_list;
66 
67  RecursiveMutex mutex_; ///< Worldwide mutex
68  void_ptr_list deferred_; ///< List of pointers to cleanup
69  bool destroy_; ///< Object destroy mode
70  ///< true = destroy immediate
71  ///< false = destroy deferred (default)
72 
73  // not allowed
76 
77  template <typename objT>
78  friend void deferred_cleanup(World&, const std::shared_ptr<objT>&, bool);
79 
80  /// Access deferred cleanup object of world
81 
82  /// \param w The \c World object that holds the \c DeferredCleanup object
83  /// \return A shared pointer to the deferred cleanup object of world \c w.
84  static std::shared_ptr<DeferredCleanup> get_deferred_cleanup(const World& w);
85 
86  public:
87  /// Construct a deferred deleter object.
89 
90  /// Set the destruction mode
91 
92  /// \param mode true for immediate destruction, false for deferred
93  /// destruction.
94  void destroy(bool mode);
95 
96  /// Get the current destruction mode mode
97 
98  /// \return true for immediate destruction and false for deferred
99  /// destruction.
100  bool destroy() const;
101 
102  /// Adds \c item to cleanup list
103 
104  /// If destroy mode is true then the pointer is destroyed immediately.
105  /// Otherwise it is stored until \c do_cleanup() is called.
106  /// \param obj Object that is ready for destruction
107  void add(const void_ptr& obj);
108 
109  /// Adds \c item to cleanup list
110 
111  /// If destroy mode is true then the pointer is destroyed immediately.
112  /// Otherwise it is stored until \c do_cleanup() is called.
113  /// \tparam objT The object pointer type
114  /// \param obj Object that is ready for destruction
115  template <typename objT>
116  void add(const std::shared_ptr<objT>& obj) {
117  add(std::static_pointer_cast<void>(obj));
118  }
119 
120 
121 
122  /// Deletes/frees any pointers that are in the list
123  void do_cleanup();
124  }; // class DeferredCleanup
125 
126 
127  /// Defer the cleanup of a shared pointer to the end of the next fence
128 
129  /// Call this function before destroying a shared pointer. If the shared
130  /// pointer is the last reference to the object,
131  /// or \p assume_p_is_unique is true,
132  /// it is placed in the deferred deletion list.
133  /// Otherwise, nothing is done with the pointer.
134  template <typename objT>
135  inline void deferred_cleanup(World& world, const std::shared_ptr<objT>& p,
136  bool assume_p_is_unique) {
137  const auto p_is_unique = p.use_count() == 1;
138  if(p_is_unique || assume_p_is_unique) {
139  // This is the last local pointer so we will place it in the
140  // deferred deleter list for later cleanup.
142  }
143  }
144 
145  } // namespace detail
146 } // namespace madness
147 
148 #endif // MADNESS_WORLD_DEFERRED_CLEANUP_H__INCLUDED
double w(double t, double eps)
Definition: DKops.h:22
Definition: deferred_cleanup.h:46
Recursive mutex using pthread mutex operations.
Definition: worldmutex.h:185
A parallel world class.
Definition: world.h:132
Deferred cleanup of shared_ptr's.
Definition: deferred_cleanup.h:60
std::shared_ptr< void > void_ptr
input pointer type
Definition: deferred_cleanup.h:62
friend void deferred_cleanup(World &, const std::shared_ptr< objT > &, bool)
Defer the cleanup of a shared pointer to the end of the next fence.
Definition: deferred_cleanup.h:135
void_ptr_list deferred_
List of pointers to cleanup.
Definition: deferred_cleanup.h:68
static std::shared_ptr< DeferredCleanup > get_deferred_cleanup(const World &w)
Access deferred cleanup object of world.
Definition: deferred_cleanup.cc:71
void add(const std::shared_ptr< objT > &obj)
Adds item to cleanup list.
Definition: deferred_cleanup.h:116
void add(const void_ptr &obj)
Adds item to cleanup list.
Definition: deferred_cleanup.cc:51
void do_cleanup()
Deletes/frees any pointers that are in the list.
Definition: deferred_cleanup.cc:59
DeferredCleanup()
Construct a deferred deleter object.
Definition: deferred_cleanup.h:88
bool destroy_
Definition: deferred_cleanup.h:69
DeferredCleanup & operator=(const DeferredCleanup &)
std::list< void_ptr > void_ptr_list
Definition: deferred_cleanup.h:65
bool destroy() const
Get the current destruction mode mode.
Definition: deferred_cleanup.cc:44
DeferredCleanup(const DeferredCleanup &)
RecursiveMutex mutex_
Worldwide mutex.
Definition: deferred_cleanup.h:67
char * p(char *buf, const char *name, int k, int initial_level, double thresh, int order)
Definition: derivatives.cc:72
void deferred_cleanup(World &world, const std::shared_ptr< objT > &p, bool assume_p_is_unique=false)
Defer the cleanup of a shared pointer to the end of the next fence.
Definition: deferred_cleanup.h:135
File holds all helper structures necessary for the CC_Operator and CC2 class.
Definition: DFParameters.h:10
Implements Mutex, MutexFair, Spinlock, ConditionVariable.