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 "vtkObject.h"
31 
32 #ifdef VTK_USE_SPROC
33 #include <sys/types.h> // Needed for unix implementation of sproc
34 #include <unistd.h> // Needed for unix implementation of sproc
35 #endif
36 
37 #if defined(VTK_USE_PTHREADS) || defined(VTK_HP_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_SPROC is defined, then sproc() will be used to create
44 // multiple threads on an SGI. If VTK_USE_PTHREADS is defined, then
45 // pthread_create() will be used to create multiple threads (on
46 // a sun, for example)
47 
48 // Defined in vtkSystemIncludes.h:
49 // VTK_MAX_THREADS
50 
51 // If VTK_USE_PTHREADS is defined, then the multithreaded
52 // function is of type void *, and returns NULL
53 // Otherwise the type is void which is correct for WIN32
54 // and SPROC
55 //BTX
56 #ifdef VTK_USE_SPROC
57 typedef int vtkThreadProcessIDType;
58 typedef int vtkMultiThreaderIDType;
59 #endif
60 
61 // Defined in vtkSystemIncludes.h:
62 // VTK_THREAD_RETURN_VALUE
63 // VTK_THREAD_RETURN_TYPE
64 
65 #ifdef VTK_USE_PTHREADS
66 typedef void *(*vtkThreadFunctionType)(void *);
67 typedef pthread_t vtkThreadProcessIDType;
68 // #define VTK_THREAD_RETURN_VALUE NULL
69 // #define VTK_THREAD_RETURN_TYPE void *
70 typedef pthread_t vtkMultiThreaderIDType;
71 #endif
72 
73 #ifdef VTK_USE_WIN32_THREADS
74 typedef vtkWindowsLPTHREAD_START_ROUTINE vtkThreadFunctionType;
75 typedef vtkWindowsHANDLE vtkThreadProcessIDType;
76 // #define VTK_THREAD_RETURN_VALUE 0
77 // #define VTK_THREAD_RETURN_TYPE DWORD __stdcall
78 typedef vtkWindowsDWORD vtkMultiThreaderIDType;
79 #endif
80 
81 #if !defined(VTK_USE_PTHREADS) && !defined(VTK_USE_WIN32_THREADS)
82 typedef void (*vtkThreadFunctionType)(void *);
84 // #define VTK_THREAD_RETURN_VALUE
85 // #define VTK_THREAD_RETURN_TYPE void
87 #endif
88 //ETX
89 
90 class vtkMutexLock;
91 
93 {
94 public:
95  static vtkMultiThreader *New();
96 
97  vtkTypeMacro(vtkMultiThreader,vtkObject);
98  void PrintSelf(ostream& os, vtkIndent indent);
99 
111  //BTX
112 #define ThreadInfoStruct vtkMultiThreader::ThreadInfo
114  {
115  public:
116  int ThreadID;
120  void *UserData;
121  };
122  //ETX
123 
125 
128  vtkSetClampMacro( NumberOfThreads, int, 1, VTK_MAX_THREADS );
129  virtual int GetNumberOfThreads();
131 
133 
136  static void SetGlobalMaximumNumberOfThreads(int val);
137  static int GetGlobalMaximumNumberOfThreads();
139 
141 
144  static void SetGlobalDefaultNumberOfThreads(int val);
145  static int GetGlobalDefaultNumberOfThreads();
147 
148  // These methods are excluded from Tcl wrapping 1) because the
149  // wrapper gives up on them and 2) because they really shouldn't be
150  // called from a script anyway.
151  //BTX
152 
155  void SingleMethodExecute();
156 
160  void MultipleMethodExecute();
161 
166  void SetSingleMethod(vtkThreadFunctionType, void *data );
167 
170  void SetMultipleMethod( int index, vtkThreadFunctionType, void *data );
171 
175  int SpawnThread( vtkThreadFunctionType, void *data );
176 
178  void TerminateThread( int thread_id );
179 
181  int IsThreadActive( int threadID );
182 
184  static vtkMultiThreaderIDType GetCurrentThreadID();
185 
187 
188  static int ThreadsEqual(vtkMultiThreaderIDType t1,
191 
192 protected:
194  ~vtkMultiThreader();
195 
196  // The number of threads to use
198 
199  // An array of thread info containing a thread id
200  // (0, 1, 2, .. VTK_MAX_THREADS-1), the thread count, and a pointer
201  // to void so that user data can be passed to each thread
202  ThreadInfo ThreadInfoArray[VTK_MAX_THREADS];
203 
204  // The methods
206  vtkThreadFunctionType MultipleMethod[VTK_MAX_THREADS];
207 
208  // Storage of MutexFunctions and ints used to control spawned
209  // threads and the spawned thread ids
210  int SpawnedThreadActiveFlag[VTK_MAX_THREADS];
211  vtkMutexLock *SpawnedThreadActiveFlagLock[VTK_MAX_THREADS];
212  vtkThreadProcessIDType SpawnedThreadProcessID[VTK_MAX_THREADS];
213  ThreadInfo SpawnedThreadInfoArray[VTK_MAX_THREADS];
214 
215 //ETX
216 
217  // Internal storage of the data
218  void *SingleData;
219  void *MultipleData[VTK_MAX_THREADS];
220 
221 private:
222  vtkMultiThreader(const vtkMultiThreader&); // Not implemented.
223  void operator=(const vtkMultiThreader&); // Not implemented.
224 };
225 
226 #endif
227 
228 
229 
230 
231 
abstract base class for most VTK objects
Definition: vtkObject.h:60
A class for performing multithreaded execution.
int vtkMultiThreaderIDType
virtual void PrintSelf(ostream &os, vtkIndent indent)
a simple class to control print indentation
Definition: vtkIndent.h:37
int vtkThreadProcessIDType
#define VTK_COMMON_EXPORT
void(* vtkThreadFunctionType)(void *)
static vtkObject * New()
mutual exclusion locking class
Definition: vtkMutexLock.h:81
vtkThreadFunctionType SingleMethod