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 
97  class ThreadInfo
98  {
99  public:
100  int ThreadID;
104  void *UserData;
105  };
106 
108 
113  vtkSetClampMacro( NumberOfThreads, int, 1, VTK_MAX_THREADS );
114  virtual int GetNumberOfThreads();
116 
118 
123  static void SetGlobalMaximumNumberOfThreads(int val);
124  static int GetGlobalMaximumNumberOfThreads();
126 
128 
133  static void SetGlobalDefaultNumberOfThreads(int val);
134  static int GetGlobalDefaultNumberOfThreads();
136 
137  // These methods are excluded from wrapping 1) because the
138  // wrapper gives up on them and 2) because they really shouldn't be
139  // called from a script anyway.
140 
145  void SingleMethodExecute();
146 
152  void MultipleMethodExecute();
153 
161  void SetSingleMethod(vtkThreadFunctionType, void *data );
162 
167  void SetMultipleMethod( int index, vtkThreadFunctionType, void *data );
168 
174  int SpawnThread( vtkThreadFunctionType, void *data );
175 
179  void TerminateThread( int thread_id );
180 
184  vtkTypeBool IsThreadActive( int threadID );
185 
189  static vtkMultiThreaderIDType GetCurrentThreadID();
190 
194  static vtkTypeBool ThreadsEqual(vtkMultiThreaderIDType t1,
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  vtkMutexLock *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 #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.
This is the structure that is passed to the thread that is created from the SingleMethodExecute,...
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