libmesh_singleton.C
Go to the documentation of this file.
1 // The libMesh Finite Element Library.
2 // Copyright (C) 2002-2013 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 // Local includes
22 #include "libmesh/threads.h"
23 
24 // C/C++ includes
25 #include <vector>
26 
27 
28 // --------------------------------------------------------
29 // Local anonymous namespace to hold miscelaneous bits
30 namespace
31 {
32  using namespace libMesh;
33 
34  // Mutex object for required locking
35  typedef Threads::spin_mutex SingletonMutex;
36  SingletonMutex singleton_mtx, setup_mtx;
37 
38  // global list of runtime Singleton objects - created dynamically,
39  // cleaned up in reverse order.
40  typedef std::vector<Singleton*> SingletonList;
41 
42  SingletonList& get_singleton_cache()
43  {
44  static SingletonList singleton_cache;
45  return singleton_cache;
46  }
47 
48  typedef std::vector<Singleton::Setup*> SetupList;
49  SetupList& get_setup_cache()
50  {
51  static SetupList setup_cache;
52  return setup_cache;
53  }
54 
55 } // end anonymous namespace
56 
57 
58 
59 // --------------------------------------------------------
60 // Local anonymous namespace to hold miscelaneous bits
61 namespace libMesh
62 {
63 
65  {
66  SingletonMutex::scoped_lock lock(singleton_mtx);
67 
68  get_singleton_cache().push_back (this);
69  }
70 
71 
72 
74  {
75  get_setup_cache().push_back (this);
76  }
77 
78 
79 
81  {
82  SingletonMutex::scoped_lock lock(setup_mtx);
83 
84  SetupList& setup_cache = get_setup_cache();
85 
86  for (SetupList::iterator it = setup_cache.begin();
87  it!=setup_cache.end(); ++it)
88  {
89  libmesh_assert (*it != NULL);
90  (*it)->setup();
91  }
92  }
93 
94 
95 
97  {
98  SingletonMutex::scoped_lock lock(singleton_mtx);
99 
100  SingletonList& singleton_cache = get_singleton_cache();
101 
102  for (SingletonList::reverse_iterator it = singleton_cache.rbegin();
103  it!=singleton_cache.rend(); ++it)
104  {
105  libmesh_assert (*it != NULL);
106  delete *it;
107  *it = NULL;
108  }
109 
110  singleton_cache.clear();
111  }
112 
113 
114 
115 } // namespace libMesh

Site Created By: libMesh Developers
Last modified: February 07 2014 16:57:05 UTC

Hosted By:
SourceForge.net Logo