Geant4  v4-10.4-release
 모두 클래스 네임스페이스들 파일들 함수 변수 타입정의 열거형 타입 열거형 멤버 Friends 매크로 그룹들 페이지들
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 // $Id:$
28 //
29 // ------------------------------------------------------------
30 // GEANT 4 class header file
31 //
32 // Class Description:
33 //
34 // Create and hold a pointer to Workspace.
35 // This class holds a thread-private static instance
36 // of the template parameter workspace.
37 //
38 // The concrete implementation of workspace objects
39 // are responsible for instantiating a singleton instance
40 // of this pool.
41 //
42 // Recycling of this pool can enable reuse among different
43 // threads in task-based - or 'on-demand' - simulation.
44 
45 // ------------------------------------------------------------
46 
47 #ifndef G4TWORKSPACEPOOL_HH
48 #define G4TWORKSPACEPOOL_HH
49 
50 #include "tls.hh"
51 #include "globals.hh"
52 
53 template<class T>
55 {
56  public:
57 
58  inline T* CreateWorkspace();
59  // For use with simple MT mode - each thread gets a workspace
60  // and uses it until end
61 
62  inline void CreateAndUseWorkspace();
63  // Create it (as above) and use it
64 
65  inline T* FindOrCreateWorkspace();
66  // For use with 'dynamic' model of threading - workspaces can be recycled
67  // Reuse an existing workspace - or create a new one if needed.
68  // This will never fail, except if system is out of resources
69 
70  inline T* GetWorkspace() { return fMyWorkspace; }
71  // Give back the existing, active workspace for my thread / task
72 
73  inline void Recycle( T * myWrkSpace );
74  // Keep the unused Workspace - for recycling
75 
76  inline void CleanUpAndDestroyAllWorkspaces();
77  // To be called once at the end of the job
78 
79  public:
80 
83 
84  private:
85 
87  // The thread's workspace - if assigned
88 };
89 
90 template<typename T> G4ThreadLocal T* G4TWorkspacePool<T>::fMyWorkspace=0;
91 
92 template<class T>
94 {
95  T* wrk = 0;
96  if ( !fMyWorkspace )
97  {
98  wrk = new T;
99  if ( !wrk )
100  {
101  G4Exception("G4TWorspacePool<someType>::CreateWorkspace",
102  "MemoryError", FatalException,
103  "Failed to create workspace.");
104  }
105  else
106  {
107  fMyWorkspace = wrk;
108  }
109  }
110  else
111  {
112  G4Exception("ParticlesWorspacePool::CreateWorkspace",
113  "InvalidCondition", FatalException,
114  "Cannot create workspace twice for the same thread.");
115  wrk = fMyWorkspace;
116  }
117  return wrk;
118 }
119 
120 template<class T>
122 {
123  (this->CreateWorkspace())->UseWorkspace();
124 }
125 
126 template<class T>
128 {
129  T* wrk= fMyWorkspace;
130  if( !wrk )
131  {
132  wrk= this->CreateWorkspace();
133  }
134  wrk->UseWorkspace();
135 
136  fMyWorkspace= wrk; // assign it for use by this thread.
137  return wrk;
138 }
139 
140 template<class T>
141 void G4TWorkspacePool<T>::Recycle( T * myWrkSpace )
142 {
143  myWrkSpace->ReleaseWorkspace();
144  delete myWrkSpace;
145 }
146 
147 template<class T>
149 {
150  if (fMyWorkspace)
151  {
152  fMyWorkspace->DestroyWorkspace();
153  delete fMyWorkspace;
154  fMyWorkspace=0;
155  }
156 }
157 
158 #endif // G4TWORKSPACEPOOL_HH
static G4ThreadLocal T * fMyWorkspace
#define G4ThreadLocal
Definition: tls.hh:69
void Recycle(T *myWrkSpace)
void CleanUpAndDestroyAllWorkspaces()
void G4Exception(const char *originOfException, const char *exceptionCode, G4ExceptionSeverity severity, const char *description)
Definition: G4Exception.hh:65