ITK  4.12.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  ITK_DISALLOW_COPY_AND_ASSIGN(MultiThreader);
181 
182  // Thread pool instance and factory
183  ThreadPool::Pointer m_ThreadPool;
184 
185  // choose whether to use Spawn or ThreadPool methods
186  bool m_UseThreadPool;
187 
191  ThreadInfoStruct m_ThreadInfoArray[ITK_MAX_THREADS];
192 
194  ThreadFunctionType m_SingleMethod;
195  ThreadFunctionType m_MultipleMethod[ITK_MAX_THREADS];
196 
199  int m_SpawnedThreadActiveFlag[ITK_MAX_THREADS];
200  MutexLock::Pointer m_SpawnedThreadActiveFlagLock[ITK_MAX_THREADS];
201  ThreadProcessIdType m_SpawnedThreadProcessID[ITK_MAX_THREADS];
202  ThreadInfoStruct m_SpawnedThreadInfoArray[ITK_MAX_THREADS];
203 
205  void *m_SingleData;
206  void *m_MultipleData[ITK_MAX_THREADS];
207 
211  static ThreadIdType m_GlobalMaximumNumberOfThreads;
212 
217  static bool m_GlobalDefaultUseThreadPool;
218 
219  /* Global variable defining the default number of threads to set at
220  * construction time of a MultiThreader instance. The
221  * m_GlobalDefaultNumberOfThreads must always be less than or equal to the
222  * m_GlobalMaximumNumberOfThreads and larger or equal to 1 once it has been
223  * initialized in the constructor of the first MultiThreader instantiation.
224  */
225  static ThreadIdType m_GlobalDefaultNumberOfThreads;
226 
228  static ThreadIdType GetGlobalDefaultNumberOfThreadsByPlatform();
229 
239  ThreadIdType m_NumberOfThreads;
240 
247  static ITK_THREAD_RETURN_TYPE SingleMethodProxy(void *arg);
248 
250  ThreadProcessIdType ThreadPoolDispatchSingleMethodThread(ThreadInfoStruct *);
251 
253  void ThreadPoolWaitForSingleMethodThread(ThreadProcessIdType);
254 
256  ThreadProcessIdType SpawnDispatchSingleMethodThread(ThreadInfoStruct *);
257 
259  void SpawnWaitForSingleMethodThread(ThreadProcessIdType);
260 
267  ThreadProcessIdType DispatchSingleMethodThread(ThreadInfoStruct *);
268 
272  void WaitForSingleMethodThread(ThreadProcessIdType);
273 
void(* ThreadFunctionType)(void *)
virtual void PrintSelf(std::ostream &os, Indent indent) const override
#define ITK_MAX_THREADS
class ITK_FORCE_EXPORT_MACRO(ITKCommon) DataObject typedef Object Superclass
SmartPointer< Self > Pointer
Definition: itkThreadPool.h:79
#define ITK_THREAD_RETURN_TYPE
SmartPointer< Self > Pointer
SmartPointer< const Self > ConstPointer
unsigned int ThreadIdType
Definition: itkIntTypes.h:159
int ThreadProcessIdType
SmartPointer< Self > Pointer
Definition: itkMutexLock.h:109