/usr/include/getfem/dal_singleton.h is in libgetfem++-dev 4.2.1~beta1~svn4482~dfsg-2build1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 | /* -*- c++ -*- (enables emacs c++ mode) */
/*===========================================================================
Copyright (C) 2004-2012 Julien Pommier
This file is a part of GETFEM++
Getfem++ is free software; you can redistribute it and/or modify it
under the terms of the GNU Lesser General Public License as published
by the Free Software Foundation; either version 3 of the License, or
(at your option) any later version along with the GCC Runtime Library
Exception either version 3.1 or (at your option) any later version.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
License and GCC Runtime Library Exception for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program; if not, write to the Free Software Foundation,
Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
As a special exception, you may use this file as it is a part of a free
software library without restriction. Specifically, if other files
instantiate templates or use macros or inline functions from this file,
or you compile this file and link it with other files to produce an
executable, this file does not by itself cause the resulting executable
to be covered by the GNU Lesser General Public License. This exception
does not however invalidate any other reasons why the executable file
might be covered by the GNU Lesser General Public License.
===========================================================================*/
/**@file dal_singleton.h
@author Julien Pommier <Julien.Pommier@insa-toulouse.fr>
@date May 2004.
@brief A simple singleton implementation
Not thread safe, of course.
Correction: (from Andriy Andreykiv)
Singleton was made thread safe for OpenMP
However, now there is a singleton instance for every
thread (singleton is thread local)
*/
#ifndef DAL_SINGLETON
#define DAL_SINGLETON
#include <vector>
#include <memory>
#include "getfem_omp.h"
#include "dal_shared_ptr.h"
namespace dal {
class singleton_instance_base {
public:
virtual ~singleton_instance_base() {}
virtual int level() = 0;
};
class singletons_manager {
protected:
getfem::omp_distribute<std::vector<singleton_instance_base *> > lst;
static shared_ptr<singletons_manager> m;
public:
static shared_ptr<singletons_manager> manager_pointer()
{
if (!m.get()) m.reset(new singletons_manager());
return m;
}
static void register_new_singleton(singleton_instance_base *p);
static void register_new_singleton(singleton_instance_base *p, int ithread);
~singletons_manager();
private:
singletons_manager() {}
};
template <typename T, int LEV> class singleton_instance : public singleton_instance_base {
public:
static getfem::omp_distribute<T*>* instance_;
static getfem::omp_distribute<T*>* instance_pointer()
{
if (!instance_) instance_ = new getfem::omp_distribute<T*>(0);
return instance_;
}
/** Instance from the current thread*/
inline static T& instance() {
T*& tinstance_ = instance_pointer()->thrd_cast();
if (!tinstance_) {
tinstance_ = new T();
singletons_manager::register_new_singleton(new singleton_instance<T,LEV>());
}
return *tinstance_;
}
/**Instance from thread ithread*/
inline static T& instance(int ithread) {
T*& tinstance_ = instance_pointer()->operator()(ithread);
if (!tinstance_) {
tinstance_ = new T();
singletons_manager::register_new_singleton(new singleton_instance<T,LEV>(),ithread);
}
return *tinstance_;
}
int level() { return LEV; }
singleton_instance() {}
~singleton_instance()
{
if (instance_) {
for(size_t i=0;i<getfem::num_threads();i++){
if((*instance_)(i)){delete (*instance_)(i); (*instance_)(i) = 0;}
}
}
delete instance_; instance_=0;
}
};
/** singleton class.
usage:
@code
foo &f = singleton<foo>::instance();
const foo &f = singleton<foo>::const_instance();
@endcode
the LEV template arguments allows one to choose the order of destruction
of the singletons:
lowest LEV will be destroyed first.
*/
template <typename T, int LEV=1> class singleton {
public:
/** Instance from the current thread*/
inline static T& instance() {
return singleton_instance<T,LEV>::instance();
}
inline static const T& const_instance() { return instance(); }
inline static T& instance(int ithread) {
return singleton_instance<T,LEV>::instance(ithread);
}
inline static const T& const_instance(int ithread) { return instance(ithread); }
protected:
singleton() {}
~singleton() {}
private:
singleton(const singleton&);
singleton& operator=(const singleton&);
};
template <typename T, int LEV> getfem::omp_distribute<T*>*
singleton_instance<T,LEV>::instance_ = 0;
}
#endif
|