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

vtkMultiThreader.h

Go to the documentation of this file.
00001 /*=========================================================================
00002 
00003   Program:   Visualization Toolkit
00004   Module:    $RCSfile: vtkMultiThreader.h,v $
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 =========================================================================*/
00036 #ifndef __vtkMultiThreader_h
00037 #define __vtkMultiThreader_h
00038 
00039 #include "vtkObject.h"
00040 
00041 #ifdef VTK_USE_SPROC
00042 #include <sys/types.h> // Needed for unix implementation of sproc
00043 #include <unistd.h> // Needed for unix implementation of sproc
00044 #endif
00045 
00046 #if defined(VTK_USE_PTHREADS) || defined(VTK_HP_PTHREADS)
00047 #include <pthread.h> // Needed for PTHREAD implementation of mutex
00048 #include <sys/types.h> // Needed for unix implementation of pthreads
00049 #include <unistd.h> // Needed for unix implementation of pthreads
00050 #endif
00051 
00052 // If VTK_USE_SPROC is defined, then sproc() will be used to create
00053 // multiple threads on an SGI. If VTK_USE_PTHREADS is defined, then
00054 // pthread_create() will be used to create multiple threads (on
00055 // a sun, for example)
00056 
00057 // Defined in vtkSystemIncludes.h:
00058 //   VTK_MAX_THREADS
00059 
00060 // If VTK_USE_PTHREADS is defined, then the multithreaded
00061 // function is of type void *, and returns NULL
00062 // Otherwise the type is void which is correct for WIN32
00063 // and SPROC
00064 //BTX
00065 #ifdef VTK_USE_SPROC
00066 typedef int vtkThreadProcessIDType;
00067 #endif
00068 
00069 // Defined in vtkSystemIncludes.h:
00070 //   VTK_THREAD_RETURN_VALUE
00071 //   VTK_THREAD_RETURN_TYPE
00072 
00073 #ifdef VTK_USE_PTHREADS
00074 typedef void *(*vtkThreadFunctionType)(void *);
00075 typedef pthread_t vtkThreadProcessIDType;
00076 // #define VTK_THREAD_RETURN_VALUE  NULL
00077 // #define VTK_THREAD_RETURN_TYPE   void *
00078 #endif
00079 
00080 #ifdef VTK_USE_WIN32_THREADS
00081 typedef LPTHREAD_START_ROUTINE vtkThreadFunctionType;
00082 typedef HANDLE vtkThreadProcessIDType;
00083 // #define VTK_THREAD_RETURN_VALUE 0
00084 // #define VTK_THREAD_RETURN_TYPE DWORD __stdcall
00085 #endif
00086 
00087 #if !defined(VTK_USE_PTHREADS) && !defined(VTK_USE_WIN32_THREADS)
00088 typedef void (*vtkThreadFunctionType)(void *);
00089 typedef int vtkThreadProcessIDType;
00090 // #define VTK_THREAD_RETURN_VALUE
00091 // #define VTK_THREAD_RETURN_TYPE void
00092 #endif
00093 //ETX
00094 
00095 class vtkMutexLock;
00096 
00097 class VTK_COMMON_EXPORT vtkMultiThreader : public vtkObject 
00098 {
00099 public:
00100   static vtkMultiThreader *New();
00101 
00102   vtkTypeRevisionMacro(vtkMultiThreader,vtkObject);
00103   void PrintSelf(ostream& os, vtkIndent indent);
00104 
00116   //BTX
00117 #define ThreadInfoStruct vtkMultiThreader::ThreadInfo
00118   class ThreadInfo
00119   {
00120   public:
00121     int                 ThreadID;
00122     int                 NumberOfThreads;
00123     int                 *ActiveFlag;
00124     vtkMutexLock        *ActiveFlagLock;
00125     void                *UserData;
00126   };
00127   //ETX
00128 
00130 
00133   vtkSetClampMacro( NumberOfThreads, int, 1, VTK_MAX_THREADS );
00134   virtual int GetNumberOfThreads();
00136 
00138 
00141   static void SetGlobalMaximumNumberOfThreads(int val);
00142   static int  GetGlobalMaximumNumberOfThreads();
00144 
00146 
00149   static void SetGlobalDefaultNumberOfThreads(int val);
00150   static int  GetGlobalDefaultNumberOfThreads();
00152 
00153   // These methods are excluded from Tcl wrapping 1) because the
00154   // wrapper gives up on them and 2) because they really shouldn't be
00155   // called from a script anyway.
00156   //BTX 
00157   
00160   void SingleMethodExecute();
00161 
00165   void MultipleMethodExecute();
00166   
00171   void SetSingleMethod(vtkThreadFunctionType, void *data );
00172  
00175   void SetMultipleMethod( int index, vtkThreadFunctionType, void *data ); 
00176 
00180   int SpawnThread( vtkThreadFunctionType, void *data );
00181 
00183   void TerminateThread( int thread_id );
00184 
00185 
00186 protected:
00187   vtkMultiThreader();
00188   ~vtkMultiThreader();
00189 
00190   // The number of threads to use
00191   int                        NumberOfThreads;
00192 
00193   // An array of thread info containing a thread id
00194   // (0, 1, 2, .. VTK_MAX_THREADS-1), the thread count, and a pointer
00195   // to void so that user data can be passed to each thread
00196   ThreadInfo                 ThreadInfoArray[VTK_MAX_THREADS];
00197 
00198   // The methods
00199   vtkThreadFunctionType      SingleMethod;
00200   vtkThreadFunctionType      MultipleMethod[VTK_MAX_THREADS];
00201 
00202   // Storage of MutexFunctions and ints used to control spawned 
00203   // threads and the spawned thread ids
00204   int                        SpawnedThreadActiveFlag[VTK_MAX_THREADS];
00205   vtkMutexLock               *SpawnedThreadActiveFlagLock[VTK_MAX_THREADS];
00206   vtkThreadProcessIDType     SpawnedThreadProcessID[VTK_MAX_THREADS];
00207   ThreadInfo                 SpawnedThreadInfoArray[VTK_MAX_THREADS];
00208 
00209 //ETX
00210 
00211   // Internal storage of the data
00212   void                       *SingleData;
00213   void                       *MultipleData[VTK_MAX_THREADS];
00214 
00215 private:
00216   vtkMultiThreader(const vtkMultiThreader&);  // Not implemented.
00217   void operator=(const vtkMultiThreader&);  // Not implemented.
00218 };
00219 
00220 #endif
00221 
00222 
00223 
00224 
00225