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  {
105  public:
106  int ThreadID;
110  void *UserData;
111  };
112 
114 
119  vtkSetClampMacro( NumberOfThreads, int, 1, VTK_MAX_THREADS );
120  virtual int GetNumberOfThreads();
122 
124 
129  static void SetGlobalMaximumNumberOfThreads(int val);
130  static int GetGlobalMaximumNumberOfThreads();
132 
134 
139  static void SetGlobalDefaultNumberOfThreads(int val);
140  static int GetGlobalDefaultNumberOfThreads();
142 
143  // These methods are excluded from wrapping 1) because the
144  // wrapper gives up on them and 2) because they really shouldn't be
145  // called from a script anyway.
146 
151  void SingleMethodExecute();
152 
158  void MultipleMethodExecute();
159 
167  void SetSingleMethod(vtkThreadFunctionType, void *data );
168 
173  void SetMultipleMethod( int index, vtkThreadFunctionType, void *data );
174 
180  int SpawnThread( vtkThreadFunctionType, void *data );
181 
185  void TerminateThread( int thread_id );
186 
190  vtkTypeBool IsThreadActive( int threadID );
191 
195  static vtkMultiThreaderIDType GetCurrentThreadID();
196 
200  static vtkTypeBool ThreadsEqual(vtkMultiThreaderIDType t1,
202 
203 protected:
205  ~vtkMultiThreader() override;
206 
207  // The number of threads to use
209 
210  // An array of thread info containing a thread id
211  // (0, 1, 2, .. VTK_MAX_THREADS-1), the thread count, and a pointer
212  // to void so that user data can be passed to each thread
213  ThreadInfo ThreadInfoArray[VTK_MAX_THREADS];
214 
215  // The methods
217  vtkThreadFunctionType MultipleMethod[VTK_MAX_THREADS];
218 
219  // Storage of MutexFunctions and ints used to control spawned
220  // threads and the spawned thread ids
221  int SpawnedThreadActiveFlag[VTK_MAX_THREADS];
222  vtkMutexLock *SpawnedThreadActiveFlagLock[VTK_MAX_THREADS];
223  vtkThreadProcessIDType SpawnedThreadProcessID[VTK_MAX_THREADS];
224  ThreadInfo SpawnedThreadInfoArray[VTK_MAX_THREADS];
225 
226  // Internal storage of the data
227  void *SingleData;
228  void *MultipleData[VTK_MAX_THREADS];
229 
230 private:
231  vtkMultiThreader(const vtkMultiThreader&) = delete;
232  void operator=(const vtkMultiThreader&) = delete;
233 };
234 
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.
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: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