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

Common/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 #endif
00056 
00057 // Defined in vtkSystemIncludes.h:
00058 //   VTK_THREAD_RETURN_VALUE
00059 //   VTK_THREAD_RETURN_TYPE
00060 
00061 #ifdef VTK_USE_PTHREADS
00062 typedef void *(*vtkThreadFunctionType)(void *);
00063 typedef pthread_t vtkThreadProcessIDType;
00064 // #define VTK_THREAD_RETURN_VALUE  NULL
00065 // #define VTK_THREAD_RETURN_TYPE   void *
00066 #endif
00067 
00068 #ifdef VTK_USE_WIN32_THREADS
00069 typedef LPTHREAD_START_ROUTINE vtkThreadFunctionType;
00070 typedef HANDLE vtkThreadProcessIDType;
00071 // #define VTK_THREAD_RETURN_VALUE 0
00072 // #define VTK_THREAD_RETURN_TYPE DWORD __stdcall
00073 #endif
00074 
00075 #if !defined(VTK_USE_PTHREADS) && !defined(VTK_USE_WIN32_THREADS)
00076 typedef void (*vtkThreadFunctionType)(void *);
00077 typedef int vtkThreadProcessIDType;
00078 // #define VTK_THREAD_RETURN_VALUE
00079 // #define VTK_THREAD_RETURN_TYPE void
00080 #endif
00081 //ETX
00082 
00083 class vtkMutexLock;
00084 
00085 class VTK_COMMON_EXPORT vtkMultiThreader : public vtkObject 
00086 {
00087 public:
00088   static vtkMultiThreader *New();
00089 
00090   vtkTypeRevisionMacro(vtkMultiThreader,vtkObject);
00091   void PrintSelf(ostream& os, vtkIndent indent);
00092 
00104   //BTX
00105 #define ThreadInfoStruct vtkMultiThreader::ThreadInfo
00106   class ThreadInfo
00107   {
00108   public:
00109     int                 ThreadID;
00110     int                 NumberOfThreads;
00111     int                 *ActiveFlag;
00112     vtkMutexLock        *ActiveFlagLock;
00113     void                *UserData;
00114   };
00115   //ETX
00116 
00118 
00121   vtkSetClampMacro( NumberOfThreads, int, 1, VTK_MAX_THREADS );
00122   vtkGetMacro( NumberOfThreads, int );
00124 
00126 
00129   static void SetGlobalMaximumNumberOfThreads(int val);
00130   static int  GetGlobalMaximumNumberOfThreads();
00132 
00134 
00137   static void SetGlobalDefaultNumberOfThreads(int val);
00138   static int  GetGlobalDefaultNumberOfThreads();
00140 
00141   // These methods are excluded from Tcl wrapping 1) because the
00142   // wrapper gives up on them and 2) because they really shouldn't be
00143   // called from a script anyway.
00144   //BTX 
00145   
00148   void SingleMethodExecute();
00149 
00153   void MultipleMethodExecute();
00154   
00159   void SetSingleMethod(vtkThreadFunctionType, void *data );
00160  
00163   void SetMultipleMethod( int index, vtkThreadFunctionType, void *data ); 
00164 
00168   int SpawnThread( vtkThreadFunctionType, void *data );
00169 
00171   void TerminateThread( int thread_id );
00172 
00173 
00174 protected:
00175   vtkMultiThreader();
00176   ~vtkMultiThreader();
00177 
00178   // The number of threads to use
00179   int                        NumberOfThreads;
00180 
00181   // An array of thread info containing a thread id
00182   // (0, 1, 2, .. VTK_MAX_THREADS-1), the thread count, and a pointer
00183   // to void so that user data can be passed to each thread
00184   ThreadInfo                 ThreadInfoArray[VTK_MAX_THREADS];
00185 
00186   // The methods
00187   vtkThreadFunctionType      SingleMethod;
00188   vtkThreadFunctionType      MultipleMethod[VTK_MAX_THREADS];
00189 
00190   // Storage of MutexFunctions and ints used to control spawned 
00191   // threads and the spawned thread ids
00192   int                        SpawnedThreadActiveFlag[VTK_MAX_THREADS];
00193   vtkMutexLock               *SpawnedThreadActiveFlagLock[VTK_MAX_THREADS];
00194   vtkThreadProcessIDType     SpawnedThreadProcessID[VTK_MAX_THREADS];
00195   ThreadInfo                 SpawnedThreadInfoArray[VTK_MAX_THREADS];
00196 
00197 //ETX
00198 
00199   // Internal storage of the data
00200   void                       *SingleData;
00201   void                       *MultipleData[VTK_MAX_THREADS];
00202 
00203 private:
00204   vtkMultiThreader(const vtkMultiThreader&);  // Not implemented.
00205   void operator=(const vtkMultiThreader&);  // Not implemented.
00206 };
00207 
00208 #endif
00209 
00210 
00211 
00212 
00213