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 
00008 Copyright (c) 1993-2001 Ken Martin, Will Schroeder, Bill Lorensen 
00009 All rights reserved.
00010 
00011 Redistribution and use in source and binary forms, with or without
00012 modification, are permitted provided that the following conditions are met:
00013 
00014  * Redistributions of source code must retain the above copyright notice,
00015    this list of conditions and the following disclaimer.
00016 
00017  * Redistributions in binary form must reproduce the above copyright notice,
00018    this list of conditions and the following disclaimer in the documentation
00019    and/or other materials provided with the distribution.
00020 
00021  * Neither name of Ken Martin, Will Schroeder, or Bill Lorensen nor the names
00022    of any contributors may be used to endorse or promote products derived
00023    from this software without specific prior written permission.
00024 
00025  * Modified source versions must be plainly marked as such, and must not be
00026    misrepresented as being the original software.
00027 
00028 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS''
00029 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00030 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00031 ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE FOR
00032 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00033 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
00034 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00035 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00036 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00037 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00038 
00039 =========================================================================*/
00055 #ifndef __vtkMultiThreader_h
00056 #define __vtkMultiThreader_h
00057 
00058 #include "vtkObject.h"
00059 #include "vtkMutexLock.h"
00060 
00061 #ifdef VTK_USE_SPROC
00062 #include <sys/types.h>
00063 #include <unistd.h>
00064 #endif
00065 
00066 #ifdef VTK_USE_PTHREADS
00067 #include <sys/types.h>
00068 #include <unistd.h>
00069 #endif
00070 
00071 // If VTK_USE_SPROC is defined, then sproc() will be used to create
00072 // multiple threads on an SGI. If VTK_USE_PTHREADS is defined, then
00073 // pthread_create() will be used to create multiple threads (on
00074 // a sun, for example)
00075 
00076 // The maximum number of threads allowed
00077 #ifdef VTK_USE_SPROC
00078 #define VTK_MAX_THREADS              32
00079 #endif
00080 
00081 #ifdef VTK_USE_PTHREADS
00082 #define VTK_MAX_THREADS              32
00083 #endif
00084 
00085 #ifdef _WIN32
00086 #define VTK_MAX_THREADS              8
00087 #endif
00088 
00089 #ifndef _WIN32
00090 #ifndef VTK_USE_SPROC
00091 #ifndef VTK_USE_PTHREADS
00092 #define VTK_MAX_THREADS              1
00093 #endif
00094 #endif
00095 #endif
00096 
00097 // If VTK_USE_PTHREADS is defined, then the multithreaded
00098 // function is of type void *, and returns NULL
00099 // Otherwise the type is void which is correct for WIN32
00100 // and SPROC
00101 //BTX
00102 #ifdef VTK_USE_SPROC
00103 typedef int vtkThreadProcessIDType;
00104 #endif
00105 
00106 #ifdef VTK_USE_PTHREADS
00107 typedef void *(*vtkThreadFunctionType)(void *);
00108 typedef pthread_t vtkThreadProcessIDType;
00109 #define VTK_THREAD_RETURN_VALUE  NULL
00110 #define VTK_THREAD_RETURN_TYPE   void *
00111 #endif
00112 
00113 #ifdef _WIN32
00114 typedef LPTHREAD_START_ROUTINE vtkThreadFunctionType;
00115 typedef HANDLE vtkThreadProcessIDType;
00116 #define VTK_THREAD_RETURN_VALUE 0
00117 #define VTK_THREAD_RETURN_TYPE DWORD __stdcall
00118 #endif
00119 
00120 #ifndef _WIN32
00121 #ifndef VTK_USE_PTHREADS
00122 typedef void (*vtkThreadFunctionType)(void *);
00123 typedef int vtkThreadProcessIDType;
00124 #define VTK_THREAD_RETURN_VALUE
00125 #define VTK_THREAD_RETURN_TYPE void
00126 #endif
00127 #endif
00128 //ETX
00129 
00140 //BTX
00141 struct ThreadInfoStruct
00142 {
00143   int                 ThreadID;
00144   int                 NumberOfThreads;
00145   int                 *ActiveFlag;
00146   vtkMutexLock        *ActiveFlagLock;
00147   void                *UserData;
00148 };
00149 //ETX
00150 
00151 class VTK_EXPORT vtkMultiThreader : public vtkObject 
00152 {
00153 public:
00154   static vtkMultiThreader *New();
00155 
00156   vtkTypeMacro(vtkMultiThreader,vtkObject);
00157   void PrintSelf(ostream& os, vtkIndent indent);
00158 
00162   vtkSetClampMacro( NumberOfThreads, int, 1, VTK_MAX_THREADS );
00163   vtkGetMacro( NumberOfThreads, int );
00164 
00168   static void SetGlobalMaximumNumberOfThreads(int val);
00169   static int  GetGlobalMaximumNumberOfThreads();
00170 
00174   static void SetGlobalDefaultNumberOfThreads(int val);
00175   static int  GetGlobalDefaultNumberOfThreads();
00176 
00177   // These methods are excluded from Tcl wrapping 1) because the
00178   // wrapper gives up on them and 2) because they really shouldn't be
00179   // called from a script anyway.
00180   //BTX 
00181   
00184   void SingleMethodExecute();
00185 
00189   void MultipleMethodExecute();
00190   
00195   void SetSingleMethod(vtkThreadFunctionType, void *data );
00196  
00199   void SetMultipleMethod( int index, vtkThreadFunctionType, void *data ); 
00200 
00204   int SpawnThread( vtkThreadFunctionType, void *data );
00205 
00207   void TerminateThread( int thread_id );
00208 
00209 
00210 protected:
00211   vtkMultiThreader();
00212   ~vtkMultiThreader();
00213   vtkMultiThreader(const vtkMultiThreader&) {};
00214   void operator=(const vtkMultiThreader&) {};
00215 
00216   // The number of threads to use
00217   int                        NumberOfThreads;
00218 
00219   // An array of thread info containing a thread id
00220   // (0, 1, 2, .. VTK_MAX_THREADS-1), the thread count, and a pointer
00221   // to void so that user data can be passed to each thread
00222   ThreadInfoStruct           ThreadInfoArray[VTK_MAX_THREADS];
00223 
00224   // The methods
00225   vtkThreadFunctionType      SingleMethod;
00226   vtkThreadFunctionType      MultipleMethod[VTK_MAX_THREADS];
00227 
00228   // Storage of MutexFunctions and ints used to control spawned 
00229   // threads and the spawned thread ids
00230   int                        SpawnedThreadActiveFlag[VTK_MAX_THREADS];
00231   vtkMutexLock               *SpawnedThreadActiveFlagLock[VTK_MAX_THREADS];
00232   vtkThreadProcessIDType     SpawnedThreadProcessID[VTK_MAX_THREADS];
00233   ThreadInfoStruct           SpawnedThreadInfoArray[VTK_MAX_THREADS];
00234 
00235 //ETX
00236 
00237   // Internal storage of the data
00238   void                       *SingleData;
00239   void                       *MultipleData[VTK_MAX_THREADS];
00240 
00241 };
00242 
00243 #endif
00244 
00245 
00246 
00247 
00248 

Generated on Wed Nov 21 12:26:52 2001 for VTK by doxygen1.2.11.1 written by Dimitri van Heesch, © 1997-2001