VTK  9.3.1
vtkMultiThreader.h
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
2 // SPDX-License-Identifier: BSD-3-Clause
19 #ifndef vtkMultiThreader_h
20 #define vtkMultiThreader_h
21 
22 #include "vtkCommonCoreModule.h" // For export macro
23 #include "vtkObject.h"
24 #include "vtkThreads.h" // for VTK_MAX_THREADS
25 
26 #include <mutex> // For std::mutex
27 
28 #if defined(VTK_USE_PTHREADS)
29 #include <pthread.h> // Needed for PTHREAD implementation of mutex
30 #include <sys/types.h> // Needed for unix implementation of pthreads
31 #include <unistd.h> // Needed for unix implementation of pthreads
32 #endif
33 
34 // If VTK_USE_PTHREADS is defined, then pthread_create() will be
35 // used to create multiple threads
36 
37 // If VTK_USE_PTHREADS is defined, then the multithreaded
38 // function is of type void *, and returns nullptr
39 // Otherwise the type is void which is correct for WIN32
40 
41 // Defined in vtkThreads.h:
42 // VTK_MAX_THREADS
43 // VTK_THREAD_RETURN_VALUE
44 // VTK_THREAD_RETURN_TYPE
45 
46 #ifdef VTK_USE_PTHREADS
47 typedef void* (*vtkThreadFunctionType)(void*);
48 typedef pthread_t vtkThreadProcessIDType;
49 // #define VTK_THREAD_RETURN_VALUE nullptr
50 // #define VTK_THREAD_RETURN_TYPE void *
51 typedef pthread_t vtkMultiThreaderIDType;
52 #endif
53 
54 #ifdef VTK_USE_WIN32_THREADS
55 typedef vtkWindowsLPTHREAD_START_ROUTINE vtkThreadFunctionType;
56 typedef vtkWindowsHANDLE vtkThreadProcessIDType;
57 // #define VTK_THREAD_RETURN_VALUE 0
58 // #define VTK_THREAD_RETURN_TYPE DWORD __stdcall
59 typedef vtkWindowsDWORD vtkMultiThreaderIDType;
60 #endif
61 
62 #if !defined(VTK_USE_PTHREADS) && !defined(VTK_USE_WIN32_THREADS)
63 typedef void (*vtkThreadFunctionType)(void*);
65 // #define VTK_THREAD_RETURN_VALUE
66 // #define VTK_THREAD_RETURN_TYPE void
68 #endif
69 
70 VTK_ABI_NAMESPACE_BEGIN
71 class VTKCOMMONCORE_EXPORT vtkMultiThreader : public vtkObject
72 {
73 public:
74  static vtkMultiThreader* New();
75 
76  vtkTypeMacro(vtkMultiThreader, vtkObject);
77  void PrintSelf(ostream& os, vtkIndent indent) override;
78 
91  class ThreadInfo
92  {
93  public:
94  int ThreadID;
96  int* ActiveFlag;
97  std::mutex* ActiveFlagLock;
98  void* UserData;
99  };
100 
102 
107  vtkSetClampMacro(NumberOfThreads, int, 1, VTK_MAX_THREADS);
108  virtual int GetNumberOfThreads();
110 
112 
115  static int GetGlobalStaticMaximumNumberOfThreads();
117 
119 
124  static void SetGlobalMaximumNumberOfThreads(int val);
125  static int GetGlobalMaximumNumberOfThreads();
127 
129 
134  static void SetGlobalDefaultNumberOfThreads(int val);
135  static int GetGlobalDefaultNumberOfThreads();
137 
138  // These methods are excluded from wrapping 1) because the
139  // wrapper gives up on them and 2) because they really shouldn't be
140  // called from a script anyway.
141 
146  void SingleMethodExecute();
147 
153  void MultipleMethodExecute();
154 
162  void SetSingleMethod(vtkThreadFunctionType, void* data);
163 
168  void SetMultipleMethod(int index, vtkThreadFunctionType, void* data);
169 
175  int SpawnThread(vtkThreadFunctionType, void* data);
176 
180  void TerminateThread(int threadId);
181 
185  vtkTypeBool IsThreadActive(int threadId);
186 
190  static vtkMultiThreaderIDType GetCurrentThreadID();
191 
195  static vtkTypeBool ThreadsEqual(vtkMultiThreaderIDType t1, vtkMultiThreaderIDType t2);
196 
197 protected:
199  ~vtkMultiThreader() override;
200 
201  // The number of threads to use
203 
204  // An array of thread info containing a thread id
205  // (0, 1, 2, .. VTK_MAX_THREADS-1), the thread count, and a pointer
206  // to void so that user data can be passed to each thread
207  ThreadInfo ThreadInfoArray[VTK_MAX_THREADS];
208 
209  // The methods
211  vtkThreadFunctionType MultipleMethod[VTK_MAX_THREADS];
212 
213  // Storage of MutexFunctions and ints used to control spawned
214  // threads and the spawned thread ids
215  int SpawnedThreadActiveFlag[VTK_MAX_THREADS];
216  std::mutex* SpawnedThreadActiveFlagLock[VTK_MAX_THREADS];
217  vtkThreadProcessIDType SpawnedThreadProcessID[VTK_MAX_THREADS];
218  ThreadInfo SpawnedThreadInfoArray[VTK_MAX_THREADS];
219 
220  // Internal storage of the data
221  void* SingleData;
222  void* MultipleData[VTK_MAX_THREADS];
223 
224 private:
225  vtkMultiThreader(const vtkMultiThreader&) = delete;
226  void operator=(const vtkMultiThreader&) = delete;
227 };
228 
230 
231 VTK_ABI_NAMESPACE_END
232 #endif
abstract base class for most VTK objects
Definition: vtkObject.h:51
void PrintSelf(ostream &os, vtkIndent indent) override
Methods invoked by print to print information about the object including superclasses.
void(* vtkThreadFunctionType)(void *)
A class for performing multithreaded execution.
This is the structure that is passed to the thread that is created from the SingleMethodExecute, MultipleMethodExecute or the SpawnThread method.
int vtkMultiThreaderIDType
int vtkTypeBool
Definition: vtkABI.h:64
a simple class to control print indentation
Definition: vtkIndent.h:28
int vtkThreadProcessIDType
static vtkObject * New()
Create an object with Debug turned off, modified time initialized to zero, and reference counting on...
vtkThreadFunctionType SingleMethod