Geant4  10.02
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 
59  }
60 
62  // Invoked by the master or work thread to create a new subinstance
63  // whenever a new split class instance is created. For each worker
64  // thread, ions are created dynamically.
65  {
66  G4AutoLock l(&mutex);
67  totalobj++;
68  if (totalobj > slavetotalspace) {
69  l.unlock();
71  l.lock();
72  }
73  return (totalobj - 1);
74  }
75 
77  // Invoked by each worker thread to grow the subinstance array and
78  // initialize each new subinstance using a particular method defined
79  // by the subclass.
80  {
81  G4AutoLock l(&mutex);
82  if (slavetotalspace >= totalobj) { return; }
83  G4int originaltotalspace = slavetotalspace;
84  slavetotalspace = totalobj + 512;
85  offset = (T *) realloc(offset, slavetotalspace * sizeof(T));
86 
87  if (offset == 0)
88  {
89  G4Exception("G4PDefSplitter::NewSubInstances()",
90  "OutOfMemory", FatalException, "Cannot malloc space!");
91  }
92 
93  for (G4int i = originaltotalspace; i < slavetotalspace; i++)
94  {
95  offset[i].initialize();
96  }
97  }
98 
99  void FreeSlave()
100  // Invoked by all threads to free the subinstance array.
101  {
102  if (!offset) { return; }
103  free(offset);
104  offset = 0;
105  }
106 
107 
108  T* GetOffset() { return offset; }
109 
110  void UseWorkArea( T* newOffset ) // , G4int numObjects, G4int numSpace)
111  {
112  // Use recycled work area - which was created previously
113  if( offset && offset!=newOffset )
114  {
115  if( newOffset != offset )
116  {
117  G4Exception("G4PDefSplitter::UseWorkspace()",
118  "TwoWorkspaces", FatalException,
119  "Thread already has workspace - cannot use another.");
120  }
121  else
122  {
123  G4Exception("G4PDefSplitter::UseWorkspace()",
124  "TwoWorkspaces", JustWarning,
125  "Thread already has a workspace - trying to set the same again.");
126  }
127  }
128  offset= newOffset;
129  // totalobj= numObjects;
130  // totalspace= numSpace;
131  }
132 
133  T* FreeWorkArea() // G4int* numObjects, G4int* numSpace)
134  {
135  // Detach this thread from this Location
136  // The object which calls this method is responsible for it.
137  //
138  T* offsetRet= offset;
139 
140  offset= 0;
141 
142  return offsetRet;
143  }
144 
145  public:
146 
149 
150  private:
151 
154 };
155 
156 #endif
#define G4MUTEXINIT(mutex)
Definition: G4Threading.hh:177
void UseWorkArea(T *newOffset)
#define G4ThreadLocalStatic
Definition: tls.hh:88
int G4int
Definition: G4Types.hh:78
G4PART_DLL G4ThreadLocalStatic T * offset
G4PART_DLL G4ThreadLocalStatic G4int slavetotalspace
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()
#define G4PART_DLL
Definition: pwdefs.hh:48
G4int CreateSubInstance()