ECCE @ EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
G4TWorkspacePool.hh
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file G4TWorkspacePool.hh
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 // GEANT 4 class header file
30 //
31 // Class Description:
32 //
33 // Create and hold a pointer to Workspace.
34 // This class holds a thread-private static instance
35 // of the template parameter workspace.
36 //
37 // The concrete implementation of workspace objects
38 // are responsible for instantiating a singleton instance
39 // of this pool.
40 //
41 // Recycling of this pool can enable reuse among different
42 // threads in task-based - or 'on-demand' - simulation.
43 
44 // ------------------------------------------------------------
45 
46 #ifndef G4TWORKSPACEPOOL_HH
47 #define G4TWORKSPACEPOOL_HH
48 
49 #include "tls.hh"
50 #include "globals.hh"
51 
52 template<class T>
54 {
55  public:
56 
57  inline T* CreateWorkspace();
58  // For use with simple MT mode - each thread gets a workspace
59  // and uses it until end
60 
61  inline void CreateAndUseWorkspace();
62  // Create it (as above) and use it
63 
64  inline T* FindOrCreateWorkspace();
65  // For use with 'dynamic' model of threading - workspaces can be recycled
66  // Reuse an existing workspace - or create a new one if needed.
67  // This will never fail, except if system is out of resources
68 
69  inline T* GetWorkspace() { return fMyWorkspace; }
70  // Give back the existing, active workspace for my thread / task
71 
72  inline void Recycle( T * myWrkSpace );
73  // Keep the unused Workspace - for recycling
74 
75  inline void CleanUpAndDestroyAllWorkspaces();
76  // To be called once at the end of the job
77 
78  public:
79 
82 
83  private:
84 
86  // The thread's workspace - if assigned
87 };
88 
89 template<typename T> G4ThreadLocal T* G4TWorkspacePool<T>::fMyWorkspace=0;
90 
91 template<class T>
93 {
94  T* wrk = 0;
95  if ( !fMyWorkspace )
96  {
97  wrk = new T;
98  if ( !wrk )
99  {
100  G4Exception("G4TWorspacePool<someType>::CreateWorkspace",
101  "MemoryError", FatalException,
102  "Failed to create workspace.");
103  }
104  else
105  {
106  fMyWorkspace = wrk;
107  }
108  }
109  else
110  {
111  G4Exception("ParticlesWorspacePool::CreateWorkspace",
112  "InvalidCondition", FatalException,
113  "Cannot create workspace twice for the same thread.");
114  wrk = fMyWorkspace;
115  }
116  return wrk;
117 }
118 
119 template<class T>
121 {
122  (this->CreateWorkspace())->UseWorkspace();
123 }
124 
125 template<class T>
127 {
128  T* wrk= fMyWorkspace;
129  if( !wrk )
130  {
131  wrk= this->CreateWorkspace();
132  }
133  wrk->UseWorkspace();
134 
135  fMyWorkspace= wrk; // assign it for use by this thread.
136  return wrk;
137 }
138 
139 template<class T>
140 void G4TWorkspacePool<T>::Recycle( T * myWrkSpace )
141 {
142  myWrkSpace->ReleaseWorkspace();
143  delete myWrkSpace;
144 }
145 
146 template<class T>
148 {
149  if (fMyWorkspace)
150  {
151  fMyWorkspace->DestroyWorkspace();
152  delete fMyWorkspace;
153  fMyWorkspace=0;
154  }
155 }
156 
157 #endif // G4TWORKSPACEPOOL_HH