Geant4  10.01.p02
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 
27 // Create and hold a pointer to Workspace.
28 //
29 // This class holds a thread-private static instance
30 // of the template parameter workspace.
31 //
32 // Note:
33 // The concrete implementation of workspace objects
34 // are responsible for instantiating a singleton instance
35 // of this pool.
36 //
37 // This class will be extended to recycle workspaces.
38 // This can enable their reuse between different threads,
39 // in task-based - or 'on-demand' - simulation.
40 
41 #ifndef G4TWORKSPACEPOOL_HH
42 #define G4TWORKSPACEPOOL_HH
43 
44 #include "tls.hh"
45 
46 template<class T>
48 {
49 public:
50  // For use with simple MT mode - each thread gets a workspace and uses it until end
51  T* CreateWorkspace();
52 
53  void CreateAndUseWorkspace(); // Create it (as above) and use it
54 
55  // For use with 'dynamic' model of threading - workspaces can be recycled
57  // Reuse an existing workspace - or create a new one if needed.
58  // This will never fail, except if system is out of resources
59 
60  inline T* GetWorkspace() { return fMyWorkspace; }
61  // Give back the existing, active workspace for my thread / task
62 
63  //The recycling logic of workspace needs to be implemented
64  //void Recycle( T * );
65  // Keep the unused Workspace - for recycling : To be implemented
66 
67  //void CleanUpAndDestroyAllWorkspaces();
68  // To be called once at the end of the job
69 
70  //void ReleaseAndDestroyWorkspace(T*);
71  // Destroy workspace - after releasing it
72 
73 public:
76 
77 private:
79  // The thread's workspace - if assigned.
80 
81 };
82 
83 #include "globals.hh"
84 
85 template<class T>
87 {
88  T* wrk = 0;
89  if ( !fMyWorkspace ) {
90  wrk = new T;
91  if ( !wrk ) {
92  G4Exception("G4TWorspacePool<someType>::CreateWorkspace", "Run0035",
93  FatalException, "Failed to create workspace.");
94  } else {
95  fMyWorkspace = wrk;
96  }
97  } else {
98  G4Exception("ParticlesWorspacePool::CreateWorkspace", "Run0035",
100  "Cannot create workspace twice for the same thread.");
101  wrk = fMyWorkspace;
102  }
103  return wrk;
104 }
105 
106 template<class T>
108 {
109  (this->CreateWorkspace())->UseWorkspace();
110 }
111 
112 template<class T>
114 {
115  T* wrk= fMyWorkspace;
116  if( !wrk ){
117  wrk= this->CreateWorkspace();
118  }
119  wrk->UseWorkspace();
120 
121  fMyWorkspace= wrk; // assign it for use by this thread.
122  return wrk;
123 }
124 
125 #endif //G4GEOMETRYWORKSPACEPOOL_HH
#define G4ThreadLocal
Definition: tls.hh:89
static G4ThreadLocal T * fMyWorkspace
void G4Exception(const char *originOfException, const char *exceptionCode, G4ExceptionSeverity severity, const char *comments)
Definition: G4Exception.cc:41