ITK  5.2.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  * 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_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  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
115  SingleMethodExecute() override;
116 
121  itkLegacyMacro(void MultipleMethodExecute());
122 
128  void
129  SetSingleMethod(ThreadFunctionType, void * data) override;
130 
133  itkLegacyMacro(void SetMultipleMethod(ThreadIdType index, ThreadFunctionType, void * data));
134 
139  itkLegacyMacro(ThreadIdType SpawnThread(ThreadFunctionType, void * data));
140 
143  itkLegacyMacro(void TerminateThread(ThreadIdType thread_id));
144 
145  void
146  SetMaximumNumberOfThreads(ThreadIdType numberOfThreads) override;
147  void
148  SetNumberOfWorkUnits(ThreadIdType numberOfWorkUnits) override;
149 
150  struct WorkUnitInfo : MultiThreaderBase::WorkUnitInfo
151  {
152  int * ActiveFlag = nullptr;
153  std::shared_ptr<std::mutex> ActiveFlagLock;
154  };
155 
156 protected:
157  PlatformMultiThreader();
158  ~PlatformMultiThreader() override;
159  void
160  PrintSelf(std::ostream & os, Indent indent) const override;
161 
162 private:
166  WorkUnitInfo m_ThreadInfoArray[ITK_MAX_THREADS];
167 
170  int m_SpawnedThreadActiveFlag[ITK_MAX_THREADS];
171  std::shared_ptr<std::mutex> m_SpawnedThreadActiveFlagLock[ITK_MAX_THREADS];
172  ThreadProcessIdType m_SpawnedThreadProcessID[ITK_MAX_THREADS];
173  WorkUnitInfo m_SpawnedThreadInfoArray[ITK_MAX_THREADS];
174 
175 #if !defined(ITK_LEGACY_REMOVE)
176 
177  ThreadFunctionType m_MultipleMethod[ITK_MAX_THREADS];
178 
180  void * m_MultipleData[ITK_MAX_THREADS];
181 #endif
182 
185  SpawnDispatchSingleMethodThread(WorkUnitInfo *);
186 
188  void SpawnWaitForSingleMethodThread(ThreadProcessIdType);
189 
itk::ThreadFunctionType
void(*)(void *) ThreadFunctionType
Definition: itkThreadSupport.h:77
itk::ThreadIdType
unsigned int ThreadIdType
Definition: itkIntTypes.h:99
itk::ITK_MAX_THREADS
constexpr std::vcl_size_t ITK_MAX_THREADS
Definition: itkThreadSupport.h:74
itk
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....
Definition: itkAnnulusOperator.h:24
itkMultiThreaderBase.h
itk::ThreadProcessIdType
int ThreadProcessIdType
Definition: itkThreadSupport.h:78