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 =========================================================================*/
25 #ifndef vtkMultiThreader_h
26 #define vtkMultiThreader_h
27 
28 #include "vtkCommonCoreModule.h" // For export macro
29 #include "vtkObject.h"
30 
31 #if defined(VTK_USE_PTHREADS)
32 #include <pthread.h> // Needed for PTHREAD implementation of mutex
33 #include <sys/types.h> // Needed for unix implementation of pthreads
34 #include <unistd.h> // Needed for unix implementation of pthreads
35 #endif
36 
37 // If VTK_USE_PTHREADS is defined, then pthread_create() will be
38 // used to create multiple threads
39 
40 // Defined in vtkSystemIncludes.h:
41 // VTK_MAX_THREADS
42 
43 // If VTK_USE_PTHREADS is defined, then the multithreaded
44 // function is of type void *, and returns nullptr
45 // Otherwise the type is void which is correct for WIN32
46 
47 // Defined in vtkSystemIncludes.h:
48 // VTK_THREAD_RETURN_VALUE
49 // VTK_THREAD_RETURN_TYPE
50 
51 #ifdef VTK_USE_PTHREADS
52 typedef void *(*vtkThreadFunctionType)(void *);
53 typedef pthread_t vtkThreadProcessIDType;
54 // #define VTK_THREAD_RETURN_VALUE nullptr
55 // #define VTK_THREAD_RETURN_TYPE void *
56 typedef pthread_t vtkMultiThreaderIDType;
57 #endif
58 
59 #ifdef VTK_USE_WIN32_THREADS
60 typedef vtkWindowsLPTHREAD_START_ROUTINE vtkThreadFunctionType;
61 typedef vtkWindowsHANDLE vtkThreadProcessIDType;
62 // #define VTK_THREAD_RETURN_VALUE 0
63 // #define VTK_THREAD_RETURN_TYPE DWORD __stdcall
64 typedef vtkWindowsDWORD vtkMultiThreaderIDType;
65 #endif
66 
67 #if !defined(VTK_USE_PTHREADS) && !defined(VTK_USE_WIN32_THREADS)
68 typedef void (*vtkThreadFunctionType)(void *);
70 // #define VTK_THREAD_RETURN_VALUE
71 // #define VTK_THREAD_RETURN_TYPE void
73 #endif
74 
75 class vtkMutexLock;
76 
77 class VTKCOMMONCORE_EXPORT vtkMultiThreader : public vtkObject
78 {
79 public:
80  static vtkMultiThreader *New();
81 
82  vtkTypeMacro(vtkMultiThreader,vtkObject);
83  void PrintSelf(ostream& os, vtkIndent indent) override;
84 
98 #define ThreadInfoStruct vtkMultiThreader::ThreadInfo
99  class ThreadInfo
100  {
101  public:
102  int ThreadID;
106  void *UserData;
107  };
108 
110 
115  vtkSetClampMacro( NumberOfThreads, int, 1, VTK_MAX_THREADS );
116  virtual int GetNumberOfThreads();
118 
120 
125  static void SetGlobalMaximumNumberOfThreads(int val);
126  static int GetGlobalMaximumNumberOfThreads();
128 
130 
135  static void SetGlobalDefaultNumberOfThreads(int val);
136  static int GetGlobalDefaultNumberOfThreads();
138 
139  // These methods are excluded from wrapping 1) because the
140  // wrapper gives up on them and 2) because they really shouldn't be
141  // called from a script anyway.
142 
147  void SingleMethodExecute();
148 
154  void MultipleMethodExecute();
155 
163  void SetSingleMethod(vtkThreadFunctionType, void *data );
164 
169  void SetMultipleMethod( int index, vtkThreadFunctionType, void *data );
170 
176  int SpawnThread( vtkThreadFunctionType, void *data );
177 
181  void TerminateThread( int thread_id );
182 
186  vtkTypeBool IsThreadActive( int threadID );
187 
191  static vtkMultiThreaderIDType GetCurrentThreadID();
192 
196  static vtkTypeBool ThreadsEqual(vtkMultiThreaderIDType t1,
198 
199 protected:
201  ~vtkMultiThreader() override;
202 
203  // The number of threads to use
205 
206  // An array of thread info containing a thread id
207  // (0, 1, 2, .. VTK_MAX_THREADS-1), the thread count, and a pointer
208  // to void so that user data can be passed to each thread
209  ThreadInfo ThreadInfoArray[VTK_MAX_THREADS];
210 
211  // The methods
213  vtkThreadFunctionType MultipleMethod[VTK_MAX_THREADS];
214 
215  // Storage of MutexFunctions and ints used to control spawned
216  // threads and the spawned thread ids
217  int SpawnedThreadActiveFlag[VTK_MAX_THREADS];
218  vtkMutexLock *SpawnedThreadActiveFlagLock[VTK_MAX_THREADS];
219  vtkThreadProcessIDType SpawnedThreadProcessID[VTK_MAX_THREADS];
220  ThreadInfo SpawnedThreadInfoArray[VTK_MAX_THREADS];
221 
222  // Internal storage of the data
223  void *SingleData;
224  void *MultipleData[VTK_MAX_THREADS];
225 
226 private:
227  vtkMultiThreader(const vtkMultiThreader&) = delete;
228  void operator=(const vtkMultiThreader&) = delete;
229 };
230 
231 #endif
232 
abstract base class for most VTK objects
Definition: vtkObject.h:53
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:33
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:77
vtkThreadFunctionType SingleMethod