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 #ifdef VTK_USE_SPROC
32 #include <sys/types.h> // Needed for unix implementation of sproc
33 #include <unistd.h> // Needed for unix implementation of sproc
34 #endif
35 
36 #if defined(VTK_USE_PTHREADS) || defined(VTK_HP_PTHREADS)
37 #include <pthread.h> // Needed for PTHREAD implementation of mutex
38 #include <sys/types.h> // Needed for unix implementation of pthreads
39 #include <unistd.h> // Needed for unix implementation of pthreads
40 #endif
41 
42 // If VTK_USE_SPROC is defined, then sproc() will be used to create
43 // multiple threads on an SGI. If VTK_USE_PTHREADS is defined, then
44 // pthread_create() will be used to create multiple threads (on
45 // a sun, for example)
46 
47 // Defined in vtkSystemIncludes.h:
48 // VTK_MAX_THREADS
49 
50 // If VTK_USE_PTHREADS is defined, then the multithreaded
51 // function is of type void *, and returns NULL
52 // Otherwise the type is void which is correct for WIN32
53 // and SPROC
54 
55 #ifdef VTK_USE_SPROC
56 typedef int vtkThreadProcessIDType;
57 typedef int vtkMultiThreaderIDType;
58 #endif
59 
60 // Defined in vtkSystemIncludes.h:
61 // VTK_THREAD_RETURN_VALUE
62 // VTK_THREAD_RETURN_TYPE
63 
64 #ifdef VTK_USE_PTHREADS
65 typedef void *(*vtkThreadFunctionType)(void *);
66 typedef pthread_t vtkThreadProcessIDType;
67 // #define VTK_THREAD_RETURN_VALUE NULL
68 // #define VTK_THREAD_RETURN_TYPE void *
69 typedef pthread_t vtkMultiThreaderIDType;
70 #endif
71 
72 #ifdef VTK_USE_WIN32_THREADS
73 typedef vtkWindowsLPTHREAD_START_ROUTINE vtkThreadFunctionType;
74 typedef vtkWindowsHANDLE vtkThreadProcessIDType;
75 // #define VTK_THREAD_RETURN_VALUE 0
76 // #define VTK_THREAD_RETURN_TYPE DWORD __stdcall
77 typedef vtkWindowsDWORD vtkMultiThreaderIDType;
78 #endif
79 
80 #if !defined(VTK_USE_PTHREADS) && !defined(VTK_USE_WIN32_THREADS)
81 typedef void (*vtkThreadFunctionType)(void *);
83 // #define VTK_THREAD_RETURN_VALUE
84 // #define VTK_THREAD_RETURN_TYPE void
86 #endif
87 
88 class vtkMutexLock;
89 
90 class VTKCOMMONCORE_EXPORT vtkMultiThreader : public vtkObject
91 {
92 public:
93  static vtkMultiThreader *New();
94 
95  vtkTypeMacro(vtkMultiThreader,vtkObject);
96  void PrintSelf(ostream& os, vtkIndent indent) VTK_OVERRIDE;
97 
111 #define ThreadInfoStruct vtkMultiThreader::ThreadInfo
113  {
114  public:
115  int ThreadID;
119  void *UserData;
120  };
121 
123 
128  vtkSetClampMacro( NumberOfThreads, int, 1, VTK_MAX_THREADS );
129  virtual int GetNumberOfThreads();
131 
133 
138  static void SetGlobalMaximumNumberOfThreads(int val);
139  static int GetGlobalMaximumNumberOfThreads();
141 
143 
148  static void SetGlobalDefaultNumberOfThreads(int val);
149  static int GetGlobalDefaultNumberOfThreads();
151 
152  // These methods are excluded from Tcl wrapping 1) because the
153  // wrapper gives up on them and 2) because they really shouldn't be
154  // called from a script anyway.
155 
160  void SingleMethodExecute();
161 
167  void MultipleMethodExecute();
168 
176  void SetSingleMethod(vtkThreadFunctionType, void *data );
177 
182  void SetMultipleMethod( int index, vtkThreadFunctionType, void *data );
183 
189  int SpawnThread( vtkThreadFunctionType, void *data );
190 
194  void TerminateThread( int thread_id );
195 
199  int IsThreadActive( int threadID );
200 
204  static vtkMultiThreaderIDType GetCurrentThreadID();
205 
209  static int ThreadsEqual(vtkMultiThreaderIDType t1,
211 
212 protected:
214  ~vtkMultiThreader() VTK_OVERRIDE;
215 
216  // The number of threads to use
217  int NumberOfThreads;
218 
219  // An array of thread info containing a thread id
220  // (0, 1, 2, .. VTK_MAX_THREADS-1), the thread count, and a pointer
221  // to void so that user data can be passed to each thread
222  ThreadInfo ThreadInfoArray[VTK_MAX_THREADS];
223 
224  // The methods
225  vtkThreadFunctionType SingleMethod;
226  vtkThreadFunctionType MultipleMethod[VTK_MAX_THREADS];
227 
228  // Storage of MutexFunctions and ints used to control spawned
229  // threads and the spawned thread ids
230  int SpawnedThreadActiveFlag[VTK_MAX_THREADS];
231  vtkMutexLock *SpawnedThreadActiveFlagLock[VTK_MAX_THREADS];
232  vtkThreadProcessIDType SpawnedThreadProcessID[VTK_MAX_THREADS];
233  ThreadInfo SpawnedThreadInfoArray[VTK_MAX_THREADS];
234 
235  // Internal storage of the data
236  void *SingleData;
237  void *MultipleData[VTK_MAX_THREADS];
238 
239 private:
240  vtkMultiThreader(const vtkMultiThreader&) VTK_DELETE_FUNCTION;
241  void operator=(const vtkMultiThreader&) VTK_DELETE_FUNCTION;
242 };
243 
244 #endif
245 
246 
247 
248 
249 
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.
A class for performing multithreaded execution.
int vtkMultiThreaderIDType
a simple class to control print indentation
Definition: vtkIndent.h:33
int vtkThreadProcessIDType
void(* vtkThreadFunctionType)(void *)
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:84