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] [ 10.5.p1 ] [ 10.6 ] [ 10.6.p1 ] [ 10.6.p2 ] [ 10.6.p3 ] [ 10.7 ] [ 10.7.p1 ] [ 10.7.p2 ] [ 10.7.p3 ] [ 10.7.p4 ] [ 11.0 ] [ 11.0.p1 ] [ 11.0.p2 ] [ 11.0.p3, ] [ 11.0.p4 ] [ 11.1 ] [ 11.1.1 ] [ 11.1.2 ] [ 11.1.3 ] [ 11.2 ] [ 11.2.1 ] [ 11.2.2 ] [ 11.3.0 ]

Diff markup

Differences between /global/management/include/G4ThreadLocalSingleton.hh (Version 11.3.0) and /global/management/include/G4ThreadLocalSingleton.hh (Version 10.5.p1)


  1 //                                                  1 //
  2 // *******************************************      2 // ********************************************************************
  3 // * License and Disclaimer                         3 // * License and Disclaimer                                           *
  4 // *                                                4 // *                                                                  *
  5 // * The  Geant4 software  is  copyright of th      5 // * The  Geant4 software  is  copyright of the Copyright Holders  of *
  6 // * the Geant4 Collaboration.  It is provided      6 // * the Geant4 Collaboration.  It is provided  under  the terms  and *
  7 // * conditions of the Geant4 Software License      7 // * conditions of the Geant4 Software License,  included in the file *
  8 // * LICENSE and available at  http://cern.ch/      8 // * LICENSE and available at  http://cern.ch/geant4/license .  These *
  9 // * include a list of copyright holders.           9 // * include a list of copyright holders.                             *
 10 // *                                               10 // *                                                                  *
 11 // * Neither the authors of this software syst     11 // * Neither the authors of this software system, nor their employing *
 12 // * institutes,nor the agencies providing fin     12 // * institutes,nor the agencies providing financial support for this *
 13 // * work  make  any representation or  warran     13 // * work  make  any representation or  warranty, express or implied, *
 14 // * regarding  this  software system or assum     14 // * regarding  this  software system or assume any liability for its *
 15 // * use.  Please see the license in the file      15 // * use.  Please see the license in the file  LICENSE  and URL above *
 16 // * for the full disclaimer and the limitatio     16 // * for the full disclaimer and the limitation of liability.         *
 17 // *                                               17 // *                                                                  *
 18 // * This  code  implementation is the result      18 // * This  code  implementation is the result of  the  scientific and *
 19 // * technical work of the GEANT4 collaboratio     19 // * technical work of the GEANT4 collaboration.                      *
 20 // * By using,  copying,  modifying or  distri     20 // * By using,  copying,  modifying or  distributing the software (or *
 21 // * any work based  on the software)  you  ag     21 // * any work based  on the software)  you  agree  to acknowledge its *
 22 // * use  in  resulting  scientific  publicati     22 // * use  in  resulting  scientific  publications,  and indicate your *
 23 // * acceptance of all terms of the Geant4 Sof     23 // * acceptance of all terms of the Geant4 Software license.          *
 24 // *******************************************     24 // ********************************************************************
 25 //                                                 25 //
 26 // G4ThreadLocalSingleton                      << 
 27 //                                                 26 //
 28 // Class description:                          <<  27 // ---------------------------------------------------------------
                                                   >>  28 // GEANT 4 class header file
 29 //                                                 29 //
 30 // This class implements a thread-private "sin <<  30 // Class Description:
 31 // private the singleton is not a singleton in <<  31 //   This class implements a thread-private "singleton". Being thread
 32 // instance exists for each thread.            <<  32 //   private the singleton is not a singleton in the term, but a different
 33 // This class is a wrapper around the real obj <<  33 //   instance existis for each thread.
 34 // make singleton.                             <<  34 //   This class is a wrapper around the real object that we need to
                                                   >>  35 //   make singleton.
 35 //                                                 36 //
 36 // Limitation:                                     37 // Limitation:
 37 //   The object that is made thread-private si <<  38 //   The object that is made thread-private singleton, should not
 38 //   contain any thread-local data member. Not <<  39 //   contain any G4ThreadLocal data member. Note that in general,
 39 //   if an object is to be thread-private it i <<  40 //   if object is to be thread-private it is unnecessary to mark
 40 //   any data-member as G4ThreadLocal.             41 //   any data-member as G4ThreadLocal.
 41 //                                             <<  42 //   
 42 // Performance issues:                             43 // Performance issues:
 43 //   This class uses locks and mutexes.            44 //   This class uses locks and mutexes.
 44 //                                                 45 //
 45 // Example:                                        46 // Example:
 46 //   This is the singleton pattern often found <<  47 //   This is the singleton patter often found in G4 (sequential):
 47 //   class G4Class                             <<  48 //   class G4Class {
 48 //   {                                         <<  49 //      private:
 49 //     private:                                <<  50 //         static G4Class* instance;
 50 //       static G4Class* instance;             <<  51 //         G4Class() { ... }
 51 //        G4Class() { ... }                    <<  52 //      public:
 52 //     public:                                 <<  53 //         static G4Class* GetInstance() {
 53 //       static G4Class* GetInstance()         <<  54 //              static G4Class theInstance;
 54 //       {                                     <<  55 //              if ( instance == 0 ) instance = &theInstance;
 55 //         static G4Class theInstance;         <<  56 //              return instance;
 56 //         if ( instance == nullptr ) instance <<  57 //         }
 57 //         return instance;                    <<  58 //  };
 58 //       }                                     <<  59 //  This is transformed to the following to implement a thread-local
 59 //   };                                        <<  60 //  singleton:
 60 //   This is transformed to the following to i <<  61 //   class G4Class {
 61 //   singleton:                                <<  62 //      private:
 62 //   class G4Class                             <<  63 //         static G4ThreadLocal G4Class* instance;
 63 //   {                                         <<  64 //         G4Class() { ... }
 64 //     private:                                <<  65 //      public:
 65 //       static G4ThreadLocal G4Class* instanc <<  66 //         static G4Class* GetInstance() {
 66 //       G4Class() { ... }                     <<  67 //              if ( instance == 0 ) instance = new G4Class;
 67 //     public:                                 <<  68 //              return instance;
 68 //       static G4Class* GetInstance()         <<  69 //         }
 69 //       {                                     <<  70 //  };
 70 //         if ( instance == nullptr ) instance <<  71 //  Note that this class also has a memory leak.
 71 //           return instance;                  <<  72 //
 72 //       }                                     <<  73 //  This class can be used as follows:
 73 //   };                                        <<  74 //   class G4Class {
 74 //   Note that this class also has a memory le <<  75 //      friend class G4ThreadLocalSingleton<G4Class>;
 75 //                                             <<  76 //      private:
 76 //   This class can be used as follows:        <<  77 //         G4Class() { ... }
 77 //   class G4Class                             <<  78 //      public:
 78 //   {                                         <<  79 //         static G4Class* GetInstance() {
 79 //     friend class G4ThreadLocalSingleton<G4C <<  80 //              static G4ThreadLocalSingleton<G4Class> instance;
 80 //     private:                                <<  81 //              return instance.Instance();
 81 //       G4Class() { ... }                     <<  82 //         }
 82 //     public:                                 <<  83 //  };
 83 //       static G4Class* GetInstance()         <<  84 //  Each thread has its own instance of G4Class.
 84 //       {                                     <<  85 //  Deletion of G4Class instances is done at end of program.
 85 //         static G4ThreadLocalSingleton<G4Cla <<  86 //  Note the "friend" statement.
 86 //         return instance.Instance();         <<  87 //
 87 //       }                                     <<  88 // History:
 88 //   };                                        <<  89 //  28 October 2013: A. Dotti - First implementation
 89 //   Each thread has its own instance of G4Cla << 
 90 //   Deletion of G4Class instances is done at  << 
 91 //   Note the "friend" statement.              << 
 92                                                    90 
 93 // Author: A.Dotti, 28 October 2013            << 
 94 // ------------------------------------------- << 
 95 #ifndef G4TLSSINGLETON_HH                          91 #ifndef G4TLSSINGLETON_HH
 96 #define G4TLSSINGLETON_HH 1                    <<  92 #define G4TLSSINGLETON_HH
 97                                                    93 
 98 #include "G4AutoLock.hh"                       <<  94 //Debug this code
 99 #include "G4Cache.hh"                          <<  95 //#define g4tlssdebug 1
100 #include "G4Backtrace.hh"                      << 
101 #include "G4Threading.hh"                      << 
102                                                    96 
                                                   >>  97 #include "G4Cache.hh"
103 #include <list>                                    98 #include <list>
104 #include <vector>                              << 
105 #include <functional>                          << 
106                                                    99 
107 // Forward declaration. See G4AutoDelete.hh    << 100 //Forward declaration. See G4AutoDelete.hh
108 //                                             << 101 namespace G4AutoDelete {
109 namespace G4AutoDelete                         << 102   template<class T>
110 {                                              << 
111   template <class T>                           << 
112   void Register(T*);                              103   void Register(T*);
113 }                                                 104 }
114                                                   105 
115 template <class T>                             << 106 template<class T>
116 class G4ThreadLocalSingleton;                  << 107 class G4ThreadLocalSingleton : private G4Cache<T*> {
117                                                << 
118 // this explicit specialization holds all the  << 
119 // to explicitly invoke the auto-deletion      << 
120 template <>                                    << 
121 class G4ThreadLocalSingleton<void>             << 
122 {                                              << 
123  private:                                      << 
124   using fvector_t = std::vector<std::function< << 
125                                                << 
126   template <class T>                           << 
127   friend class G4ThreadLocalSingleton;         << 
128                                                << 
129   static fvector_t& GetCallbacks();            << 
130   static G4Mutex& GetMutex();                  << 
131                                                << 
132  public:                                       << 
133   static void Clear();                         << 
134                                                << 
135   template <typename FuncT>                    << 
136   static typename fvector_t::iterator Insert(F << 
137   {                                            << 
138     G4AutoLock _lk{ GetMutex() };              << 
139     return GetCallbacks().emplace(GetCallbacks << 
140                                   std::forward << 
141   }                                            << 
142 };                                             << 
143                                                << 
144 template <class T>                             << 
145 class G4ThreadLocalSingleton : private G4Cache << 
146 {                                              << 
147   friend void G4AutoDelete::Register<T>(T*);      108   friend void G4AutoDelete::Register<T>(T*);
148                                                << 109 public:
149  public:                                       << 
150   G4ThreadLocalSingleton();                       110   G4ThreadLocalSingleton();
151   // Creates thread-local singleton manager    << 111   //Creates thread-local singleton manager
152                                                   112 
153   ~G4ThreadLocalSingleton() override;          << 113   ~G4ThreadLocalSingleton(); 
154                                                   114 
155   G4ThreadLocalSingleton(const G4ThreadLocalSi << 115   T* Instance() const; 
156   G4ThreadLocalSingleton(G4ThreadLocalSingleto << 116   //Returns a pointer to a thread-private instance of T
157                                                   117 
158   G4ThreadLocalSingleton& operator=(const G4Th << 118 private:
159   G4ThreadLocalSingleton& operator=(G4ThreadLo << 119   G4ThreadLocalSingleton( G4ThreadLocalSingleton& rhs);// {}
160                                                << 120   void Register(T* i) const; 
161   T* Instance() const;                         << 
162   // Returns a pointer to a thread-private ins << 
163                                                << 
164  private:                                      << 
165   void Register(T* i) const;                   << 
166                                                   121 
167   void Clear();                                   122   void Clear();
168                                                   123 
169   mutable std::list<T*> instances;                124   mutable std::list<T*> instances;
170   mutable G4Mutex listm;                          125   mutable G4Mutex listm;
171 };                                                126 };
172                                                   127 
                                                   >> 128 
173 //============================================    129 //=============================================================
174 // Inline methods implementation               << 130 // Implementation details follow
175 //============================================    131 //=============================================================
176                                                << 132 #include "G4AutoLock.hh"
177 template <class T>                             << 133 template<class T>
178 G4ThreadLocalSingleton<T>::G4ThreadLocalSingle << 134 G4ThreadLocalSingleton<T>::G4ThreadLocalSingleton() : G4Cache<T*>() {
179   : G4Cache<T*>()                              << 
180 {                                              << 
181   G4MUTEXINIT(listm);                             135   G4MUTEXINIT(listm);
182   G4Cache<T*>::Put(nullptr);                   << 136   G4Cache<T*>::Put(static_cast<T*>(0)); 
183   // Uncomment below to find the origin of whe << 
184   /*                                           << 
185   auto bt = G4Backtrace::GetDemangled<4, 1>(   << 
186     [](const char* cstr) { return std::string{ << 
187   std::cout << "Backtrace to G4ThreadLocalSing << 
188             << G4Demangle<T>().c_str() << ">:\ << 
189   for(auto& itr : bt)                          << 
190   {                                            << 
191     if(!itr.empty())                           << 
192       std::cout << "\t" << itr << "\n";        << 
193   }                                            << 
194   */                                           << 
195   G4ThreadLocalSingleton<void>::Insert([&]() { << 
196     // printf("Deleting G4ThreadLocalSingleton << 
197     //       G4Demangle<T>().c_str());         << 
198     this->Clear();                             << 
199   });                                          << 
200 }                                                 137 }
201                                                   138 
202 template <class T>                             << 139 template<class T>
203 G4ThreadLocalSingleton<T>::~G4ThreadLocalSingl << 140 G4ThreadLocalSingleton<T>::~G4ThreadLocalSingleton() {
204 {                                              << 
205   Clear();                                        141   Clear();
206   G4MUTEXDESTROY(listm);                          142   G4MUTEXDESTROY(listm);
207 }                                                 143 }
208                                                   144 
209 template <class T>                             << 145 template<class T>
210 T* G4ThreadLocalSingleton<T>::Instance() const << 146 T* G4ThreadLocalSingleton<T>::Instance() const {
211 {                                              << 147     T* instance = G4Cache<T*>::Get();
212   T* instance = G4Cache<T*>::Get();            << 148     if ( instance == static_cast<T*>(0) ) {
213   if(instance == static_cast<T*>(0))           << 149       instance = new T;
214   {                                            << 150       G4Cache<T*>::Put( instance );
215     instance = new T;                          << 151       Register(instance);
216     G4Cache<T*>::Put(instance);                << 152     }
217     Register(instance);                        << 153     return instance;
218   }                                               154   }
219   return instance;                             << 
220 }                                              << 
221                                                   155 
222 template <class T>                             << 156 template<class T>
223 void G4ThreadLocalSingleton<T>::Register(T* i) << 157 G4ThreadLocalSingleton<T>::G4ThreadLocalSingleton( G4ThreadLocalSingleton&) {}
224 {                                              << 158 template<class T>
                                                   >> 159 void G4ThreadLocalSingleton<T>::Register(T* i) const {
225   G4AutoLock l(&listm);                           160   G4AutoLock l(&listm);
226   instances.push_back(i);                         161   instances.push_back(i);
227 }                                                 162 }
228                                                   163 
229 template <class T>                             << 164 
230 void G4ThreadLocalSingleton<T>::Clear()        << 165 template<class T>
231 {                                              << 166 void G4ThreadLocalSingleton<T>::Clear() {
232   if(instances.empty())                        << 167     G4AutoLock l(&listm);
233     return;                                    << 168     while ( ! instances.empty() ) 
234   G4AutoLock l(&listm);                        << 169     {
235   while(!instances.empty())                    << 170       T* thisinst = instances.front();
236   {                                            << 171       instances.pop_front();
237     T* thisinst = instances.front();           << 172       if ( thisinst != 0 ) delete thisinst;
238     instances.pop_front();                     << 173     }
239     delete thisinst;                           << 
240   }                                               174   }
241 }                                              << 
242                                                   175 
243 #endif                                         << 176 #endif //G4TLSSINGLETON_HH
244                                                   177