Main Page | Class Hierarchy | Alphabetical List | Class List | File List | Class Members | File Members | Related Pages

dox/Common/vtkMultiThreader.h

Go to the documentation of this file.
00001 /*========================================================================= 00002 00003 Program: Visualization Toolkit 00004 Module: $RCSfile: vtkMultiThreader.h,v $ 00005 Language: C++ 00006 00007 Copyright (c) 1993-2002 Ken Martin, Will Schroeder, Bill Lorensen 00008 All rights reserved. 00009 See Copyright.txt or http://www.kitware.com/Copyright.htm for details. 00010 00011 This software is distributed WITHOUT ANY WARRANTY; without even 00012 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 00013 PURPOSE. See the above copyright notice for more information. 00014 00015 =========================================================================*/ 00028 #ifndef __vtkMultiThreader_h 00029 #define __vtkMultiThreader_h 00030 00031 #include "vtkObject.h" 00032 00033 #ifdef VTK_USE_SPROC 00034 #include <sys/types.h> // Needed for unix implementation of sproc 00035 #include <unistd.h> // Needed for unix implementation of sproc 00036 #endif 00037 00038 #if defined(VTK_USE_PTHREADS) || defined(VTK_HP_PTHREADS) 00039 #include <pthread.h> // Needed for PTHREAD implementation of mutex 00040 #include <sys/types.h> // Needed for unix implementation of pthreads 00041 #include <unistd.h> // Needed for unix implementation of pthreads 00042 #endif 00043 00044 // If VTK_USE_SPROC is defined, then sproc() will be used to create 00045 // multiple threads on an SGI. If VTK_USE_PTHREADS is defined, then 00046 // pthread_create() will be used to create multiple threads (on 00047 // a sun, for example) 00048 00049 // Defined in vtkSystemIncludes.h: 00050 // VTK_MAX_THREADS 00051 00052 // If VTK_USE_PTHREADS is defined, then the multithreaded 00053 // function is of type void *, and returns NULL 00054 // Otherwise the type is void which is correct for WIN32 00055 // and SPROC 00056 //BTX 00057 #ifdef VTK_USE_SPROC 00058 typedef int vtkThreadProcessIDType; 00059 #endif 00060 00061 // Defined in vtkSystemIncludes.h: 00062 // VTK_THREAD_RETURN_VALUE 00063 // VTK_THREAD_RETURN_TYPE 00064 00065 #ifdef VTK_USE_PTHREADS 00066 typedef void *(*vtkThreadFunctionType)(void *); 00067 typedef pthread_t vtkThreadProcessIDType; 00068 // #define VTK_THREAD_RETURN_VALUE NULL 00069 // #define VTK_THREAD_RETURN_TYPE void * 00070 #endif 00071 00072 #ifdef VTK_USE_WIN32_THREADS 00073 typedef LPTHREAD_START_ROUTINE vtkThreadFunctionType; 00074 typedef HANDLE vtkThreadProcessIDType; 00075 // #define VTK_THREAD_RETURN_VALUE 0 00076 // #define VTK_THREAD_RETURN_TYPE DWORD __stdcall 00077 #endif 00078 00079 #if !defined(VTK_USE_PTHREADS) && !defined(VTK_USE_WIN32_THREADS) 00080 typedef void (*vtkThreadFunctionType)(void *); 00081 typedef int vtkThreadProcessIDType; 00082 // #define VTK_THREAD_RETURN_VALUE 00083 // #define VTK_THREAD_RETURN_TYPE void 00084 #endif 00085 //ETX 00086 00087 class vtkMutexLock; 00088 00089 class VTK_COMMON_EXPORT vtkMultiThreader : public vtkObject 00090 { 00091 public: 00092 static vtkMultiThreader *New(); 00093 00094 vtkTypeRevisionMacro(vtkMultiThreader,vtkObject); 00095 void PrintSelf(ostream& os, vtkIndent indent); 00096 00108 //BTX 00109 #define ThreadInfoStruct vtkMultiThreader::ThreadInfo 00110 class ThreadInfo 00111 { 00112 public: 00113 int ThreadID; 00114 int NumberOfThreads; 00115 int *ActiveFlag; 00116 vtkMutexLock *ActiveFlagLock; 00117 void *UserData; 00118 }; 00119 //ETX 00120 00122 00125 vtkSetClampMacro( NumberOfThreads, int, 1, VTK_MAX_THREADS ); 00126 vtkGetMacro( NumberOfThreads, int ); 00128 00130 00133 static void SetGlobalMaximumNumberOfThreads(int val); 00134 static int GetGlobalMaximumNumberOfThreads(); 00136 00138 00141 static void SetGlobalDefaultNumberOfThreads(int val); 00142 static int GetGlobalDefaultNumberOfThreads(); 00144 00145 // These methods are excluded from Tcl wrapping 1) because the 00146 // wrapper gives up on them and 2) because they really shouldn't be 00147 // called from a script anyway. 00148 //BTX 00149 00152 void SingleMethodExecute(); 00153 00157 void MultipleMethodExecute(); 00158 00163 void SetSingleMethod(vtkThreadFunctionType, void *data ); 00164 00167 void SetMultipleMethod( int index, vtkThreadFunctionType, void *data ); 00168 00172 int SpawnThread( vtkThreadFunctionType, void *data ); 00173 00175 void TerminateThread( int thread_id ); 00176 00177 00178 protected: 00179 vtkMultiThreader(); 00180 ~vtkMultiThreader(); 00181 00182 // The number of threads to use 00183 int NumberOfThreads; 00184 00185 // An array of thread info containing a thread id 00186 // (0, 1, 2, .. VTK_MAX_THREADS-1), the thread count, and a pointer 00187 // to void so that user data can be passed to each thread 00188 ThreadInfo ThreadInfoArray[VTK_MAX_THREADS]; 00189 00190 // The methods 00191 vtkThreadFunctionType SingleMethod; 00192 vtkThreadFunctionType MultipleMethod[VTK_MAX_THREADS]; 00193 00194 // Storage of MutexFunctions and ints used to control spawned 00195 // threads and the spawned thread ids 00196 int SpawnedThreadActiveFlag[VTK_MAX_THREADS]; 00197 vtkMutexLock *SpawnedThreadActiveFlagLock[VTK_MAX_THREADS]; 00198 vtkThreadProcessIDType SpawnedThreadProcessID[VTK_MAX_THREADS]; 00199 ThreadInfo SpawnedThreadInfoArray[VTK_MAX_THREADS]; 00200 00201 //ETX 00202 00203 // Internal storage of the data 00204 void *SingleData; 00205 void *MultipleData[VTK_MAX_THREADS]; 00206 00207 private: 00208 vtkMultiThreader(const vtkMultiThreader&); // Not implemented. 00209 void operator=(const vtkMultiThreader&); // Not implemented. 00210 }; 00211 00212 #endif 00213 00214 00215 00216 00217