Geant4  10.03.p03
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups 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 
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 #include "globals.hh"
46 
47 template<class T>
49 {
50 public:
51  // For use with simple MT mode - each thread gets a workspace and uses it until end
52  T* CreateWorkspace();
53 
54  void CreateAndUseWorkspace(); // Create it (as above) and use it
55 
56  // For use with 'dynamic' model of threading - workspaces can be recycled
58  // Reuse an existing workspace - or create a new one if needed.
59  // This will never fail, except if system is out of resources
60 
61  inline T* GetWorkspace() { return fMyWorkspace; }
62  // Give back the existing, active workspace for my thread / task
63 
64  //The recycling logic of workspace needs to be implemented
65  //void Recycle( T * );
66  // Keep the unused Workspace - for recycling : To be implemented
67 
68  //void CleanUpAndDestroyAllWorkspaces();
69  // To be called once at the end of the job
70 
71  //void ReleaseAndDestroyWorkspace(T*);
72  // Destroy workspace - after releasing it
73 
74 public:
77 
78 private:
79  static G4ThreadLocal T* fMyWorkspace;
80  // The thread's workspace - if assigned.
81 
82 };
83 
84 template<typename T> G4ThreadLocal T* G4TWorkspacePool<T>::fMyWorkspace=0;
85 
86 template<class T>
88 {
89  T* wrk = 0;
90  if ( !fMyWorkspace ) {
91  wrk = new T;
92  if ( !wrk ) {
93  G4Exception("G4TWorspacePool<someType>::CreateWorkspace", "Workspace01",
94  FatalException, "Failed to create workspace.");
95  } else {
96  fMyWorkspace = wrk;
97  }
98  } else {
99  G4Exception("ParticlesWorspacePool::CreateWorkspace", "Workspace02",
101  "Cannot create workspace twice for the same thread.");
102  wrk = fMyWorkspace;
103  }
104  return wrk;
105 }
106 
107 template<class T>
109 {
110  (this->CreateWorkspace())->UseWorkspace();
111 }
112 
113 template<class T>
115 {
116  T* wrk= fMyWorkspace;
117  if( !wrk ){
118  wrk= this->CreateWorkspace();
119  }
120  wrk->UseWorkspace();
121 
122  fMyWorkspace= wrk; // assign it for use by this thread.
123  return wrk;
124 }
125 
126 #endif //G4GEOMETRYWORKSPACEPOOL_HH
#define G4ThreadLocal
Definition: tls.hh:89
void G4Exception(const char *originOfException, const char *exceptionCode, G4ExceptionSeverity severity, const char *comments)
Definition: G4Exception.cc:41