GetFEM++  5.3
dal_singleton.cc
1 /*===========================================================================
2 
3  Copyright (C) 2004-2017 Julien Pommier
4 
5  This file is a part of GetFEM++
6 
7  GetFEM++ is free software; you can redistribute it and/or modify it
8  under the terms of the GNU Lesser General Public License as published
9  by the Free Software Foundation; either version 3 of the License, or
10  (at your option) any later version along with the GCC Runtime Library
11  Exception either version 3.1 or (at your option) any later version.
12  This program is distributed in the hope that it will be useful, but
13  WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14  or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
15  License and GCC Runtime Library Exception for more details.
16  You should have received a copy of the GNU Lesser General Public License
17  along with this program; if not, write to the Free Software Foundation,
18  Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
19 
20 ===========================================================================*/
21 
22 #include "getfem/dal_singleton.h"
23 #include <algorithm>
24 #include "gmm/gmm.h"
25 #include "getfem/getfem_omp.h"
26 
27 
28 namespace dal {
29 
30 
31  singletons_manager::singletons_manager() : lst() {}
32 
33  singletons_manager& singletons_manager::m = manager();
34 
35  singletons_manager& singletons_manager::manager()
36  {
37  static singletons_manager x;
38  return x;
39  }
40 
41 
42  void singletons_manager::register_new_singleton(singleton_instance_base *p)
43  {
44  manager().lst.thrd_cast().push_back(p);
45  }
46 
47  void singletons_manager::register_new_singleton(singleton_instance_base *p, size_t ithread)
48  {
49  manager().lst(ithread).push_back(p);
50  }
51 
52 
53  static int level_compare(singleton_instance_base *a,
54  singleton_instance_base *b)
55  {
56  return a->level() < b->level();
57  }
58 
59  singletons_manager::~singletons_manager() {
60  GMM_ASSERT1(!getfem::me_is_multithreaded_now(),
61  "singletons_manager destructor should"
62  "not be running in parallel !!");
63  //arrange distruction per thread
64  for(size_t i=0;i<getfem::num_threads();i++)
65  {
66  /* sort singletons in increasing levels,
67  lowest levels will be destroyed first */
68  std::sort(lst(i).begin(),lst(i).end(), level_compare);
69  std::vector<singleton_instance_base *>::const_iterator it = lst(i).begin();
70  std::vector<singleton_instance_base *>::const_iterator ite = lst(i).end();
71  for ( ; it != ite; ++it) { delete *it; }
72  }
73  }
74 }
Tools for multithreaded, OpenMP and Boost based parallelization.
A simple singleton implementation.
Dynamic Array Library.
Include common gmm files.