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 =========================================================================*/
00052 #ifndef __vtkMultiThreader_h
00053 #define __vtkMultiThreader_h
00054 
00055 #include "vtkObject.h"
00056 #include "vtkMutexLock.h"
00057 
00058 #ifdef VTK_USE_SPROC
00059 #include <sys/types.h>
00060 #include <unistd.h>
00061 #endif
00062 
00063 #ifdef VTK_USE_PTHREADS
00064 #include <sys/types.h>
00065 #include <unistd.h>
00066 #endif
00067 
00068 // If VTK_USE_SPROC is defined, then sproc() will be used to create
00069 // multiple threads on an SGI. If VTK_USE_PTHREADS is defined, then
00070 // pthread_create() will be used to create multiple threads (on
00071 // a sun, for example)
00072 
00073 // The maximum number of threads allowed
00074 #ifdef VTK_USE_SPROC
00075 #define VTK_MAX_THREADS              32
00076 #endif
00077 
00078 #ifdef VTK_USE_PTHREADS
00079 #define VTK_MAX_THREADS              32
00080 #endif
00081 
00082 
00083 #ifdef VTK_USE_WIN32_THREADS
00084 #define VTK_MAX_THREADS              8
00085 #endif
00086 
00087 #ifndef VTK_USE_WIN32_THREADS
00088 #ifndef VTK_USE_SPROC
00089 #ifndef VTK_USE_PTHREADS
00090 #define VTK_MAX_THREADS              1
00091 #endif
00092 #endif
00093 #endif
00094 
00095 // If VTK_USE_PTHREADS is defined, then the multithreaded
00096 // function is of type void *, and returns NULL
00097 // Otherwise the type is void which is correct for WIN32
00098 // and SPROC
00099 //BTX
00100 #ifdef VTK_USE_SPROC
00101 typedef int vtkThreadProcessIDType;
00102 #endif
00103 
00104 #ifdef VTK_USE_PTHREADS
00105 typedef void *(*vtkThreadFunctionType)(void *);
00106 typedef pthread_t vtkThreadProcessIDType;
00107 #define VTK_THREAD_RETURN_VALUE  NULL
00108 #define VTK_THREAD_RETURN_TYPE   void *
00109 #endif
00110 
00111 #ifdef VTK_USE_WIN32_THREADS
00112 typedef LPTHREAD_START_ROUTINE vtkThreadFunctionType;
00113 typedef HANDLE vtkThreadProcessIDType;
00114 #define VTK_THREAD_RETURN_VALUE 0
00115 #define VTK_THREAD_RETURN_TYPE DWORD __stdcall
00116 #endif
00117 
00118 #if !defined(VTK_USE_PTHREADS) && !defined(VTK_USE_WIN32_THREADS)
00119 typedef void (*vtkThreadFunctionType)(void *);
00120 typedef int vtkThreadProcessIDType;
00121 #define VTK_THREAD_RETURN_VALUE
00122 #define VTK_THREAD_RETURN_TYPE void
00123 #endif
00124 //ETX
00125 
00136 //BTX
00137 struct ThreadInfoStruct
00138 {
00139   int                 ThreadID;
00140   int                 NumberOfThreads;
00141   int                 *ActiveFlag;
00142   vtkMutexLock        *ActiveFlagLock;
00143   void                *UserData;
00144 };
00145 //ETX
00146 
00147 class VTK_COMMON_EXPORT vtkMultiThreader : public vtkObject 
00148 {
00149 public:
00150   static vtkMultiThreader *New();
00151 
00152   vtkTypeMacro(vtkMultiThreader,vtkObject);
00153   void PrintSelf(ostream& os, vtkIndent indent);
00154 
00156 
00159   vtkSetClampMacro( NumberOfThreads, int, 1, VTK_MAX_THREADS );
00160   vtkGetMacro( NumberOfThreads, int );
00162 
00164 
00167   static void SetGlobalMaximumNumberOfThreads(int val);
00168   static int  GetGlobalMaximumNumberOfThreads();
00170 
00172 
00175   static void SetGlobalDefaultNumberOfThreads(int val);
00176   static int  GetGlobalDefaultNumberOfThreads();
00178 
00179   // These methods are excluded from Tcl wrapping 1) because the
00180   // wrapper gives up on them and 2) because they really shouldn't be
00181   // called from a script anyway.
00182   //BTX 
00183   
00186   void SingleMethodExecute();
00187 
00191   void MultipleMethodExecute();
00192   
00197   void SetSingleMethod(vtkThreadFunctionType, void *data );
00198  
00201   void SetMultipleMethod( int index, vtkThreadFunctionType, void *data ); 
00202 
00206   int SpawnThread( vtkThreadFunctionType, void *data );
00207 
00209   void TerminateThread( int thread_id );
00210 
00211 
00212 protected:
00213   vtkMultiThreader();
00214   ~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 private:
00242   vtkMultiThreader(const vtkMultiThreader&);  // Not implemented.
00243   void operator=(const vtkMultiThreader&);  // Not implemented.
00244 };
00245 
00246 #endif
00247 
00248 
00249 
00250 
00251 

Generated on Thu Mar 28 14:19:16 2002 for VTK by doxygen1.2.11.1 written by Dimitri van Heesch, © 1997-2001