VTK
dox/Common/Core/vtkMultiThreader.h
Go to the documentation of this file.
00001 /*=========================================================================
00002 
00003   Program:   Visualization Toolkit
00004   Module:    vtkMultiThreader.h
00005 
00006   Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
00007   All rights reserved.
00008   See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
00009 
00010      This software is distributed WITHOUT ANY WARRANTY; without even
00011      the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
00012      PURPOSE.  See the above copyright notice for more information.
00013 
00014 =========================================================================*/
00027 #ifndef __vtkMultiThreader_h
00028 #define __vtkMultiThreader_h
00029 
00030 #include "vtkCommonCoreModule.h" // For export macro
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 typedef int vtkMultiThreaderIDType;
00060 #endif
00061 
00062 // Defined in vtkSystemIncludes.h:
00063 //   VTK_THREAD_RETURN_VALUE
00064 //   VTK_THREAD_RETURN_TYPE
00065 
00066 #ifdef VTK_USE_PTHREADS
00067 typedef void *(*vtkThreadFunctionType)(void *);
00068 typedef pthread_t vtkThreadProcessIDType;
00069 // #define VTK_THREAD_RETURN_VALUE  NULL
00070 // #define VTK_THREAD_RETURN_TYPE   void *
00071 typedef pthread_t vtkMultiThreaderIDType;
00072 #endif
00073 
00074 #ifdef VTK_USE_WIN32_THREADS
00075 typedef vtkWindowsLPTHREAD_START_ROUTINE vtkThreadFunctionType;
00076 typedef vtkWindowsHANDLE vtkThreadProcessIDType;
00077 // #define VTK_THREAD_RETURN_VALUE 0
00078 // #define VTK_THREAD_RETURN_TYPE DWORD __stdcall
00079 typedef vtkWindowsDWORD vtkMultiThreaderIDType;
00080 #endif
00081 
00082 #if !defined(VTK_USE_PTHREADS) && !defined(VTK_USE_WIN32_THREADS)
00083 typedef void (*vtkThreadFunctionType)(void *);
00084 typedef int vtkThreadProcessIDType;
00085 // #define VTK_THREAD_RETURN_VALUE
00086 // #define VTK_THREAD_RETURN_TYPE void
00087 typedef int vtkMultiThreaderIDType;
00088 #endif
00089 //ETX
00090 
00091 class vtkMutexLock;
00092 
00093 class VTKCOMMONCORE_EXPORT vtkMultiThreader : public vtkObject
00094 {
00095 public:
00096   static vtkMultiThreader *New();
00097 
00098   vtkTypeMacro(vtkMultiThreader,vtkObject);
00099   void PrintSelf(ostream& os, vtkIndent indent);
00100 
00112   //BTX
00113 #define ThreadInfoStruct vtkMultiThreader::ThreadInfo
00114   class ThreadInfo
00115   {
00116   public:
00117     int                 ThreadID;
00118     int                 NumberOfThreads;
00119     int                 *ActiveFlag;
00120     vtkMutexLock        *ActiveFlagLock;
00121     void                *UserData;
00122   };
00123   //ETX
00124 
00126 
00129   vtkSetClampMacro( NumberOfThreads, int, 1, VTK_MAX_THREADS );
00130   virtual int GetNumberOfThreads();
00132 
00134 
00137   static void SetGlobalMaximumNumberOfThreads(int val);
00138   static int  GetGlobalMaximumNumberOfThreads();
00140 
00142 
00145   static void SetGlobalDefaultNumberOfThreads(int val);
00146   static int  GetGlobalDefaultNumberOfThreads();
00148 
00149   // These methods are excluded from Tcl wrapping 1) because the
00150   // wrapper gives up on them and 2) because they really shouldn't be
00151   // called from a script anyway.
00152   //BTX
00153 
00156   void SingleMethodExecute();
00157 
00161   void MultipleMethodExecute();
00162 
00167   void SetSingleMethod(vtkThreadFunctionType, void *data );
00168 
00171   void SetMultipleMethod( int index, vtkThreadFunctionType, void *data );
00172 
00176   int SpawnThread( vtkThreadFunctionType, void *data );
00177 
00179   void TerminateThread( int thread_id );
00180 
00182   int IsThreadActive( int threadID );
00183 
00185   static vtkMultiThreaderIDType GetCurrentThreadID();
00186 
00188 
00189   static int ThreadsEqual(vtkMultiThreaderIDType t1,
00190                           vtkMultiThreaderIDType t2);
00192 
00193 protected:
00194   vtkMultiThreader();
00195   ~vtkMultiThreader();
00196 
00197   // The number of threads to use
00198   int                        NumberOfThreads;
00199 
00200   // An array of thread info containing a thread id
00201   // (0, 1, 2, .. VTK_MAX_THREADS-1), the thread count, and a pointer
00202   // to void so that user data can be passed to each thread
00203   ThreadInfo                 ThreadInfoArray[VTK_MAX_THREADS];
00204 
00205   // The methods
00206   vtkThreadFunctionType      SingleMethod;
00207   vtkThreadFunctionType      MultipleMethod[VTK_MAX_THREADS];
00208 
00209   // Storage of MutexFunctions and ints used to control spawned
00210   // threads and the spawned thread ids
00211   int                        SpawnedThreadActiveFlag[VTK_MAX_THREADS];
00212   vtkMutexLock               *SpawnedThreadActiveFlagLock[VTK_MAX_THREADS];
00213   vtkThreadProcessIDType     SpawnedThreadProcessID[VTK_MAX_THREADS];
00214   ThreadInfo                 SpawnedThreadInfoArray[VTK_MAX_THREADS];
00215 
00216 //ETX
00217 
00218   // Internal storage of the data
00219   void                       *SingleData;
00220   void                       *MultipleData[VTK_MAX_THREADS];
00221 
00222 private:
00223   vtkMultiThreader(const vtkMultiThreader&);  // Not implemented.
00224   void operator=(const vtkMultiThreader&);  // Not implemented.
00225 };
00226 
00227 #endif
00228 
00229 
00230 
00231 
00232