Geant4  v4-10.4-release
 모두 클래스 네임스페이스들 파일들 함수 변수 타입정의 열거형 타입 열거형 멤버 Friends 매크로 그룹들 페이지들
G4GeomSplitter.hh
이 파일의 문서화 페이지로 가기
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 // class G4LogicalVolume
31 //
32 // Class description:
33 //
34 // Utility template class for splitting of RW data for thread-safety from
35 // classes: G4LogicalVolume, G4Region, G4VPhysicalVolume, G4PolyconeSide
36 // G4PolyhedraSide, G4PVReplica.
37 
38 // Author:
39 // 01.25.09 X.Dong: Initial version from automatic MT conversion.
40 // ------------------------------------------------------------------------
41 #ifndef G4GEOMSPLITTER_HH
42 #define G4GEOMSPLITTER_HH
43 
44 #include "globals.hh"
45 #include "geomwdefs.hh"
46 #include "G4AutoLock.hh"
47 #include "G4Allocator.hh"
48 
49 template <class T> // T is the private data from the object to be split
51 {
52  public:
53 
55  : totalobj(0), totalspace(0), sharedOffset(0)
56  {
58  }
59 
60  void *operator new(size_t)
61  // Override "new" for "G4Allocator"
62  {
64  return (void *) anOffsetAllocator->MallocSingle();
65  }
66 
67  void operator delete(void *anOffset)
68  // Override "delete" for "G4Allocator".
69  {
70  anOffsetAllocator->FreeSingle((T*) anOffset);
71  }
72 
73  T* Reallocate(G4int size)
74  {
75  T* noffset = new T[size];
76  if (offset)
77  {
78  for (G4int i=0; i<totalspace; ++i)
79  {
80  noffset[i] = offset[i];
81  }
82  delete [] offset;
83  }
84  totalspace = size;
85 
86  return noffset;
87  }
88 
90  // Invoked by the master or work thread to create a new subinstance
91  // whenever a new split class instance is created.
92  {
93  G4AutoLock l(&mutex);
94  totalobj++;
95  if (totalobj > totalspace)
96  {
98  if (offset == 0)
99  {
100  G4Exception("G4GeomSPlitter::CreateSubInstance()",
101  "OutOfMemory", FatalException, "Cannot malloc space!");
102  }
104  }
105  return (totalobj - 1);
106  }
107 
109  {
110  G4AutoLock l(&mutex);
111  for (G4int i=0; i<totalspace; ++i)
112  {
113  offset[i] = sharedOffset[i];
114  }
115  }
116 
118  // Invoked by each worker thread to copy all the subinstance array
119  // from the master thread.
120  {
121  G4AutoLock l(&mutex);
122  if (offset) { return; }
124  if (offset == 0)
125  {
126  G4Exception("G4GeomSplitter::SlaveCopySubInstanceArray()",
127  "OutOfMemory", FatalException, "Cannot malloc space!");
128  }
129  l.unlock();
131  }
132 
134  // Invoked by each worker thread to create the subinstance array and
135  // initialize each subinstance using a particular method defined by
136  // the subclass.
137  {
138  G4AutoLock l(&mutex);
139  if (offset) { return; }
141 
142  if (offset == 0)
143  {
144  G4Exception("G4GeomSplitter::SlaveInitializeSubInstance()",
145  "OutOfMemory", FatalException, "Cannot malloc space!");
146  }
147 
148  for (G4int i=0 ; i<totalspace; ++i)
149  {
150  offset[i].initialize();
151  }
152  }
153 
155  // Invoked by each worker thread at start of a run (2nd or later)
156  // to copy again all the subinstance array from the master thread.
157  // To cope with user's changes in Geometry - e.g. change of material
158  // in a volume
159  {
160  if (!offset)
161  {
163  G4Exception("G4GeomSPlitter::SlaveReCopySubInstance()",
164  "MissingInitialisation", JustWarning,
165  "Must be called after Initialisation or first Copy.");
166  }
168  }
169 
170  void FreeSlave()
171  // Invoked by all threads to free the subinstance array.
172  {
173  if (!offset) { return; }
174  delete [] offset;
175  offset = 0;
176  }
177 
178  // Extension - to allow sharing of workspaces
179 
180  T* GetOffset() { return offset; }
181 
182  void UseWorkArea( T* newOffset )
183  // Use recycled work area - which was created previously
184  {
185  if( offset && offset!=newOffset )
186  {
187  G4Exception("G4GeomSplitter::UseWorkspace()",
188  "TwoWorkspaces", FatalException,
189  "Thread already has workspace - cannot use another.");
190  }
191  offset= newOffset;
192  }
193 
195  // Detach this thread from this Location.
196  // The object which calls this method is responsible for it.
197  {
198  T* offsetRet = offset;
199  offset = 0;
200  return offsetRet;
201  }
202 
203  public:
204 
207 
208  private:
209 
214 };
215 
216 template <typename T> G4ThreadLocal
218 template <typename T> G4ThreadLocal
220 
221 #endif
void UseWorkArea(T *newOffset)
G4int CreateSubInstance()
static G4GEOM_DLL G4ThreadLocal G4Allocator< T > * anOffsetAllocator
Type * MallocSingle()
Definition: G4Allocator.hh:202
void FreeSingle(Type *anElement)
Definition: G4Allocator.hh:212
#define G4ThreadLocal
Definition: tls.hh:69
#define G4MUTEXINIT(mutex)
Definition: G4Threading.hh:89
void SlaveInitializeSubInstance()
static G4GEOM_DLL G4ThreadLocal T * offset
T * Reallocate(G4int size)
void G4Exception(const char *originOfException, const char *exceptionCode, G4ExceptionSeverity severity, const char *description)
Definition: G4Exception.hh:65
int G4int
Definition: G4Types.hh:78
void SlaveCopySubInstanceArray()
void CopyMasterContents()
void SlaveReCopySubInstanceArray()
#define G4GEOM_DLL
Definition: geomwdefs.hh:48
std::mutex G4Mutex
Definition: G4Threading.hh:84