Import Geant4 10.7.0.beta source tree
This commit is contained in:
@@ -23,101 +23,108 @@
|
||||
// * acceptance of all terms of the Geant4 Software license. *
|
||||
// ********************************************************************
|
||||
//
|
||||
// G4ThreadLocalSingleton
|
||||
//
|
||||
// ---------------------------------------------------------------
|
||||
// GEANT 4 class header file
|
||||
// Class description:
|
||||
//
|
||||
// Class Description:
|
||||
// This class implements a thread-private "singleton". Being thread
|
||||
// private the singleton is not a singleton in the term, but a different
|
||||
// instance existis for each thread.
|
||||
// This class is a wrapper around the real object that we need to
|
||||
// make singleton.
|
||||
// This class implements a thread-private "singleton". Being thread
|
||||
// private the singleton is not a singleton in the term, but a different
|
||||
// instance exists for each thread.
|
||||
// This class is a wrapper around the real object that we need to
|
||||
// make singleton.
|
||||
//
|
||||
// Limitation:
|
||||
// The object that is made thread-private singleton, should not
|
||||
// contain any G4ThreadLocal data member. Note that in general,
|
||||
// if object is to be thread-private it is unnecessary to mark
|
||||
// The object that is made thread-private singleton should not
|
||||
// contain any thread-local data member. Note that in general,
|
||||
// if an object is to be thread-private it is unnecessary to mark
|
||||
// any data-member as G4ThreadLocal.
|
||||
//
|
||||
//
|
||||
// Performance issues:
|
||||
// This class uses locks and mutexes.
|
||||
//
|
||||
// Example:
|
||||
// This is the singleton patter often found in G4 (sequential):
|
||||
// class G4Class {
|
||||
// private:
|
||||
// static G4Class* instance;
|
||||
// G4Class() { ... }
|
||||
// public:
|
||||
// static G4Class* GetInstance() {
|
||||
// static G4Class theInstance;
|
||||
// if ( instance == 0 ) instance = &theInstance;
|
||||
// return instance;
|
||||
// }
|
||||
// };
|
||||
// This is transformed to the following to implement a thread-local
|
||||
// singleton:
|
||||
// class G4Class {
|
||||
// private:
|
||||
// static G4ThreadLocal G4Class* instance;
|
||||
// G4Class() { ... }
|
||||
// public:
|
||||
// static G4Class* GetInstance() {
|
||||
// if ( instance == 0 ) instance = new G4Class;
|
||||
// return instance;
|
||||
// }
|
||||
// };
|
||||
// Note that this class also has a memory leak.
|
||||
// This is the singleton pattern often found in Geant4 (sequential):
|
||||
// class G4Class
|
||||
// {
|
||||
// private:
|
||||
// static G4Class* instance;
|
||||
// G4Class() { ... }
|
||||
// public:
|
||||
// static G4Class* GetInstance()
|
||||
// {
|
||||
// static G4Class theInstance;
|
||||
// if ( instance == nullptr ) instance = &theInstance;
|
||||
// return instance;
|
||||
// }
|
||||
// };
|
||||
// This is transformed to the following to implement a thread-local
|
||||
// singleton:
|
||||
// class G4Class
|
||||
// {
|
||||
// private:
|
||||
// static G4ThreadLocal G4Class* instance;
|
||||
// G4Class() { ... }
|
||||
// public:
|
||||
// static G4Class* GetInstance()
|
||||
// {
|
||||
// if ( instance == nullptr ) instance = new G4Class;
|
||||
// return instance;
|
||||
// }
|
||||
// };
|
||||
// Note that this class also has a memory leak.
|
||||
//
|
||||
// This class can be used as follows:
|
||||
// class G4Class {
|
||||
// friend class G4ThreadLocalSingleton<G4Class>;
|
||||
// private:
|
||||
// G4Class() { ... }
|
||||
// public:
|
||||
// static G4Class* GetInstance() {
|
||||
// static G4ThreadLocalSingleton<G4Class> instance;
|
||||
// return instance.Instance();
|
||||
// }
|
||||
// };
|
||||
// Each thread has its own instance of G4Class.
|
||||
// Deletion of G4Class instances is done at end of program.
|
||||
// Note the "friend" statement.
|
||||
//
|
||||
// History:
|
||||
// 28 October 2013: A. Dotti - First implementation
|
||||
// This class can be used as follows:
|
||||
// class G4Class
|
||||
// {
|
||||
// friend class G4ThreadLocalSingleton<G4Class>;
|
||||
// private:
|
||||
// G4Class() { ... }
|
||||
// public:
|
||||
// static G4Class* GetInstance()
|
||||
// {
|
||||
// static G4ThreadLocalSingleton<G4Class> instance;
|
||||
// return instance.Instance();
|
||||
// }
|
||||
// };
|
||||
// Each thread has its own instance of G4Class.
|
||||
// Deletion of G4Class instances is done at end of program.
|
||||
// Note the "friend" statement.
|
||||
|
||||
// Author: A.Dotti, 28 October 2013
|
||||
// --------------------------------------------------------------------
|
||||
#ifndef G4TLSSINGLETON_HH
|
||||
#define G4TLSSINGLETON_HH
|
||||
#define G4TLSSINGLETON_HH 1
|
||||
|
||||
//Debug this code
|
||||
//#define g4tlssdebug 1
|
||||
|
||||
#include "G4Cache.hh"
|
||||
#include <list>
|
||||
|
||||
//Forward declaration. See G4AutoDelete.hh
|
||||
namespace G4AutoDelete {
|
||||
template<class T>
|
||||
#include "G4AutoLock.hh"
|
||||
#include "G4Cache.hh"
|
||||
|
||||
// Forward declaration. See G4AutoDelete.hh
|
||||
//
|
||||
namespace G4AutoDelete
|
||||
{
|
||||
template <class T>
|
||||
void Register(T*);
|
||||
}
|
||||
|
||||
template<class T>
|
||||
class G4ThreadLocalSingleton : private G4Cache<T*> {
|
||||
template <class T>
|
||||
class G4ThreadLocalSingleton : private G4Cache<T*>
|
||||
{
|
||||
friend void G4AutoDelete::Register<T>(T*);
|
||||
public:
|
||||
|
||||
public:
|
||||
G4ThreadLocalSingleton();
|
||||
//Creates thread-local singleton manager
|
||||
// Creates thread-local singleton manager
|
||||
|
||||
~G4ThreadLocalSingleton();
|
||||
~G4ThreadLocalSingleton();
|
||||
|
||||
T* Instance() const;
|
||||
//Returns a pointer to a thread-private instance of T
|
||||
T* Instance() const;
|
||||
// Returns a pointer to a thread-private instance of T
|
||||
|
||||
private:
|
||||
G4ThreadLocalSingleton( G4ThreadLocalSingleton& rhs);// {}
|
||||
void Register(T* i) const;
|
||||
private:
|
||||
G4ThreadLocalSingleton(G4ThreadLocalSingleton& rhs);
|
||||
void Register(T* i) const;
|
||||
|
||||
void Clear();
|
||||
|
||||
@@ -125,52 +132,59 @@ private:
|
||||
mutable G4Mutex listm;
|
||||
};
|
||||
|
||||
//=============================================================
|
||||
// Inline methods implementation
|
||||
//=============================================================
|
||||
|
||||
//=============================================================
|
||||
// Implementation details follow
|
||||
//=============================================================
|
||||
#include "G4AutoLock.hh"
|
||||
template<class T>
|
||||
G4ThreadLocalSingleton<T>::G4ThreadLocalSingleton() : G4Cache<T*>() {
|
||||
template <class T>
|
||||
G4ThreadLocalSingleton<T>::G4ThreadLocalSingleton()
|
||||
: G4Cache<T*>()
|
||||
{
|
||||
G4MUTEXINIT(listm);
|
||||
G4Cache<T*>::Put(static_cast<T*>(0));
|
||||
G4Cache<T*>::Put(static_cast<T*>(0));
|
||||
}
|
||||
|
||||
template<class T>
|
||||
G4ThreadLocalSingleton<T>::~G4ThreadLocalSingleton() {
|
||||
template <class T>
|
||||
G4ThreadLocalSingleton<T>::~G4ThreadLocalSingleton()
|
||||
{
|
||||
Clear();
|
||||
G4MUTEXDESTROY(listm);
|
||||
}
|
||||
|
||||
template<class T>
|
||||
T* G4ThreadLocalSingleton<T>::Instance() const {
|
||||
T* instance = G4Cache<T*>::Get();
|
||||
if ( instance == static_cast<T*>(0) ) {
|
||||
instance = new T;
|
||||
G4Cache<T*>::Put( instance );
|
||||
Register(instance);
|
||||
}
|
||||
return instance;
|
||||
template <class T>
|
||||
T* G4ThreadLocalSingleton<T>::Instance() const
|
||||
{
|
||||
T* instance = G4Cache<T*>::Get();
|
||||
if(instance == static_cast<T*>(0))
|
||||
{
|
||||
instance = new T;
|
||||
G4Cache<T*>::Put(instance);
|
||||
Register(instance);
|
||||
}
|
||||
return instance;
|
||||
}
|
||||
|
||||
template<class T>
|
||||
G4ThreadLocalSingleton<T>::G4ThreadLocalSingleton( G4ThreadLocalSingleton&) {}
|
||||
template<class T>
|
||||
void G4ThreadLocalSingleton<T>::Register(T* i) const {
|
||||
template <class T>
|
||||
G4ThreadLocalSingleton<T>::G4ThreadLocalSingleton(G4ThreadLocalSingleton&)
|
||||
{}
|
||||
|
||||
template <class T>
|
||||
void G4ThreadLocalSingleton<T>::Register(T* i) const
|
||||
{
|
||||
G4AutoLock l(&listm);
|
||||
instances.push_back(i);
|
||||
}
|
||||
|
||||
|
||||
template<class T>
|
||||
void G4ThreadLocalSingleton<T>::Clear() {
|
||||
G4AutoLock l(&listm);
|
||||
while ( ! instances.empty() )
|
||||
{
|
||||
T* thisinst = instances.front();
|
||||
instances.pop_front();
|
||||
if ( thisinst != 0 ) delete thisinst;
|
||||
}
|
||||
template <class T>
|
||||
void G4ThreadLocalSingleton<T>::Clear()
|
||||
{
|
||||
G4AutoLock l(&listm);
|
||||
while(!instances.empty())
|
||||
{
|
||||
T* thisinst = instances.front();
|
||||
instances.pop_front();
|
||||
delete thisinst;
|
||||
}
|
||||
}
|
||||
|
||||
#endif //G4TLSSINGLETON_HH
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user