VTK
dox/Common/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 "vtkObject.h"
00031 
00032 #ifdef VTK_USE_SPROC
00033 #include <sys/types.h> // Needed for unix implementation of sproc
00034 #include <unistd.h> // Needed for unix implementation of sproc
00035 #endif
00036 
00037 #if defined(VTK_USE_PTHREADS) || defined(VTK_HP_PTHREADS)
00038 #include <pthread.h> // Needed for PTHREAD implementation of mutex
00039 #include <sys/types.h> // Needed for unix implementation of pthreads
00040 #include <unistd.h> // Needed for unix implementation of pthreads
00041 #endif
00042 
00043 // If VTK_USE_SPROC is defined, then sproc() will be used to create
00044 // multiple threads on an SGI. If VTK_USE_PTHREADS is defined, then
00045 // pthread_create() will be used to create multiple threads (on
00046 // a sun, for example)
00047 
00048 // Defined in vtkSystemIncludes.h:
00049 //   VTK_MAX_THREADS
00050 
00051 // If VTK_USE_PTHREADS is defined, then the multithreaded
00052 // function is of type void *, and returns NULL
00053 // Otherwise the type is void which is correct for WIN32
00054 // and SPROC
00055 //BTX
00056 #ifdef VTK_USE_SPROC
00057 typedef int vtkThreadProcessIDType;
00058 typedef int vtkMultiThreaderIDType;
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 typedef pthread_t vtkMultiThreaderIDType;
00071 #endif
00072 
00073 #ifdef VTK_USE_WIN32_THREADS
00074 typedef vtkWindowsLPTHREAD_START_ROUTINE vtkThreadFunctionType;
00075 typedef vtkWindowsHANDLE vtkThreadProcessIDType;
00076 // #define VTK_THREAD_RETURN_VALUE 0
00077 // #define VTK_THREAD_RETURN_TYPE DWORD __stdcall
00078 typedef vtkWindowsDWORD vtkMultiThreaderIDType;
00079 #endif
00080 
00081 #if !defined(VTK_USE_PTHREADS) && !defined(VTK_USE_WIN32_THREADS)
00082 typedef void (*vtkThreadFunctionType)(void *);
00083 typedef int vtkThreadProcessIDType;
00084 // #define VTK_THREAD_RETURN_VALUE
00085 // #define VTK_THREAD_RETURN_TYPE void
00086 typedef int vtkMultiThreaderIDType;
00087 #endif
00088 //ETX
00089 
00090 class vtkMutexLock;
00091 
00092 class VTK_COMMON_EXPORT vtkMultiThreader : public vtkObject 
00093 {
00094 public:
00095   static vtkMultiThreader *New();
00096 
00097   vtkTypeMacro(vtkMultiThreader,vtkObject);
00098   void PrintSelf(ostream& os, vtkIndent indent);
00099 
00111   //BTX
00112 #define ThreadInfoStruct vtkMultiThreader::ThreadInfo
00113   class ThreadInfo
00114   {
00115   public:
00116     int                 ThreadID;
00117     int                 NumberOfThreads;
00118     int                 *ActiveFlag;
00119     vtkMutexLock        *ActiveFlagLock;
00120     void                *UserData;
00121   };
00122   //ETX
00123 
00125 
00128   vtkSetClampMacro( NumberOfThreads, int, 1, VTK_MAX_THREADS );
00129   virtual int GetNumberOfThreads();
00131 
00133 
00136   static void SetGlobalMaximumNumberOfThreads(int val);
00137   static int  GetGlobalMaximumNumberOfThreads();
00139 
00141 
00144   static void SetGlobalDefaultNumberOfThreads(int val);
00145   static int  GetGlobalDefaultNumberOfThreads();
00147 
00148   // These methods are excluded from Tcl wrapping 1) because the
00149   // wrapper gives up on them and 2) because they really shouldn't be
00150   // called from a script anyway.
00151   //BTX 
00152   
00155   void SingleMethodExecute();
00156 
00160   void MultipleMethodExecute();
00161   
00166   void SetSingleMethod(vtkThreadFunctionType, void *data );
00167  
00170   void SetMultipleMethod( int index, vtkThreadFunctionType, void *data ); 
00171 
00175   int SpawnThread( vtkThreadFunctionType, void *data );
00176 
00178   void TerminateThread( int thread_id );
00179 
00181   int IsThreadActive( int threadID );
00182   
00184   static vtkMultiThreaderIDType GetCurrentThreadID();
00185 
00187 
00188   static int ThreadsEqual(vtkMultiThreaderIDType t1,
00189                           vtkMultiThreaderIDType t2);
00191 
00192 protected:
00193   vtkMultiThreader();
00194   ~vtkMultiThreader();
00195 
00196   // The number of threads to use
00197   int                        NumberOfThreads;
00198 
00199   // An array of thread info containing a thread id
00200   // (0, 1, 2, .. VTK_MAX_THREADS-1), the thread count, and a pointer
00201   // to void so that user data can be passed to each thread
00202   ThreadInfo                 ThreadInfoArray[VTK_MAX_THREADS];
00203 
00204   // The methods
00205   vtkThreadFunctionType      SingleMethod;
00206   vtkThreadFunctionType      MultipleMethod[VTK_MAX_THREADS];
00207 
00208   // Storage of MutexFunctions and ints used to control spawned 
00209   // threads and the spawned thread ids
00210   int                        SpawnedThreadActiveFlag[VTK_MAX_THREADS];
00211   vtkMutexLock               *SpawnedThreadActiveFlagLock[VTK_MAX_THREADS];
00212   vtkThreadProcessIDType     SpawnedThreadProcessID[VTK_MAX_THREADS];
00213   ThreadInfo                 SpawnedThreadInfoArray[VTK_MAX_THREADS];
00214 
00215 //ETX
00216 
00217   // Internal storage of the data
00218   void                       *SingleData;
00219   void                       *MultipleData[VTK_MAX_THREADS];
00220 
00221 private:
00222   vtkMultiThreader(const vtkMultiThreader&);  // Not implemented.
00223   void operator=(const vtkMultiThreader&);  // Not implemented.
00224 };
00225 
00226 #endif
00227 
00228 
00229 
00230 
00231