Main Page | Class Hierarchy | Alphabetical List | Class List | Directories | 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 =========================================================================*/
00024 #ifndef __vtkMultiThreader_h
00025 #define __vtkMultiThreader_h
00026 
00027 #include "vtkObject.h"
00028 
00029 #ifdef VTK_USE_SPROC
00030 #include <sys/types.h> // Needed for unix implementation of sproc
00031 #include <unistd.h> // Needed for unix implementation of sproc
00032 #endif
00033 
00034 #if defined(VTK_USE_PTHREADS) || defined(VTK_HP_PTHREADS)
00035 #include <pthread.h> // Needed for PTHREAD implementation of mutex
00036 #include <sys/types.h> // Needed for unix implementation of pthreads
00037 #include <unistd.h> // Needed for unix implementation of pthreads
00038 #endif
00039 
00040 // If VTK_USE_SPROC is defined, then sproc() will be used to create
00041 // multiple threads on an SGI. If VTK_USE_PTHREADS is defined, then
00042 // pthread_create() will be used to create multiple threads (on
00043 // a sun, for example)
00044 
00045 // Defined in vtkSystemIncludes.h:
00046 //   VTK_MAX_THREADS
00047 
00048 // If VTK_USE_PTHREADS is defined, then the multithreaded
00049 // function is of type void *, and returns NULL
00050 // Otherwise the type is void which is correct for WIN32
00051 // and SPROC
00052 //BTX
00053 #ifdef VTK_USE_SPROC
00054 typedef int vtkThreadProcessIDType;
00055 typedef int vtkMultiThreaderIDType;
00056 #endif
00057 
00058 // Defined in vtkSystemIncludes.h:
00059 //   VTK_THREAD_RETURN_VALUE
00060 //   VTK_THREAD_RETURN_TYPE
00061 
00062 #ifdef VTK_USE_PTHREADS
00063 typedef void *(*vtkThreadFunctionType)(void *);
00064 typedef pthread_t vtkThreadProcessIDType;
00065 // #define VTK_THREAD_RETURN_VALUE  NULL
00066 // #define VTK_THREAD_RETURN_TYPE   void *
00067 typedef pthread_t vtkMultiThreaderIDType;
00068 #endif
00069 
00070 #ifdef VTK_USE_WIN32_THREADS
00071 typedef vtkWindowsLPTHREAD_START_ROUTINE vtkThreadFunctionType;
00072 typedef vtkWindowsHANDLE vtkThreadProcessIDType;
00073 // #define VTK_THREAD_RETURN_VALUE 0
00074 // #define VTK_THREAD_RETURN_TYPE DWORD __stdcall
00075 typedef vtkWindowsDWORD vtkMultiThreaderIDType;
00076 #endif
00077 
00078 #if !defined(VTK_USE_PTHREADS) && !defined(VTK_USE_WIN32_THREADS)
00079 typedef void (*vtkThreadFunctionType)(void *);
00080 typedef int vtkThreadProcessIDType;
00081 // #define VTK_THREAD_RETURN_VALUE
00082 // #define VTK_THREAD_RETURN_TYPE void
00083 typedef int vtkMultiThreaderIDType;
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   virtual int GetNumberOfThreads();
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 
00178   static vtkMultiThreaderIDType GetCurrentThreadID();
00179 
00181 
00182   static int ThreadsEqual(vtkMultiThreaderIDType t1,
00183                           vtkMultiThreaderIDType t2);
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 

Generated on Mon Jan 21 23:07:17 2008 for VTK by  doxygen 1.4.3-20050530