Main Page   Groups   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members   Concepts

itkMultiThreader.h

Go to the documentation of this file.
00001 /*=========================================================================
00002 
00003   Program:   Insight Segmentation & Registration Toolkit
00004   Module:    $RCSfile: itkMultiThreader.h,v $
00005   Language:  C++
00006   Date:      $Date: 2002/10/16 12:06:46 $
00007   Version:   $Revision: 1.19 $
00008 
00009   Copyright (c) 2002 Insight Consortium. All rights reserved.
00010   See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm for details.
00011 
00012   Portions of this code are covered under the VTK copyright.
00013   See VTKCopyright.txt or http://www.kitware.com/VTKCopyright.htm for details.
00014 
00015      This software is distributed WITHOUT ANY WARRANTY; without even 
00016      the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
00017      PURPOSE.  See the above copyright notices for more information.
00018 
00019 =========================================================================*/
00020 #ifndef __itkMultiThreader_h
00021 #define __itkMultiThreader_h
00022 
00023 #include "itkObject.h"
00024 #include "itkMutexLock.h"
00025 
00026 #ifdef ITK_USE_SPROC
00027 #include <sys/types.h>
00028 #endif
00029 
00030 #ifdef ITK_USE_PTHREADS
00031 #include <pthread.h>
00032 #endif
00033 
00034 namespace itk
00035 {
00052 // The maximum number of threads allowed
00053 #ifdef ITK_USE_SPROC
00054 #define ITK_MAX_THREADS              32
00055 #endif
00056 
00057 #ifdef ITK_USE_PTHREADS
00058 #define ITK_MAX_THREADS              32
00059 #endif
00060 
00061 #ifdef ITK_USE_WIN32_THREADS
00062 #define ITK_MAX_THREADS              32
00063 #endif
00064 
00065 // cygwin threads are unreliable
00066 #ifdef __CYGWIN__
00067 #undef ITK_MAX_THREADS
00068 #define ITK_MAX_THREADS 1
00069 #endif
00070   
00071 #ifndef ITK_MAX_THREADS
00072 #define ITK_MAX_THREADS              1
00073 #endif
00074 
00080 #ifdef ITK_USE_SPROC
00081 typedef int ThreadProcessIDType;
00082 #endif
00083 
00084 #ifdef ITK_USE_PTHREADS
00085 typedef void *(*ThreadFunctionType)(void *);
00086 typedef pthread_t ThreadProcessIDType;
00087 #define ITK_THREAD_RETURN_VALUE  NULL
00088 #define ITK_THREAD_RETURN_TYPE   void *
00089 #endif
00090 
00091 #ifdef ITK_USE_WIN32_THREADS
00092 typedef LPTHREAD_START_ROUTINE ThreadFunctionType;
00093 typedef HANDLE ThreadProcessIDType;
00094 #define ITK_THREAD_RETURN_VALUE 0
00095 #define ITK_THREAD_RETURN_TYPE DWORD __stdcall
00096 #endif
00097 
00098 #ifndef ITK_THREAD_RETURN_VALUE
00099 typedef void (*ThreadFunctionType)(void *);
00100 typedef int ThreadProcessIDType;
00101 #define ITK_THREAD_RETURN_VALUE
00102 #define ITK_THREAD_RETURN_TYPE void
00103 #endif
00104   
00105 class ITK_EXPORT MultiThreader : public Object 
00106 {
00107 public:
00109   typedef MultiThreader         Self;
00110   typedef Object  Superclass;
00111   typedef SmartPointer<Self>  Pointer;
00112   typedef SmartPointer<const Self>  ConstPointer;
00113   
00115   itkNewMacro(Self);  
00116 
00118   itkTypeMacro(MultiThreader, Object);
00119 
00123   itkSetClampMacro( NumberOfThreads, int, 1, ITK_MAX_THREADS );
00124   itkGetMacro( NumberOfThreads, int );
00125     
00129   static void SetGlobalMaximumNumberOfThreads(int val);
00130   static int  GetGlobalMaximumNumberOfThreads();
00131   
00135   static void SetGlobalDefaultNumberOfThreads(int val);
00136   static int  GetGlobalDefaultNumberOfThreads();
00137   
00140   void SingleMethodExecute();
00141 
00145   void MultipleMethodExecute();
00146   
00152   void SetSingleMethod(ThreadFunctionType, void *data );
00153  
00156   void SetMultipleMethod( int index, ThreadFunctionType, void *data ); 
00157 
00161   int SpawnThread( ThreadFunctionType, void *data );
00162 
00164   void TerminateThread( int thread_id );
00165 
00177   struct ThreadInfoStruct
00178   {
00179     int                 ThreadID;
00180     int                 NumberOfThreads;
00181     int                 *ActiveFlag;
00182     MutexLock::Pointer  ActiveFlagLock;
00183     void                *UserData;
00184   };
00185 
00186 protected:
00187   MultiThreader();
00188   ~MultiThreader();
00189   void PrintSelf(std::ostream& os, Indent indent) const;
00190 
00191 private:
00192   MultiThreader(const Self&); //purposely not implemented
00193   void operator=(const Self&); //purposely not implemented
00194 
00196   int                        m_NumberOfThreads;
00197 
00201   ThreadInfoStruct           m_ThreadInfoArray[ITK_MAX_THREADS];
00202 
00204   ThreadFunctionType         m_SingleMethod;
00205   ThreadFunctionType         m_MultipleMethod[ITK_MAX_THREADS];
00206 
00209   int                        m_SpawnedThreadActiveFlag[ITK_MAX_THREADS];
00210   MutexLock::Pointer         m_SpawnedThreadActiveFlagLock[ITK_MAX_THREADS];
00211   ThreadProcessIDType        m_SpawnedThreadProcessID[ITK_MAX_THREADS];
00212   ThreadInfoStruct           m_SpawnedThreadInfoArray[ITK_MAX_THREADS];
00213 
00215   void                       *m_SingleData;
00216   void                       *m_MultipleData[ITK_MAX_THREADS];
00217 
00219   static int                  m_GlobalMaximumNumberOfThreads;
00220   static int                  m_GlobalDefaultNumberOfThreads;
00221 
00225   friend class ProcessObject;
00226 };
00227 
00228 }  // end namespace itk
00229 #endif
00230 
00231 
00232 
00233 
00234 

Generated at Wed Mar 12 01:13:08 2003 for ITK by doxygen 1.2.15 written by Dimitri van Heesch, © 1997-2000