ITK  4.3.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 namespace itk
36 {
53 class ITKCommon_EXPORT MultiThreader:public Object
54 {
55 public:
57  typedef MultiThreader Self;
58  typedef Object Superclass;
59  typedef SmartPointer< Self > Pointer;
60  typedef SmartPointer< const Self > ConstPointer;
61 
63  itkNewMacro(Self);
64 
66  itkTypeMacro(MultiThreader, Object);
67 
71  void SetNumberOfThreads(ThreadIdType numberOfThreads);
72 
73  itkGetConstMacro(NumberOfThreads, ThreadIdType);
74 
80  static void SetGlobalMaximumNumberOfThreads(ThreadIdType val);
81 
82  static ThreadIdType GetGlobalMaximumNumberOfThreads();
83 
88  static void SetGlobalDefaultNumberOfThreads(ThreadIdType val);
89 
90  static ThreadIdType GetGlobalDefaultNumberOfThreads();
91 
96  void SingleMethodExecute();
97 
102  void MultipleMethodExecute();
103 
109  void SetSingleMethod(ThreadFunctionType, void *data);
110 
113  void SetMultipleMethod(ThreadIdType index, ThreadFunctionType, void *data);
114 
118  int SpawnThread(ThreadFunctionType, void *data);
119 
121  void TerminateThread(ThreadIdType thread_id);
122 
134 #ifdef ThreadInfoStruct
135 #undef ThreadInfoStruct
136 #endif
137  struct ThreadInfoStruct {
138  ThreadIdType ThreadID;
139  ThreadIdType NumberOfThreads;
140  int *ActiveFlag;
141  MutexLock::Pointer ActiveFlagLock;
142  void *UserData;
143  ThreadFunctionType ThreadFunction;
144  enum { SUCCESS, ITK_EXCEPTION, ITK_PROCESS_ABORTED_EXCEPTION, STD_EXCEPTION, UNKNOWN } ThreadExitCode;
145  };
146 
147 protected:
148  MultiThreader();
149  ~MultiThreader();
150  void PrintSelf(std::ostream & os, Indent indent) const;
151 
152 private:
153  MultiThreader(const Self &); //purposely not implemented
154  void operator=(const Self &); //purposely not implemented
155 
159  ThreadInfoStruct m_ThreadInfoArray[ITK_MAX_THREADS];
160 
162  ThreadFunctionType m_SingleMethod;
163  ThreadFunctionType m_MultipleMethod[ITK_MAX_THREADS];
164 
167  int m_SpawnedThreadActiveFlag[ITK_MAX_THREADS];
168  MutexLock::Pointer m_SpawnedThreadActiveFlagLock[ITK_MAX_THREADS];
169  ThreadProcessIDType m_SpawnedThreadProcessID[ITK_MAX_THREADS];
170  ThreadInfoStruct m_SpawnedThreadInfoArray[ITK_MAX_THREADS];
171 
173  void *m_SingleData;
174  void *m_MultipleData[ITK_MAX_THREADS];
175 
179  static ThreadIdType m_GlobalMaximumNumberOfThreads;
180 
181  /* Global variable defining the default number of threads to set at
182  * construction time of a MultiThreader instance. The
183  * m_GlobalDefaultNumberOfThreads must always be less than or equal to the
184  * m_GlobalMaximumNumberOfThreads and larger or equal to 1 once it has been
185  * initialized in the constructor of the first MultiThreader instantiation.
186  */
187  static ThreadIdType m_GlobalDefaultNumberOfThreads;
188 
190  static ThreadIdType GetGlobalDefaultNumberOfThreadsByPlatform();
191 
201  ThreadIdType m_NumberOfThreads;
202 
209  static ITK_THREAD_RETURN_TYPE SingleMethodProxy(void *arg);
210 
217  ThreadProcessIDType DispatchSingleMethodThread(ThreadInfoStruct *);
218 
222  void WaitForSingleMethodThread(ThreadProcessIDType);
223 
224