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 =========================================================================*/
27 #ifndef vtkMultiThreader_h
28 #define vtkMultiThreader_h
29 
30 #include "vtkCommonCoreModule.h" // For export macro
31 #include "vtkObject.h"
32 
33 #ifdef VTK_USE_SPROC
34 #include <sys/types.h> // Needed for unix implementation of sproc
35 #include <unistd.h> // Needed for unix implementation of sproc
36 #endif
37 
38 #if defined(VTK_USE_PTHREADS) || defined(VTK_HP_PTHREADS)
39 #include <pthread.h> // Needed for PTHREAD implementation of mutex
40 #include <sys/types.h> // Needed for unix implementation of pthreads
41 #include <unistd.h> // Needed for unix implementation of pthreads
42 #endif
43 
44 // If VTK_USE_SPROC is defined, then sproc() will be used to create
45 // multiple threads on an SGI. If VTK_USE_PTHREADS is defined, then
46 // pthread_create() will be used to create multiple threads (on
47 // a sun, for example)
48 
49 // Defined in vtkSystemIncludes.h:
50 // VTK_MAX_THREADS
51 
52 // If VTK_USE_PTHREADS is defined, then the multithreaded
53 // function is of type void *, and returns NULL
54 // Otherwise the type is void which is correct for WIN32
55 // and SPROC
56 //BTX
57 #ifdef VTK_USE_SPROC
58 typedef int vtkThreadProcessIDType;
59 typedef int vtkMultiThreaderIDType;
60 #endif
61 
62 // Defined in vtkSystemIncludes.h:
63 // VTK_THREAD_RETURN_VALUE
64 // VTK_THREAD_RETURN_TYPE
65 
66 #ifdef VTK_USE_PTHREADS
67 typedef void *(*vtkThreadFunctionType)(void *);
68 typedef pthread_t vtkThreadProcessIDType;
69 // #define VTK_THREAD_RETURN_VALUE NULL
70 // #define VTK_THREAD_RETURN_TYPE void *
71 typedef pthread_t vtkMultiThreaderIDType;
72 #endif
73 
74 #ifdef VTK_USE_WIN32_THREADS
75 typedef vtkWindowsLPTHREAD_START_ROUTINE vtkThreadFunctionType;
76 typedef vtkWindowsHANDLE vtkThreadProcessIDType;
77 // #define VTK_THREAD_RETURN_VALUE 0
78 // #define VTK_THREAD_RETURN_TYPE DWORD __stdcall
79 typedef vtkWindowsDWORD vtkMultiThreaderIDType;
80 #endif
81 
82 #if !defined(VTK_USE_PTHREADS) && !defined(VTK_USE_WIN32_THREADS)
83 typedef void (*vtkThreadFunctionType)(void *);
85 // #define VTK_THREAD_RETURN_VALUE
86 // #define VTK_THREAD_RETURN_TYPE void
88 #endif
89 //ETX
90 
91 class vtkMutexLock;
92 
94 {
95 public:
96  static vtkMultiThreader *New();
97 
98  vtkTypeMacro(vtkMultiThreader,vtkObject);
99  void PrintSelf(ostream& os, vtkIndent indent);
100 
112  //BTX
113 #define ThreadInfoStruct vtkMultiThreader::ThreadInfo
115  {
116  public:
117  int ThreadID;
121  void *UserData;
122  };
123  //ETX
124 
126 
129  vtkSetClampMacro( NumberOfThreads, int, 1, VTK_MAX_THREADS );
130  virtual int GetNumberOfThreads();
132 
134 
137  static void SetGlobalMaximumNumberOfThreads(int val);
138  static int GetGlobalMaximumNumberOfThreads();
140 
142 
145  static void SetGlobalDefaultNumberOfThreads(int val);
146  static int GetGlobalDefaultNumberOfThreads();
148 
149  // These methods are excluded from Tcl wrapping 1) because the
150  // wrapper gives up on them and 2) because they really shouldn't be
151  // called from a script anyway.
152  //BTX
153 
156  void SingleMethodExecute();
157 
161  void MultipleMethodExecute();
162 
167  void SetSingleMethod(vtkThreadFunctionType, void *data );
168 
171  void SetMultipleMethod( int index, vtkThreadFunctionType, void *data );
172 
176  int SpawnThread( vtkThreadFunctionType, void *data );
177 
179  void TerminateThread( int thread_id );
180 
182  int IsThreadActive( int threadID );
183 
185  static vtkMultiThreaderIDType GetCurrentThreadID();
186 
188 
189  static int ThreadsEqual(vtkMultiThreaderIDType t1,
192 
193 protected:
195  ~vtkMultiThreader();
196 
197  // The number of threads to use
199 
200  // An array of thread info containing a thread id
201  // (0, 1, 2, .. VTK_MAX_THREADS-1), the thread count, and a pointer
202  // to void so that user data can be passed to each thread
203  ThreadInfo ThreadInfoArray[VTK_MAX_THREADS];
204 
205  // The methods
208 
209  // Storage of MutexFunctions and ints used to control spawned
210  // threads and the spawned thread ids
211  int SpawnedThreadActiveFlag[VTK_MAX_THREADS];
212  vtkMutexLock *SpawnedThreadActiveFlagLock[VTK_MAX_THREADS];
213  vtkThreadProcessIDType SpawnedThreadProcessID[VTK_MAX_THREADS];
214  ThreadInfo SpawnedThreadInfoArray[VTK_MAX_THREADS];
215 
216 //ETX
217 
218  // Internal storage of the data
219  void *SingleData;
220  void *MultipleData[VTK_MAX_THREADS];
221 
222 private:
223  vtkMultiThreader(const vtkMultiThreader&); // Not implemented.
224  void operator=(const vtkMultiThreader&); // Not implemented.
225 };
226 
227 #endif
228 
229 
230 
231 
232 
abstract base class for most VTK objects
Definition: vtkObject.h:61
void(* vtkThreadFunctionType)(void *)
#define VTKCOMMONCORE_EXPORT
A class for performing multithreaded execution.
#define VTK_MAX_THREADS
Definition: vtkConfigure.h:41
int vtkMultiThreaderIDType
virtual void PrintSelf(ostream &os, vtkIndent indent)
a simple class to control print indentation
Definition: vtkIndent.h:38
int vtkThreadProcessIDType
static vtkObject * New()
mutual exclusion locking class
Definition: vtkMutexLock.h:86
vtkThreadFunctionType SingleMethod