ITK  6.0.0
Insight Toolkit
itkPlatformMultiThreader.h
Go to the documentation of this file.
1 /*=========================================================================
2  *
3  * Copyright NumFOCUS
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  * https://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_MOVE(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  itkOverrideGetNameOfClassMacro(PlatformMultiThreader);
66 
67 #if !defined(ITK_LEGACY_REMOVE)
68 
73  itkLegacyMacro(static void SetGlobalMaximumNumberOfThreads(ThreadIdType val))
74  {
75  itkGenericOutputMacro("Warning: SetGlobalMaximumNumberOfThreads should now be called on itk::MultiThreaderBase. It "
76  "can affect all MultiThreaderBase's derived classes in ITK");
77  Superclass::SetGlobalMaximumNumberOfThreads(val);
78  }
79  itkLegacyMacro(static ThreadIdType GetGlobalMaximumNumberOfThreads())
80  {
81  itkGenericOutputMacro("Warning: GetGlobalMaximumNumberOfThreads should now be called on itk::MultiThreaderBase. It "
82  "can affect all MultiThreaderBase's derived classes in ITK");
83  return Superclass::GetGlobalMaximumNumberOfThreads();
84  }
91  itkLegacyMacro(static void SetGlobalDefaultNumberOfThreads(ThreadIdType val))
92  {
93  itkGenericOutputMacro("Warning: SetGlobalDefaultNumberOfThreads should now be called on itk::MultiThreaderBase. It "
94  "can affect all MultiThreaderBase's derived classes in ITK");
95  Superclass::SetGlobalDefaultNumberOfThreads(val);
96  }
97  itkLegacyMacro(static ThreadIdType GetGlobalDefaultNumberOfThreads())
98  {
99  itkGenericOutputMacro("Warning: GetGlobalDefaultNumberOfThreads should now be called on itk::MultiThreaderBase. It "
100  "can affect all MultiThreaderBase's derived classes in ITK");
101  return Superclass::GetGlobalDefaultNumberOfThreads();
102  }
103 #endif
104 
110  void
111  SingleMethodExecute() override;
112 
117  itkLegacyMacro(void MultipleMethodExecute();)
118 
124  void
125  SetSingleMethod(ThreadFunctionType, void * data) override;
126 
129  itkLegacyMacro(void SetMultipleMethod(ThreadIdType index, ThreadFunctionType, void * data);)
130 
135  itkLegacyMacro(ThreadIdType SpawnThread(ThreadFunctionType, void * data);)
136 
139  itkLegacyMacro(void TerminateThread(ThreadIdType thread_id);)
140 
141  void
142  SetMaximumNumberOfThreads(ThreadIdType numberOfThreads) override;
143  void
144  SetNumberOfWorkUnits(ThreadIdType numberOfWorkUnits) override;
145 
146  struct WorkUnitInfo : MultiThreaderBase::WorkUnitInfo
147  {
148  int * ActiveFlag = nullptr;
149  std::shared_ptr<std::mutex> ActiveFlagLock;
150  };
151 
152 protected:
153  PlatformMultiThreader();
154  ~PlatformMultiThreader() override;
155  void
156  PrintSelf(std::ostream & os, Indent indent) const override;
157 
158 private:
162  WorkUnitInfo m_ThreadInfoArray[ITK_MAX_THREADS]{};
163 
166  int m_SpawnedThreadActiveFlag[ITK_MAX_THREADS]{};
167  std::shared_ptr<std::mutex> m_SpawnedThreadActiveFlagMutex[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 
181  SpawnDispatchSingleMethodThread(WorkUnitInfo *);
182 
184  void SpawnWaitForSingleMethodThread(ThreadProcessIdType);
185 
Pointer
SmartPointer< Self > Pointer
Definition: itkAddImageFilter.h:93
ConstPointer
SmartPointer< const Self > ConstPointer
Definition: itkAddImageFilter.h:94
itk::ITK_MAX_THREADS
constexpr vcl_size_t ITK_MAX_THREADS
Definition: itkThreadSupport.h:79
itk::ThreadFunctionType
void(*)(void *) ThreadFunctionType
Definition: itkThreadSupport.h:82
itk::ThreadIdType
unsigned int ThreadIdType
Definition: itkIntTypes.h:102
itk
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....
Definition: itkAnatomicalOrientation.h:29
itkMultiThreaderBase.h
AddImageFilter
Definition: itkAddImageFilter.h:81
Superclass
BinaryGeneratorImageFilter< TInputImage1, TInputImage2, TOutputImage > Superclass
Definition: itkAddImageFilter.h:90
itk::ThreadProcessIdType
int ThreadProcessIdType
Definition: itkThreadSupport.h:83