Geant4 11.1.1
Toolkit for the simulation of the passage of particles through matter
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
G4TWorkspacePool.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// G4TWorkspacePool
27//
28// Class description:
29//
30// Create and hold a pointer to Workspace.
31// This class holds a thread-private static instance of the template
32// parameter workspace.
33//
34// The concrete implementation of workspace objects are responsible for
35// instantiating a singleton instance of this pool.
36//
37// Recycling of this pool can enable reuse among different threads in
38// task-based - or 'on-demand' - simulation.
39
40// Authors: J.Apostolakis, A.Dotti - 24 October 2014
41// Revisions: G.Cosmo - 21 Obctober 2016, revised pool initialisation
42// ------------------------------------------------------------
43#ifndef G4TWORKSPACEPOOL_HH
44#define G4TWORKSPACEPOOL_HH 1
45
46#include "globals.hh"
47#include "tls.hh"
48
49template <class T>
51{
52 public:
53 inline T* CreateWorkspace();
54 // For use with simple MT mode - each thread gets a workspace
55 // and uses it until end
56
57 inline void CreateAndUseWorkspace();
58 // Create it (as above) and use it
59
60 inline T* FindOrCreateWorkspace();
61 // For use with 'dynamic' model of threading - workspaces can be recycled
62 // Reuse an existing workspace - or create a new one if needed.
63 // This will never fail, except if system is out of resources
64
65 inline T* GetWorkspace() { return fMyWorkspace; }
66 // Give back the existing, active workspace for my thread / task
67
68 inline void Recycle(T* myWrkSpace);
69 // Keep the unused Workspace - for recycling
70
72 // To be called once at the end of the job
73
76
77 private:
78 static G4ThreadLocal T* fMyWorkspace;
79 // The thread's workspace - if assigned
80};
81
82template <typename T>
84
85// -----------------------------
86// Inline methods implementation
87// -----------------------------
88
89template <class T>
91{
92 T* wrk = nullptr;
93 if(fMyWorkspace == nullptr)
94 {
95 wrk = new T;
96 if(wrk == nullptr)
97 {
98 G4Exception("G4TWorspacePool<someType>::CreateWorkspace()", "MemoryError",
99 FatalException, "Failed to create workspace.");
100 }
101 else
102 {
103 fMyWorkspace = wrk;
104 }
105 }
106 else
107 {
108 G4Exception("ParticlesWorspacePool::CreateWorkspace()", "InvalidCondition",
110 "Cannot create workspace twice for the same thread.");
111 wrk = fMyWorkspace;
112 }
113 return wrk;
114}
115
116template <class T>
118{
119 (this->CreateWorkspace())->UseWorkspace();
120}
121
122template <class T>
124{
125 T* wrk = fMyWorkspace;
126 if(wrk == nullptr)
127 {
128 wrk = this->CreateWorkspace();
129 }
130 wrk->UseWorkspace();
131
132 fMyWorkspace = wrk; // assign it for use by this thread.
133 return wrk;
134}
135
136template <class T>
138{
139 myWrkSpace->ReleaseWorkspace();
140 delete myWrkSpace;
141}
142
143template <class T>
145{
146 if(fMyWorkspace != nullptr)
147 {
148 fMyWorkspace->DestroyWorkspace();
149 delete fMyWorkspace;
150 fMyWorkspace = nullptr;
151 }
152}
153
154#endif
@ FatalException
void G4Exception(const char *originOfException, const char *exceptionCode, G4ExceptionSeverity severity, const char *description)
Definition: G4Exception.cc:59
void CleanUpAndDestroyAllWorkspaces()
void Recycle(T *myWrkSpace)
#define G4ThreadLocal
Definition: tls.hh:77