VTK
vtkMultiThreader.h
Go to the documentation of this file.
1 /*=========================================================================
2 
3  Program: Visualization Toolkit
4  Module: vtkMultiThreader.h
5 
6  Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
7  All rights reserved.
8  See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
9 
10  This software is distributed WITHOUT ANY WARRANTY; without even
11  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
12  PURPOSE. See the above copyright notice for more information.
13 
14 =========================================================================*/
31 #ifndef vtkMultiThreader_h
32 #define vtkMultiThreader_h
33 
34 #include "vtkCommonCoreModule.h" // For export macro
35 #include "vtkObject.h"
36 
37 #if defined(VTK_USE_PTHREADS)
38 #include <pthread.h> // Needed for PTHREAD implementation of mutex
39 #include <sys/types.h> // Needed for unix implementation of pthreads
40 #include <unistd.h> // Needed for unix implementation of pthreads
41 #endif
42 
43 // If VTK_USE_PTHREADS is defined, then pthread_create() will be
44 // used to create multiple threads
45 
46 // Defined in vtkSystemIncludes.h:
47 // VTK_MAX_THREADS
48 
49 // If VTK_USE_PTHREADS is defined, then the multithreaded
50 // function is of type void *, and returns nullptr
51 // Otherwise the type is void which is correct for WIN32
52 
53 // Defined in vtkSystemIncludes.h:
54 // VTK_THREAD_RETURN_VALUE
55 // VTK_THREAD_RETURN_TYPE
56 
57 #ifdef VTK_USE_PTHREADS
58 typedef void *(*vtkThreadFunctionType)(void *);
59 typedef pthread_t vtkThreadProcessIDType;
60 // #define VTK_THREAD_RETURN_VALUE nullptr
61 // #define VTK_THREAD_RETURN_TYPE void *
62 typedef pthread_t vtkMultiThreaderIDType;
63 #endif
64 
65 #ifdef VTK_USE_WIN32_THREADS
66 typedef vtkWindowsLPTHREAD_START_ROUTINE vtkThreadFunctionType;
67 typedef vtkWindowsHANDLE vtkThreadProcessIDType;
68 // #define VTK_THREAD_RETURN_VALUE 0
69 // #define VTK_THREAD_RETURN_TYPE DWORD __stdcall
70 typedef vtkWindowsDWORD vtkMultiThreaderIDType;
71 #endif
72 
73 #if !defined(VTK_USE_PTHREADS) && !defined(VTK_USE_WIN32_THREADS)
74 typedef void (*vtkThreadFunctionType)(void *);
76 // #define VTK_THREAD_RETURN_VALUE
77 // #define VTK_THREAD_RETURN_TYPE void
79 #endif
80 
81 class vtkMutexLock;
82 
83 class VTKCOMMONCORE_EXPORT vtkMultiThreader : public vtkObject
84 {
85 public:
86  static vtkMultiThreader *New();
87 
88  vtkTypeMacro(vtkMultiThreader,vtkObject);
89  void PrintSelf(ostream& os, vtkIndent indent) override;
90 
104 #define ThreadInfoStruct vtkMultiThreader::ThreadInfo
106  {
107  public:
108  int ThreadID;
112  void *UserData;
113  };
114 
116 
121  vtkSetClampMacro( NumberOfThreads, int, 1, VTK_MAX_THREADS );
122  virtual int GetNumberOfThreads();
124 
126 
131  static void SetGlobalMaximumNumberOfThreads(int val);
132  static int GetGlobalMaximumNumberOfThreads();
134 
136 
141  static void SetGlobalDefaultNumberOfThreads(int val);
142  static int GetGlobalDefaultNumberOfThreads();
144 
145  // These methods are excluded from Tcl wrapping 1) because the
146  // wrapper gives up on them and 2) because they really shouldn't be
147  // called from a script anyway.
148 
153  void SingleMethodExecute();
154 
160  void MultipleMethodExecute();
161 
169  void SetSingleMethod(vtkThreadFunctionType, void *data );
170 
175  void SetMultipleMethod( int index, vtkThreadFunctionType, void *data );
176 
182  int SpawnThread( vtkThreadFunctionType, void *data );
183 
187  void TerminateThread( int thread_id );
188 
192  vtkTypeBool IsThreadActive( int threadID );
193 
197  static vtkMultiThreaderIDType GetCurrentThreadID();
198 
202  static vtkTypeBool ThreadsEqual(vtkMultiThreaderIDType t1,
204 
205 protected:
207  ~vtkMultiThreader() override;
208 
209  // The number of threads to use
211 
212  // An array of thread info containing a thread id
213  // (0, 1, 2, .. VTK_MAX_THREADS-1), the thread count, and a pointer
214  // to void so that user data can be passed to each thread
215  ThreadInfo ThreadInfoArray[VTK_MAX_THREADS];
216 
217  // The methods
219  vtkThreadFunctionType MultipleMethod[VTK_MAX_THREADS];
220 
221  // Storage of MutexFunctions and ints used to control spawned
222  // threads and the spawned thread ids
223  int SpawnedThreadActiveFlag[VTK_MAX_THREADS];
224  vtkMutexLock *SpawnedThreadActiveFlagLock[VTK_MAX_THREADS];
225  vtkThreadProcessIDType SpawnedThreadProcessID[VTK_MAX_THREADS];
226  ThreadInfo SpawnedThreadInfoArray[VTK_MAX_THREADS];
227 
228  // Internal storage of the data
229  void *SingleData;
230  void *MultipleData[VTK_MAX_THREADS];
231 
232 private:
233  vtkMultiThreader(const vtkMultiThreader&) = delete;
234  void operator=(const vtkMultiThreader&) = delete;
235 };
236 
237 #endif
238 
abstract base class for most VTK objects
Definition: vtkObject.h:59
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.
int vtkMultiThreaderIDType
int vtkTypeBool
Definition: vtkABI.h:69
a simple class to control print indentation
Definition: vtkIndent.h:39
int vtkThreadProcessIDType
static vtkObject * New()
Create an object with Debug turned off, modified time initialized to zero, and reference counting on...
mutual exclusion locking class
Definition: vtkMutexLock.h:80
vtkThreadFunctionType SingleMethod