ITK  4.13.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 {
52 class ITKCommon_EXPORT MultiThreader : public Object
53 {
54 public:
56  typedef MultiThreader Self;
57  typedef Object Superclass;
58  typedef SmartPointer<Self> Pointer;
59  typedef SmartPointer<const Self> ConstPointer;
60 
62  itkNewMacro(Self);
63 
65  itkTypeMacro(MultiThreader, Object);
66 
70  void SetNumberOfThreads(ThreadIdType numberOfThreads);
71 
72  itkGetConstMacro(NumberOfThreads, ThreadIdType);
73 
79  static void SetGlobalMaximumNumberOfThreads(ThreadIdType val);
80  static ThreadIdType GetGlobalMaximumNumberOfThreads();
82 
87  static void SetGlobalDefaultUseThreadPool( const bool GlobalDefaultUseThreadPool );
88  static bool GetGlobalDefaultUseThreadPool( );
90 
95  static void SetGlobalDefaultNumberOfThreads(ThreadIdType val);
96 
97  static ThreadIdType GetGlobalDefaultNumberOfThreads();
98 
103  void SingleMethodExecute();
104 
109  void MultipleMethodExecute();
110 
116  void SetSingleMethod(ThreadFunctionType, void *data);
117 
120  void SetMultipleMethod(ThreadIdType index, ThreadFunctionType, void *data);
121 
125  ThreadIdType SpawnThread(ThreadFunctionType, void *data);
126 
128  void TerminateThread(ThreadIdType thread_id);
129 
133  itkSetObjectMacro(ThreadPool, ThreadPool);
134 
136  itkGetModifiableObjectMacro(ThreadPool, ThreadPool);
137 
141  itkSetMacro(UseThreadPool,bool);
142 
144  itkGetMacro(UseThreadPool,bool);
145 
146  typedef ThreadPool::Semaphore JobSemaphoreType;
147 
159 #ifdef ThreadInfoStruct
160 #undef ThreadInfoStruct
161 #endif
162  struct ThreadInfoStruct
163  {
164  ThreadIdType ThreadID;
165  ThreadIdType NumberOfThreads;
166  int *ActiveFlag;
167  MutexLock::Pointer ActiveFlagLock;
168  void *UserData;
169  ThreadFunctionType ThreadFunction;
170  JobSemaphoreType Semaphore;
171  enum { SUCCESS, ITK_EXCEPTION, ITK_PROCESS_ABORTED_EXCEPTION, STD_EXCEPTION, UNKNOWN } ThreadExitCode;
172  };
173 
174 protected:
175  MultiThreader();
176  ~MultiThreader() ITK_OVERRIDE;
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 
236  ThreadIdType m_NumberOfThreads;
237 
244  static ITK_THREAD_RETURN_TYPE SingleMethodProxy(void *arg);
245 
247  void ThreadPoolDispatchSingleMethodThread(ThreadInfoStruct *);
248 
250  ThreadProcessIdType SpawnDispatchSingleMethodThread(ThreadInfoStruct *);
251 
253  void SpawnWaitForSingleMethodThread(ThreadProcessIdType);
254 
void(* ThreadFunctionType)(void *)
#define ITK_MAX_THREADS
#define ITK_THREAD_RETURN_TYPE
unsigned int ThreadIdType
Definition: itkIntTypes.h:159
int ThreadProcessIdType
ThreadJob::Semaphore Semaphore
Definition: itkThreadPool.h:71
SmartPointer< Self > Pointer
Definition: itkMutexLock.h:109