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