Geant4  10.02.p03
G4PDefSplitter.hh
Go to the documentation of this file.
1 //
2 // ********************************************************************
3 // * License and Disclaimer *
4 // * *
5 // * The Geant4 software is copyright of the Copyright Holders of *
6 // * the Geant4 Collaboration. It is provided under the terms and *
7 // * conditions of the Geant4 Software License, included in the file *
8 // * LICENSE and available at http://cern.ch/geant4/license . These *
9 // * include a list of copyright holders. *
10 // * *
11 // * Neither the authors of this software system, nor their employing *
12 // * institutes,nor the agencies providing financial support for this *
13 // * work make any representation or warranty, express or implied, *
14 // * regarding this software system or assume any liability for its *
15 // * use. Please see the license in the file LICENSE and URL above *
16 // * for the full disclaimer and the limitation of liability. *
17 // * *
18 // * This code implementation is the result of the scientific and *
19 // * technical work of the GEANT4 collaboration. *
20 // * By using, copying, modifying or distributing the software (or *
21 // * any work based on the software) you agree to acknowledge its *
22 // * use in resulting scientific publications, and indicate your *
23 // * acceptance of all terms of the Geant4 Software license. *
24 // ********************************************************************
25 //
26 //
27 // $Id: $
28 //
29 //
30 // ------------------------------------------------------------
31 //
32 // GEANT 4 class header file
33 //
34 // ---------------- G4PDefSplitter ----------------
35 //
36 // Utility template class for splitting RW data for thread-safety from
37 // classes: G4ParticleDefinition, G4VDecayChannel.
38 //
39 // ------------------------------------------------------------
40 // History:
41 // 01.25.2009 Xin Dong: First implementation from automatic MT conversion.
42 // ------------------------------------------------------------
43 #ifndef G4PDEFSPLITTER_HH
44 #define G4PDEFSPLITTER_HH
45 
46 #include <stdlib.h>
47 
48 #include "globals.hh"
49 #include "pwdefs.hh"
50 #include "G4AutoLock.hh"
51 
52 template <class T> // T is the private data from the object to be split
54 {
55  public:
56 
58  {
60  }
61 
63  // Invoked by the master or work thread to create a new subinstance
64  // whenever a new split class instance is created. For each worker
65  // thread, ions are created dynamically.
66  {
67  G4AutoLock l(&mutex);
68  totalobj++;
70  {
71  l.unlock();
73  l.lock();
74  }
75  return (totalobj - 1);
76  }
77 
79  // Invoked by each worker thread to grow the subinstance array and
80  // initialize each new subinstance using a particular method defined
81  // by the subclass.
82  {
83  G4AutoLock l(&mutex);
84  if (slavetotalspace >= totalobj) { return; }
85  G4int originaltotalspace = slavetotalspace;
86  slavetotalspace = totalobj + 512;
87  offset = (T *) realloc(offset, slavetotalspace * sizeof(T));
88 
89  if (offset == 0)
90  {
91  G4Exception("G4PDefSplitter::NewSubInstances()",
92  "OutOfMemory", FatalException, "Cannot malloc space!");
93  }
94 
95  for (G4int i = originaltotalspace; i < slavetotalspace; i++)
96  {
97  offset[i].initialize();
98  }
99  }
100 
101  void FreeSlave()
102  // Invoked by all threads to free the subinstance array.
103  {
104  if (!offset) { return; }
105  free(offset);
106  offset = 0;
107  }
108 
109 
110  T* GetOffset() { return offset; }
111 
112  void UseWorkArea( T* newOffset ) // , G4int numObjects, G4int numSpace)
113  {
114  // Use recycled work area - which was created previously
115  if( offset && offset!=newOffset )
116  {
117  G4Exception("G4PDefSplitter::UseWorkspace()",
118  "TwoWorkspaces", FatalException,
119  "Thread already has workspace - cannot use another.");
120  }
121  offset= newOffset;
122  // totalobj= numObjects;
123  // totalspace= numSpace;
124  }
125 
126  T* FreeWorkArea() // G4int* numObjects, G4int* numSpace)
127  {
128  // Detach this thread from this Location
129  // The object which calls this method is responsible for it.
130  //
131  T* offsetRet= offset;
132 
133  offset= 0;
134 
135  return offsetRet;
136  }
137 
138  public:
139 
142 
143  private:
144 
147 };
148 #if defined G4PARTICLES_ALLOC_EXPORT
150  template<typename T> G4ThreadLocal T* G4PDefSplitter<T>::offset=0;
151 #endif
152 #endif
#define G4MUTEXINIT(mutex)
Definition: G4Threading.hh:177
G4PART_DLL G4ThreadLocalStatic G4int slavetotalspace
void UseWorkArea(T *newOffset)
#define G4ThreadLocal
Definition: tls.hh:89
#define G4ThreadLocalStatic
Definition: tls.hh:88
int G4int
Definition: G4Types.hh:78
void G4Exception(const char *originOfException, const char *exceptionCode, G4ExceptionSeverity severity, const char *comments)
Definition: G4Exception.cc:41
G4int G4Mutex
Definition: G4Threading.hh:173
void NewSubInstances()
G4PART_DLL G4ThreadLocalStatic T * offset
#define G4PART_DLL
Definition: pwdefs.hh:48
G4int CreateSubInstance()