GetFEM  5.4.3
dal_singleton.cc
1 /*===========================================================================
2 
3  Copyright (C) 2004-2020 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 
24 namespace dal {
25 
26  singletons_manager::singletons_manager()
27  : lst{}, nb_partitions{lst.num_threads()}
28  {}
29 
30  singletons_manager& singletons_manager::manager(){
31  static singletons_manager x;
32  return x;
33  }
34 
35  void singletons_manager::register_new_singleton(singleton_instance_base *p){
36  register_new_singleton(p, manager().lst.this_thread());
37  }
38 
39  void singletons_manager::register_new_singleton(singleton_instance_base *p, size_t ithread){
40  if (p) manager().lst(ithread).push_back(p);
41  }
42 
43  void singletons_manager::on_partitions_change(){
44  auto new_nb_partitions = manager().lst.num_threads();
45  auto &nb_partitions = manager().nb_partitions;
46  if (new_nb_partitions > nb_partitions){ //not allowing reducing nb. of partitions,
47  manager().lst.on_thread_update(); //as it will invalidate global storage
48  nb_partitions = new_nb_partitions;
49  }
50  }
51 
52  static int level_compare(singleton_instance_base *a, singleton_instance_base *b) {
53  return a->level() < b->level();
54  }
55 
56  singletons_manager::~singletons_manager() {
57  for(size_type i = 0; i != nb_partitions; ++i){
58  std::sort(lst(i).begin(), lst(i).end(), level_compare);
59  for (auto &&p : lst(i)) delete p;
60  }
61  }
62 
63 }/* end of namespace dal */
A simple singleton implementation.
size_t size_type
used as the common size type in the library
Definition: bgeot_poly.h:49
Dynamic Array Library.