Geant4 Cross Reference |
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 49 template <class T> 50 class G4TWorkspacePool 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 71 inline void CleanUpAndDestroyAllWorkspaces(); 72 // To be called once at the end of the job 73 74 G4TWorkspacePool() {} 75 ~G4TWorkspacePool() {} 76 77 private: 78 static G4ThreadLocal T* fMyWorkspace; 79 // The thread's workspace - if assigned 80 }; 81 82 template <typename T> 83 G4ThreadLocal T* G4TWorkspacePool<T>::fMyWorkspace = nullptr; 84 85 // ----------------------------- 86 // Inline methods implementation 87 // ----------------------------- 88 89 template <class T> 90 T* G4TWorkspacePool<T>::CreateWorkspace() 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", 109 FatalException, 110 "Cannot create workspace twice for the same thread."); 111 wrk = fMyWorkspace; 112 } 113 return wrk; 114 } 115 116 template <class T> 117 void G4TWorkspacePool<T>::CreateAndUseWorkspace() 118 { 119 (this->CreateWorkspace())->UseWorkspace(); 120 } 121 122 template <class T> 123 T* G4TWorkspacePool<T>::FindOrCreateWorkspace() 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 136 template <class T> 137 void G4TWorkspacePool<T>::Recycle(T* myWrkSpace) 138 { 139 myWrkSpace->ReleaseWorkspace(); 140 delete myWrkSpace; 141 } 142 143 template <class T> 144 void G4TWorkspacePool<T>::CleanUpAndDestroyAllWorkspaces() 145 { 146 if(fMyWorkspace != nullptr) 147 { 148 fMyWorkspace->DestroyWorkspace(); 149 delete fMyWorkspace; 150 fMyWorkspace = nullptr; 151 } 152 } 153 154 #endif 155