ITK  4.8.0
Insight Segmentation and Registration Toolkit
itkMultiThreader.h
Go to the documentation of this file.
1 /*=========================================================================
2  *
3  * Copyright Insight Software Consortium
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0.txt
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  *=========================================================================*/
18 /*=========================================================================
19  *
20  * Portions of this file are subject to the VTK Toolkit Version 3 copyright.
21  *
22  * Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
23  *
24  * For complete copyright, license and disclaimer of warranty information
25  * please refer to the NOTICE file at the top of the ITK source tree.
26  *
27  *=========================================================================*/
28 #ifndef itkMultiThreader_h
29 #define itkMultiThreader_h
30 
31 #include "itkMutexLock.h"
32 #include "itkThreadSupport.h"
33 #include "itkIntTypes.h"
34 
35 #include "itkThreadPool.h"
36 
37 namespace itk
38 {
55 class ITKCommon_EXPORT MultiThreader : public Object
56 {
57 public:
59  typedef MultiThreader Self;
60  typedef Object Superclass;
61  typedef SmartPointer<Self> Pointer;
62  typedef SmartPointer<const Self> ConstPointer;
63 
65  itkNewMacro(Self);
66 
68  itkTypeMacro(MultiThreader, Object);
69 
73  void SetNumberOfThreads(ThreadIdType numberOfThreads);
74 
75  itkGetConstMacro(NumberOfThreads, ThreadIdType);
76 
82  static void SetGlobalMaximumNumberOfThreads(ThreadIdType val);
83  static ThreadIdType GetGlobalMaximumNumberOfThreads();
85 
90  static void SetGlobalDefaultUseThreadPool( const bool GlobalDefaultUseThreadPool );
91  static bool GetGlobalDefaultUseThreadPool( );
93 
98  static void SetGlobalDefaultNumberOfThreads(ThreadIdType val);
99 
100  static ThreadIdType GetGlobalDefaultNumberOfThreads();
101 
106  void SingleMethodExecute();
107 
112  void MultipleMethodExecute();
113 
119  void SetSingleMethod(ThreadFunctionType, void *data);
120 
123  void SetMultipleMethod(ThreadIdType index, ThreadFunctionType, void *data);
124 
128  ThreadIdType SpawnThread(ThreadFunctionType, void *data);
129 
131  void TerminateThread(ThreadIdType thread_id);
132 
136  itkSetObjectMacro(ThreadPool, ThreadPool);
137 
139  itkGetModifiableObjectMacro(ThreadPool, ThreadPool);
140 
144  itkSetMacro(UseThreadPool,bool);
145 
147  itkGetMacro(UseThreadPool,bool);
148 
160 #ifdef ThreadInfoStruct
161 #undef ThreadInfoStruct
162 #endif
163  struct ThreadInfoStruct
164  {
165  ThreadIdType ThreadID;
166  ThreadIdType NumberOfThreads;
167  int *ActiveFlag;
168  MutexLock::Pointer ActiveFlagLock;
169  void *UserData;
170  ThreadFunctionType ThreadFunction;
171  enum { SUCCESS, ITK_EXCEPTION, ITK_PROCESS_ABORTED_EXCEPTION, STD_EXCEPTION, UNKNOWN } ThreadExitCode;
172  };
173 
174 protected:
175  MultiThreader();
176  ~MultiThreader();
177  virtual void PrintSelf(std::ostream & os, Indent indent) const ITK_OVERRIDE;
178 
179 private:
180  MultiThreader(const Self &); // purposely not implemented
181  void operator=(const Self &); // purposely not implemented
182 
183  // Thread pool instance and factory
184  ThreadPool::Pointer m_ThreadPool;
185 
186  // choose whether to use Spawn or ThreadPool methods
187  bool m_UseThreadPool;
188 
192  ThreadInfoStruct m_ThreadInfoArray[ITK_MAX_THREADS];
193 
195  ThreadFunctionType m_SingleMethod;
196  ThreadFunctionType m_MultipleMethod[ITK_MAX_THREADS];
197 
200  int m_SpawnedThreadActiveFlag[ITK_MAX_THREADS];
201  MutexLock::Pointer m_SpawnedThreadActiveFlagLock[ITK_MAX_THREADS];
202  ThreadProcessIdType m_SpawnedThreadProcessID[ITK_MAX_THREADS];
203  ThreadInfoStruct m_SpawnedThreadInfoArray[ITK_MAX_THREADS];
204 
206  void *m_SingleData;
207  void *m_MultipleData[ITK_MAX_THREADS];
208 
212  static ThreadIdType m_GlobalMaximumNumberOfThreads;
213 
218  static bool m_GlobalDefaultUseThreadPool;
219 
220  /* Global variable defining the default number of threads to set at
221  * construction time of a MultiThreader instance. The
222  * m_GlobalDefaultNumberOfThreads must always be less than or equal to the
223  * m_GlobalMaximumNumberOfThreads and larger or equal to 1 once it has been
224  * initialized in the constructor of the first MultiThreader instantiation.
225  */
226  static ThreadIdType m_GlobalDefaultNumberOfThreads;
227 
229  static ThreadIdType GetGlobalDefaultNumberOfThreadsByPlatform();
230 
240  ThreadIdType m_NumberOfThreads;
241 
248  static ITK_THREAD_RETURN_TYPE SingleMethodProxy(void *arg);
249 
251  ThreadProcessIdType ThreadPoolDispatchSingleMethodThread(ThreadInfoStruct *);
252 
254  void ThreadPoolWaitForSingleMethodThread(ThreadProcessIdType);
255 
257  ThreadProcessIdType SpawnDispatchSingleMethodThread(ThreadInfoStruct *);
258 
260  void SpawnWaitForSingleMethodThread(ThreadProcessIdType);
261 
268  ThreadProcessIdType DispatchSingleMethodThread(ThreadInfoStruct *);
269 
273  void WaitForSingleMethodThread(ThreadProcessIdType);
274 
void(* ThreadFunctionType)(void *)
#define ITK_MAX_THREADS
SmartPointer< Self > Pointer
Definition: itkThreadPool.h:79
#define ITK_THREAD_RETURN_TYPE
unsigned int ThreadIdType
Definition: itkIntTypes.h:159
int ThreadProcessIdType
SmartPointer< Self > Pointer
Definition: itkMutexLock.h:109