ECCE @ EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
G4PDefManager.cc
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file G4PDefManager.cc
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 //
28 //
29 // ------------------------------------------------------------
30 //
31 // GEANT4 class header file
32 //
33 // ---------------- G4PDefManager ----------------
34 //
35 // Utility template class for splitting RW data for thread-safety from
36 // classes: G4ParticleDefinition, G4VDecayChannel.
37 //
38 // ------------------------------------------------------------
39 // History:
40 // 01.25.2009 Xin Dong: First implementation from automatic MT conversion.
41 // ------------------------------------------------------------
42 
43 #include <stdlib.h>
44 
45 #include "G4PDefManager.hh"
46 #include "globals.hh"
47 #include "pwdefs.hh"
48 #include "G4AutoLock.hh"
49 
51 {
53 }
54 
56 {
57  G4ThreadLocalStatic G4int _instance = 0;
58  return _instance;
59 }
60 
62 {
63  G4ThreadLocalStatic G4PDefData* _instance = nullptr;
64  return _instance;
65 }
66 
68 {
70 }
71 
73  // Invoked by the master or work thread to create a new subinstance
74  // whenever a new split class instance is created. For each worker
75  // thread, ions are created dynamically.
76 {
77  G4AutoLock l(&mutex);
78  totalobj++;
79  if (totalobj > slavetotalspace())
80  {
81  l.unlock();
83  l.lock();
84  }
85  return (totalobj - 1);
86 }
87 
89  // Invoked by each worker thread to grow the subinstance array and
90  // initialize each new subinstance using a particular method defined
91  // by the subclass.
92 {
93  G4AutoLock l(&mutex);
94  if (slavetotalspace() >= totalobj) { return; }
95  G4int originaltotalspace = slavetotalspace();
96  slavetotalspace() = totalobj + 512;
97  offset() = (G4PDefData *) realloc(offset(), slavetotalspace() * sizeof(G4PDefData));
98 
99  if (offset() == 0)
100  {
101  G4Exception("G4PDefManager::NewSubInstances()",
102  "OutOfMemory", FatalException, "Cannot malloc space!");
103  }
104 
105  for (G4int i = originaltotalspace; i < slavetotalspace(); i++)
106  {
107  offset()[i].initialize();
108  }
109 }
110 
112  // Invoked by all threads to free the subinstance array.
113 {
114  if (!offset()) { return; }
115  free(offset());
116  offset() = 0;
117 }
118 
120 {
121  return offset();
122 }
123 
125  // Use recycled work area, which was created previously.
126 {
127  if( offset() && offset()!=newOffset )
128  {
129  G4Exception("G4PDefManager::UseWorkspace()",
130  "InvalidCondition", FatalException,
131  "Thread already has workspace - cannot use another.");
132  }
133  offset()= newOffset;
134 }
135 
137  // Detach this thread from this Location
138  // The object which calls this method is responsible for it.
139 {
140  G4PDefData* offsetRet= offset();
141  offset()= 0;
142  return offsetRet;
143 }