ITK  5.0.0
Insight Segmentation and Registration Toolkit
itkPlatformMultiThreader.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 itkPlatformMultiThreader_h
29 #define itkPlatformMultiThreader_h
30 
31 #include "itkMultiThreaderBase.h"
32 #include <mutex>
33 #include <memory>
34 
35 namespace itk
36 {
50 class ITKCommon_EXPORT PlatformMultiThreader : public MultiThreaderBase
51 {
52 public:
53  ITK_DISALLOW_COPY_AND_ASSIGN(PlatformMultiThreader);
54 
56  using Self = PlatformMultiThreader;
57  using Superclass = MultiThreaderBase;
58  using Pointer = SmartPointer<Self>;
59  using ConstPointer = SmartPointer<const Self>;
60 
62  itkNewMacro(Self);
63 
65  itkTypeMacro(PlatformMultiThreader, MultiThreaderBase);
66 
67 #if !defined ( ITK_LEGACY_REMOVE )
68 
73  itkLegacyMacro(static void SetGlobalMaximumNumberOfThreads(ThreadIdType val))
74  {
75  itkGenericOutputMacro("Warning: SetGlobalMaximumNumberOfThreads \
76 should now be called on itk::MultiThreaderBase. \
77 It can affect all MultiThreaderBase's derived classes in ITK");
78  Superclass::SetGlobalMaximumNumberOfThreads(val);
79  }
80  itkLegacyMacro(static ThreadIdType GetGlobalMaximumNumberOfThreads())
81  {
82  itkGenericOutputMacro("Warning: GetGlobalMaximumNumberOfThreads \
83 should now be called on itk::MultiThreaderBase. \
84 It can affect all MultiThreaderBase's derived classes in ITK");
85  return Superclass::GetGlobalMaximumNumberOfThreads();
86  }
88 
93  itkLegacyMacro(static void SetGlobalDefaultNumberOfThreads(ThreadIdType val))
94  {
95  itkGenericOutputMacro("Warning: SetGlobalDefaultNumberOfThreads \
96 should now be called on itk::MultiThreaderBase. \
97 It can affect all MultiThreaderBase's derived classes in ITK");
98  Superclass::SetGlobalDefaultNumberOfThreads(val);
99  }
100  itkLegacyMacro(static ThreadIdType GetGlobalDefaultNumberOfThreads())
101  {
102  itkGenericOutputMacro("Warning: GetGlobalDefaultNumberOfThreads \
103 should now be called on itk::MultiThreaderBase. \
104 It can affect all MultiThreaderBase's derived classes in ITK");
105  return Superclass::GetGlobalDefaultNumberOfThreads();
106  }
107 #endif
108 
109 
114  void SingleMethodExecute() override;
115 
120  itkLegacyMacro(void MultipleMethodExecute());
121 
127  void SetSingleMethod(ThreadFunctionType, void *data) override;
128 
131  itkLegacyMacro(void SetMultipleMethod(ThreadIdType index, ThreadFunctionType, void *data));
132 
137  itkLegacyMacro(ThreadIdType SpawnThread(ThreadFunctionType, void *data));
138 
141  itkLegacyMacro(void TerminateThread(ThreadIdType thread_id));
142 
143  virtual void SetMaximumNumberOfThreads( ThreadIdType numberOfThreads ) override;
144  virtual void SetNumberOfWorkUnits( ThreadIdType numberOfWorkUnits ) override;
145 
146  struct WorkUnitInfo: MultiThreaderBase::WorkUnitInfo
147  {
148  int *ActiveFlag;
149  std::shared_ptr< std::mutex > ActiveFlagLock;
150  };
151 
152 protected:
153  PlatformMultiThreader();
154  ~PlatformMultiThreader() override;
155  void PrintSelf(std::ostream & os, Indent indent) const override;
156 
157 private:
161  WorkUnitInfo m_ThreadInfoArray[ITK_MAX_THREADS];
162 
165  int m_SpawnedThreadActiveFlag[ITK_MAX_THREADS];
166  std::shared_ptr< std::mutex >
167  m_SpawnedThreadActiveFlagLock[ITK_MAX_THREADS];
168  ThreadProcessIdType m_SpawnedThreadProcessID[ITK_MAX_THREADS];
169  WorkUnitInfo m_SpawnedThreadInfoArray[ITK_MAX_THREADS];
170 
171 #if !defined ( ITK_LEGACY_REMOVE )
172 
173  ThreadFunctionType m_MultipleMethod[ITK_MAX_THREADS];
174 
176  void *m_MultipleData[ITK_MAX_THREADS];
177 #endif
178 
180  ThreadProcessIdType SpawnDispatchSingleMethodThread(WorkUnitInfo *);
181 
183  void SpawnWaitForSingleMethodThread(ThreadProcessIdType);
184 
void(* ThreadFunctionType)(void *)
constexpr std::vcl_size_t ITK_MAX_THREADS
unsigned int ThreadIdType
Definition: itkIntTypes.h:99
int ThreadProcessIdType