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 #ifdef VTK_USE_SPROC
38 #include <sys/types.h> // Needed for unix implementation of sproc
39 #include <unistd.h> // Needed for unix implementation of sproc
40 #endif
41 
42 #if defined(VTK_USE_PTHREADS) || defined(VTK_HP_PTHREADS)
43 #include <pthread.h> // Needed for PTHREAD implementation of mutex
44 #include <sys/types.h> // Needed for unix implementation of pthreads
45 #include <unistd.h> // Needed for unix implementation of pthreads
46 #endif
47 
48 // If VTK_USE_SPROC is defined, then sproc() will be used to create
49 // multiple threads on an SGI. If VTK_USE_PTHREADS is defined, then
50 // pthread_create() will be used to create multiple threads (on
51 // a sun, for example)
52 
53 // Defined in vtkSystemIncludes.h:
54 // VTK_MAX_THREADS
55 
56 // If VTK_USE_PTHREADS is defined, then the multithreaded
57 // function is of type void *, and returns NULL
58 // Otherwise the type is void which is correct for WIN32
59 // and SPROC
60 
61 #ifdef VTK_USE_SPROC
62 typedef int vtkThreadProcessIDType;
63 typedef int vtkMultiThreaderIDType;
64 #endif
65 
66 // Defined in vtkSystemIncludes.h:
67 // VTK_THREAD_RETURN_VALUE
68 // VTK_THREAD_RETURN_TYPE
69 
70 #ifdef VTK_USE_PTHREADS
71 typedef void *(*vtkThreadFunctionType)(void *);
72 typedef pthread_t vtkThreadProcessIDType;
73 // #define VTK_THREAD_RETURN_VALUE NULL
74 // #define VTK_THREAD_RETURN_TYPE void *
75 typedef pthread_t vtkMultiThreaderIDType;
76 #endif
77 
78 #ifdef VTK_USE_WIN32_THREADS
79 typedef vtkWindowsLPTHREAD_START_ROUTINE vtkThreadFunctionType;
80 typedef vtkWindowsHANDLE vtkThreadProcessIDType;
81 // #define VTK_THREAD_RETURN_VALUE 0
82 // #define VTK_THREAD_RETURN_TYPE DWORD __stdcall
83 typedef vtkWindowsDWORD vtkMultiThreaderIDType;
84 #endif
85 
86 #if !defined(VTK_USE_PTHREADS) && !defined(VTK_USE_WIN32_THREADS)
87 typedef void (*vtkThreadFunctionType)(void *);
89 // #define VTK_THREAD_RETURN_VALUE
90 // #define VTK_THREAD_RETURN_TYPE void
92 #endif
93 
94 class vtkMutexLock;
95 
96 class VTKCOMMONCORE_EXPORT vtkMultiThreader : public vtkObject
97 {
98 public:
99  static vtkMultiThreader *New();
100 
102  void PrintSelf(ostream& os, vtkIndent indent) VTK_OVERRIDE;
103 
117 #define ThreadInfoStruct vtkMultiThreader::ThreadInfo
119  {
120  public:
121  int ThreadID;
125  void *UserData;
126  };
127 
129 
134  vtkSetClampMacro( NumberOfThreads, int, 1, VTK_MAX_THREADS );
135  virtual int GetNumberOfThreads();
137 
139 
144  static void SetGlobalMaximumNumberOfThreads(int val);
145  static int GetGlobalMaximumNumberOfThreads();
147 
149 
154  static void SetGlobalDefaultNumberOfThreads(int val);
155  static int GetGlobalDefaultNumberOfThreads();
157 
158  // These methods are excluded from Tcl wrapping 1) because the
159  // wrapper gives up on them and 2) because they really shouldn't be
160  // called from a script anyway.
161 
166  void SingleMethodExecute();
167 
173  void MultipleMethodExecute();
174 
182  void SetSingleMethod(vtkThreadFunctionType, void *data );
183 
188  void SetMultipleMethod( int index, vtkThreadFunctionType, void *data );
189 
195  int SpawnThread( vtkThreadFunctionType, void *data );
196 
200  void TerminateThread( int thread_id );
201 
205  int IsThreadActive( int threadID );
206 
210  static vtkMultiThreaderIDType GetCurrentThreadID();
211 
215  static int ThreadsEqual(vtkMultiThreaderIDType t1,
217 
218 protected:
220  ~vtkMultiThreader() VTK_OVERRIDE;
221 
222  // The number of threads to use
223  int NumberOfThreads;
224 
225  // An array of thread info containing a thread id
226  // (0, 1, 2, .. VTK_MAX_THREADS-1), the thread count, and a pointer
227  // to void so that user data can be passed to each thread
228  ThreadInfo ThreadInfoArray[VTK_MAX_THREADS];
229 
230  // The methods
231  vtkThreadFunctionType SingleMethod;
232  vtkThreadFunctionType MultipleMethod[VTK_MAX_THREADS];
233 
234  // Storage of MutexFunctions and ints used to control spawned
235  // threads and the spawned thread ids
236  int SpawnedThreadActiveFlag[VTK_MAX_THREADS];
237  vtkMutexLock *SpawnedThreadActiveFlagLock[VTK_MAX_THREADS];
238  vtkThreadProcessIDType SpawnedThreadProcessID[VTK_MAX_THREADS];
239  ThreadInfo SpawnedThreadInfoArray[VTK_MAX_THREADS];
240 
241  // Internal storage of the data
242  void *SingleData;
243  void *MultipleData[VTK_MAX_THREADS];
244 
245 private:
246  vtkMultiThreader(const vtkMultiThreader&) VTK_DELETE_FUNCTION;
247  void operator=(const vtkMultiThreader&) VTK_DELETE_FUNCTION;
248 };
249 
250 #endif
251 
252 
253 
254 
255 
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
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:87