Geant4  v4-10.4-release
 모두 클래스 네임스페이스들 파일들 함수 변수 타입정의 열거형 타입 열거형 멤버 Friends 매크로 그룹들 페이지들
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 // $Id: G4PDefManager.cc 110257 2018-05-17 14:20:12Z gcosmo $
28 //
29 //
30 // ------------------------------------------------------------
31 //
32 // GEANT4 class header file
33 //
34 // ---------------- G4PDefManager ----------------
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 
44 #include <stdlib.h>
45 
46 #include "G4PDefManager.hh"
47 #include "globals.hh"
48 #include "pwdefs.hh"
49 #include "G4AutoLock.hh"
50 
52 {
54 }
55 
57 {
58  G4ThreadLocalStatic G4int _instance = 0;
59  return _instance;
60 }
61 
63 {
64  G4ThreadLocalStatic G4PDefData* _instance = nullptr;
65  return _instance;
66 }
67 
69 {
71 }
72 
74  // Invoked by the master or work thread to create a new subinstance
75  // whenever a new split class instance is created. For each worker
76  // thread, ions are created dynamically.
77 {
78  G4AutoLock l(&mutex);
79  totalobj++;
80  if (totalobj > slavetotalspace())
81  {
82  l.unlock();
84  l.lock();
85  }
86  return (totalobj - 1);
87 }
88 
90  // Invoked by each worker thread to grow the subinstance array and
91  // initialize each new subinstance using a particular method defined
92  // by the subclass.
93 {
94  G4AutoLock l(&mutex);
95  if (slavetotalspace() >= totalobj) { return; }
96  G4int originaltotalspace = slavetotalspace();
97  slavetotalspace() = totalobj + 512;
98  offset() = (G4PDefData *) realloc(offset(), slavetotalspace() * sizeof(G4PDefData));
99 
100  if (offset() == 0)
101  {
102  G4Exception("G4PDefManager::NewSubInstances()",
103  "OutOfMemory", FatalException, "Cannot malloc space!");
104  }
105 
106  for (G4int i = originaltotalspace; i < slavetotalspace(); i++)
107  {
108  offset()[i].initialize();
109  }
110 }
111 
113  // Invoked by all threads to free the subinstance array.
114 {
115  if (!offset()) { return; }
116  free(offset());
117  offset() = 0;
118 }
119 
121 {
122  return offset();
123 }
124 
126  // Use recycled work area, which was created previously.
127 {
128  if( offset() && offset()!=newOffset )
129  {
130  G4Exception("G4PDefManager::UseWorkspace()",
131  "InvalidCondition", FatalException,
132  "Thread already has workspace - cannot use another.");
133  }
134  offset()= newOffset;
135 }
136 
138  // Detach this thread from this Location
139  // The object which calls this method is responsible for it.
140 {
141  G4PDefData* offsetRet= offset();
142  offset()= 0;
143  return offsetRet;
144 }
static G4PART_DLL G4int & slavetotalspace()
void NewSubInstances()
G4ProcessManager * theProcessManager
#define G4ThreadLocalStatic
Definition: tls.hh:68
static G4PART_DLL G4PDefData *& offset()
void UseWorkArea(G4PDefData *newOffset)
#define G4MUTEXINIT(mutex)
Definition: G4Threading.hh:89
G4PDefData * FreeWorkArea()
void G4Exception(const char *originOfException, const char *exceptionCode, G4ExceptionSeverity severity, const char *description)
Definition: G4Exception.hh:65
G4PDefData * GetOffset()
int G4int
Definition: G4Types.hh:78
void initialize()
G4int CreateSubInstance()