Geant4 10.7.0
Toolkit for the simulation of the passage of particles through matter
Loading...
Searching...
No Matches
G4PDefManager.cc
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// G4PDefManager class implementation
27//
28// Author: Xin Dong, 25.01.2009 - First implementation
29// from automatic MT conversion.
30// --------------------------------------------------------------------
31
32#include <stdlib.h>
33
34#include "G4PDefManager.hh"
35#include "globals.hh"
36#include "pwdefs.hh"
37#include "G4AutoLock.hh"
38
40{
41 theProcessManager = nullptr;
42}
43
45{
46 G4ThreadLocalStatic G4int _instance = 0;
47 return _instance;
48}
49
51{
52 G4ThreadLocalStatic G4PDefData* _instance = nullptr;
53 return _instance;
54}
55
57{
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 {
70 l.unlock();
72 l.lock();
73 }
74 return (totalobj - 1);
75}
76
78 // Invoked by each worker thread to grow the subinstance array and
79 // initialize each new subinstance using a particular method defined
80 // by the subclass.
81{
82 G4AutoLock l(&mutex);
83 if (slavetotalspace() >= totalobj) { return; }
84 G4int originaltotalspace = slavetotalspace();
85 slavetotalspace() = totalobj + 512;
86 offset() = (G4PDefData *) realloc(offset(),
87 slavetotalspace() * sizeof(G4PDefData));
88 if (offset() == nullptr)
89 {
90 G4Exception("G4PDefManager::NewSubInstances()",
91 "OutOfMemory", FatalException, "Cannot malloc space!");
92 }
93
94 for (G4int i = originaltotalspace; i < slavetotalspace(); ++i)
95 {
96 offset()[i].initialize();
97 }
98}
99
101 // Invoked by all threads to free the subinstance array.
102{
103 if (offset() == nullptr) { return; }
104 free(offset());
105 offset() = nullptr;
106}
107
109{
110 return offset();
111}
112
114 // Use recycled work area, which was created previously.
115{
116 if( (offset() != nullptr) && (offset() != newOffset) )
117 {
118 G4Exception("G4PDefManager::UseWorkspace()",
119 "InvalidCondition", FatalException,
120 "Thread already has workspace - cannot use another.");
121 }
122 offset() = newOffset;
123}
124
126 // Detach this thread from this Location
127 // The object which calls this method is responsible for it.
128{
129 G4PDefData* offsetRet = offset();
130 offset() = nullptr;
131 return offsetRet;
132}
@ FatalException
void G4Exception(const char *originOfException, const char *exceptionCode, G4ExceptionSeverity severity, const char *description)
Definition: G4Exception.cc:35
#define G4MUTEXINIT(mutex)
Definition: G4Threading.hh:87
int G4int
Definition: G4Types.hh:85
void initialize()
G4ProcessManager * theProcessManager
G4int CreateSubInstance()
G4PDefData * FreeWorkArea()
static G4PART_DLL G4int & slavetotalspace()
static G4PART_DLL G4PDefData *& offset()
void UseWorkArea(G4PDefData *newOffset)
G4PDefData * GetOffset()
void NewSubInstances()
#define G4ThreadLocalStatic
Definition: tls.hh:76