Main Page Class Hierarchy Alphabetical List Compound List File List Compound 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   Language:  C++
    00006
    00007   Copyright (c) 1993-2002 Ken Martin, Will Schroeder, Bill Lorensen
    00008   All rights reserved.
    00009   See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
    00010
    00011      This software is distributed WITHOUT ANY WARRANTY; without even
    00012      the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
    00013      PURPOSE.  See the above copyright notice for more information.
    00014
    00015 =========================================================================*/
    00040 #ifndef __vtkMultiThreader_h
    00041 #define __vtkMultiThreader_h
    00042
    00043 #include "vtkObject.h"
    00044
    00045 #ifdef VTK_USE_SPROC
    00046 #include <sys/types.h> // Needed for unix implementation of sproc
    00047 #include <unistd.h> // Needed for unix implementation of sproc
    00048 #endif
    00049
    00050 #if defined(VTK_USE_PTHREADS) || defined(VTK_HP_PTHREADS)
    00051 #include <pthread.h> // Needed for PTHREAD implementation of mutex
    00052 #include <sys/types.h> // Needed for unix implementation of pthreads
    00053 #include <unistd.h> // Needed for unix implementation of pthreads
    00054 #endif
    00055
    00056 // If VTK_USE_SPROC is defined, then sproc() will be used to create
    00057 // multiple threads on an SGI. If VTK_USE_PTHREADS is defined, then
    00058 // pthread_create() will be used to create multiple threads (on
    00059 // a sun, for example)
    00060
    00061 // Defined in vtkSystemIncludes.h:
    00062 //   VTK_MAX_THREADS
    00063
    00064 // If VTK_USE_PTHREADS is defined, then the multithreaded
    00065 // function is of type void *, and returns NULL
    00066 // Otherwise the type is void which is correct for WIN32
    00067 // and SPROC
    00068 //BTX
    00069 #ifdef VTK_USE_SPROC
    00070 typedef int vtkThreadProcessIDType;
    00071 #endif
    00072
    00073 // Defined in vtkSystemIncludes.h:
    00074 //   VTK_THREAD_RETURN_VALUE
    00075 //   VTK_THREAD_RETURN_TYPE
    00076
    00077 #ifdef VTK_USE_PTHREADS
    00078 typedef void *(*vtkThreadFunctionType)(void *);
    00079 typedef pthread_t vtkThreadProcessIDType;
    00080 // #define VTK_THREAD_RETURN_VALUE  NULL
    00081 // #define VTK_THREAD_RETURN_TYPE   void *
    00082 #endif
    00083
    00084 #ifdef VTK_USE_WIN32_THREADS
    00085 typedef LPTHREAD_START_ROUTINE vtkThreadFunctionType;
    00086 typedef HANDLE vtkThreadProcessIDType;
    00087 // #define VTK_THREAD_RETURN_VALUE 0
    00088 // #define VTK_THREAD_RETURN_TYPE DWORD __stdcall
    00089 #endif
    00090
    00091 #if !defined(VTK_USE_PTHREADS) && !defined(VTK_USE_WIN32_THREADS)
    00092 typedef void (*vtkThreadFunctionType)(void *);
    00093 typedef int vtkThreadProcessIDType;
    00094 // #define VTK_THREAD_RETURN_VALUE
    00095 // #define VTK_THREAD_RETURN_TYPE void
    00096 #endif
    00097 //ETX
    00098
    00099 class vtkMutexLock;
    00100
    00101 class VTK_COMMON_EXPORT vtkMultiThreader : public vtkObject
    00102 {
    00103 public:
    00104   static vtkMultiThreader *New();
    00105
    00106   vtkTypeRevisionMacro(vtkMultiThreader,vtkObject);
    00107   void PrintSelf(ostream& os, vtkIndent indent);
    00108
    00120   //BTX
    00121 #define ThreadInfoStruct vtkMultiThreader::ThreadInfo
    00122   class ThreadInfo
    00123   {
    00124   public:
    00125     int                 ThreadID;
    00126     int                 NumberOfThreads;
    00127     int                 *ActiveFlag;
    00128     vtkMutexLock        *ActiveFlagLock;
    00129     void                *UserData;
    00130   };
    00131   //ETX
    00132
    00134
    00137   vtkSetClampMacro( NumberOfThreads, int, 1, VTK_MAX_THREADS );
    00138   vtkGetMacro( NumberOfThreads, int );
    00140
    00142
    00145   static void SetGlobalMaximumNumberOfThreads(int val);
    00146   static int  GetGlobalMaximumNumberOfThreads();
    00148
    00150
    00153   static void SetGlobalDefaultNumberOfThreads(int val);
    00154   static int  GetGlobalDefaultNumberOfThreads();
    00156
    00157   // These methods are excluded from Tcl wrapping 1) because the
    00158   // wrapper gives up on them and 2) because they really shouldn't be
    00159   // called from a script anyway.
    00160   //BTX
    00161
    00164   void SingleMethodExecute();
    00165
    00169   void MultipleMethodExecute();
    00170
    00175   void SetSingleMethod(vtkThreadFunctionType, void *data );
    00176
    00179   void SetMultipleMethod( int index, vtkThreadFunctionType, void *data );
    00180
    00184   int SpawnThread( vtkThreadFunctionType, void *data );
    00185
    00187   void TerminateThread( int thread_id );
    00188
    00189
    00190 protected:
    00191   vtkMultiThreader();
    00192   ~vtkMultiThreader();
    00193
    00194   // The number of threads to use
    00195   int                        NumberOfThreads;
    00196
    00197   // An array of thread info containing a thread id
    00198   // (0, 1, 2, .. VTK_MAX_THREADS-1), the thread count, and a pointer
    00199   // to void so that user data can be passed to each thread
    00200   ThreadInfo                 ThreadInfoArray[VTK_MAX_THREADS];
    00201
    00202   // The methods
    00203   vtkThreadFunctionType      SingleMethod;
    00204   vtkThreadFunctionType      MultipleMethod[VTK_MAX_THREADS];
    00205
    00206   // Storage of MutexFunctions and ints used to control spawned
    00207   // threads and the spawned thread ids
    00208   int                        SpawnedThreadActiveFlag[VTK_MAX_THREADS];
    00209   vtkMutexLock               *SpawnedThreadActiveFlagLock[VTK_MAX_THREADS];
    00210   vtkThreadProcessIDType     SpawnedThreadProcessID[VTK_MAX_THREADS];
    00211   ThreadInfo                 SpawnedThreadInfoArray[VTK_MAX_THREADS];
    00212
    00213 //ETX
    00214
    00215   // Internal storage of the data
    00216   void                       *SingleData;
    00217   void                       *MultipleData[VTK_MAX_THREADS];
    00218
    00219 private:
    00220   vtkMultiThreader(const vtkMultiThreader&);  // Not implemented.
    00221   void operator=(const vtkMultiThreader&);  // Not implemented.
    00222 };
    00223
    00224 #endif
    00225
    00226
    00227
    00228
    00229