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: 2005/08/22 20:23:36 $
00007   Version:   $Revision: 1.28 $
00008 
00009   Copyright (c) Insight Software 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 #include <sys/resource.h>
00029 #include <sys/types.h>
00030 #include <sys/prctl.h>
00031 #include <wait.h>
00032 #include <errno.h>
00033 #include <ulocks.h>
00034 #include <fcntl.h>
00035 #include <unistd.h>
00036 #include <stdlib.h>
00037 #include <signal.h>
00038 #include <sys/signal.h>
00039 #include <sys/sysmp.h>
00040 #include <sys/errno.h>
00041 #include <sys/syssgi.h>
00042 
00043 extern "C" {
00044 #include <sys/pmo.h>
00045 #include <fetchop.h>
00046 }
00047 #endif
00048 
00049 #ifdef ITK_USE_PTHREADS
00050 #include <pthread.h>
00051 #endif
00052 
00053 namespace itk
00054 {
00071 // The maximum number of threads allowed
00072 #ifdef ITK_USE_SPROC
00073 #define ITK_MAX_THREADS              128
00074 #endif
00075 
00076 #ifdef ITK_USE_PTHREADS
00077 #define ITK_MAX_THREADS              128
00078 #endif
00079 
00080 #ifdef ITK_USE_WIN32_THREADS
00081 #define ITK_MAX_THREADS              128
00082 #endif
00083 
00084 // cygwin threads are unreliable
00085 #ifdef __CYGWIN__
00086 #undef ITK_MAX_THREADS
00087 #define ITK_MAX_THREADS 128 
00088 #endif
00089 
00090 // mingw threads cause crashes  so limit to 1
00091 #if defined(__MINGW32__)
00092 #undef ITK_MAX_THREADS
00093 #define ITK_MAX_THREADS 1 
00094 #endif
00095   
00096 // On some sgi machines, threads and stl don't mix so limit to 1
00097 #if defined(__sgi) && defined(_COMPILER_VERSION) && _COMPILER_VERSION <= 730
00098 #undef ITK_MAX_THREADS
00099 #define ITK_MAX_THREADS 1 
00100 #endif
00101   
00102 #ifndef ITK_MAX_THREADS
00103 #define ITK_MAX_THREADS 1
00104 #endif
00105 
00111 #ifdef ITK_USE_SPROC
00112 typedef int ThreadProcessIDType;
00113 #endif
00114 
00115 #ifdef ITK_USE_PTHREADS
00116 typedef void *(*ThreadFunctionType)(void *);
00117 typedef pthread_t ThreadProcessIDType;
00118 #define ITK_THREAD_RETURN_VALUE  NULL
00119 #define ITK_THREAD_RETURN_TYPE   void *
00120 #endif
00121 
00122 #ifdef ITK_USE_WIN32_THREADS
00123 typedef LPTHREAD_START_ROUTINE ThreadFunctionType;
00124 typedef HANDLE ThreadProcessIDType;
00125 #define ITK_THREAD_RETURN_VALUE 0
00126 #define ITK_THREAD_RETURN_TYPE DWORD __stdcall
00127 #endif
00128 
00129 #ifndef ITK_THREAD_RETURN_VALUE
00130 typedef void (*ThreadFunctionType)(void *);
00131 typedef int ThreadProcessIDType;
00132 #define ITK_THREAD_RETURN_VALUE
00133 #define ITK_THREAD_RETURN_TYPE void
00134 #endif
00135   
00136 class ITKCommon_EXPORT MultiThreader : public Object 
00137 {
00138 public:
00140   typedef MultiThreader         Self;
00141   typedef Object  Superclass;
00142   typedef SmartPointer<Self>  Pointer;
00143   typedef SmartPointer<const Self>  ConstPointer;
00144   
00146   itkNewMacro(Self);  
00147 
00149   itkTypeMacro(MultiThreader, Object);
00150 
00154   itkSetClampMacro( NumberOfThreads, int, 1, ITK_MAX_THREADS );
00155   itkGetMacro( NumberOfThreads, int );
00156     
00160   static void SetGlobalMaximumNumberOfThreads(int val);
00161   static int  GetGlobalMaximumNumberOfThreads();
00162   
00166   static void SetGlobalDefaultNumberOfThreads(int val);
00167   static int  GetGlobalDefaultNumberOfThreads();
00168   
00171   void SingleMethodExecute();
00172 
00176   void MultipleMethodExecute();
00177   
00183   void SetSingleMethod(ThreadFunctionType, void *data );
00184   
00187   void SetMultipleMethod( int index, ThreadFunctionType, void *data ); 
00188 
00192   int SpawnThread( ThreadFunctionType, void *data );
00193 
00195   void TerminateThread( int thread_id );
00196   
00197 #ifdef ITK_USE_SPROC
00198   static bool GetInitialized()
00199   { return m_Initialized; }
00200   static usptr_t * GetThreadArena()
00201   { return m_ThreadArena; }
00202 
00203   static void Initialize();
00204 #endif
00205   
00217 #ifdef ThreadInfoStruct
00218 #undef ThreadInfoStruct
00219 #endif
00220   struct ThreadInfoStruct
00221   {
00222 #ifdef ITK_USE_SPROC
00223     char Pad1[128];
00224 #endif
00225     int                 ThreadID;
00226     int                 NumberOfThreads;
00227     int                 *ActiveFlag;
00228     MutexLock::Pointer  ActiveFlagLock;
00229     void                *UserData;
00230     ThreadFunctionType  ThreadFunction;
00231     enum {SUCCESS, ITK_EXCEPTION, ITK_PROCESS_ABORTED_EXCEPTION, STD_EXCEPTION, UNKNOWN} ThreadExitCode;
00232 #ifdef ITK_USE_SPROC
00233     char Pad2[128];
00234 #endif
00235   };
00236   
00237 protected:
00238   MultiThreader();
00239   ~MultiThreader();
00240   void PrintSelf(std::ostream& os, Indent indent) const;
00241 
00242 private:
00243   
00244 #ifdef ITK_USE_SPROC
00245   static bool m_Initialized;
00246   static usptr_t * m_ThreadArena;
00247   static int m_DevzeroFd;
00248 #endif
00249 
00250   MultiThreader(const Self&); //purposely not implemented
00251   void operator=(const Self&); //purposely not implemented
00252 
00254   int                        m_NumberOfThreads;
00255 
00259   ThreadInfoStruct           m_ThreadInfoArray[ITK_MAX_THREADS];
00260 
00262   ThreadFunctionType         m_SingleMethod;
00263   ThreadFunctionType         m_MultipleMethod[ITK_MAX_THREADS];
00264   
00267   int                        m_SpawnedThreadActiveFlag    [ITK_MAX_THREADS];
00268   MutexLock::Pointer         m_SpawnedThreadActiveFlagLock[ITK_MAX_THREADS];
00269   ThreadProcessIDType        m_SpawnedThreadProcessID     [ITK_MAX_THREADS];
00270   ThreadInfoStruct           m_SpawnedThreadInfoArray     [ITK_MAX_THREADS];
00271   
00273   void                       *m_SingleData;
00274   void                       *m_MultipleData[ITK_MAX_THREADS];
00275 
00277   static int                  m_GlobalMaximumNumberOfThreads;
00278   static int                  m_GlobalDefaultNumberOfThreads;
00279   
00286   static ITK_THREAD_RETURN_TYPE SingleMethodProxy( void *arg );
00287 
00294   ThreadProcessIDType DispatchSingleMethodThread(ThreadInfoStruct *);
00295 
00299   void WaitForSingleMethodThread(ThreadProcessIDType);
00300 
00301   
00305   friend class ProcessObject;
00306 }; 
00307  
00308 }  // end namespace itk
00309 #endif

Generated at Wed May 24 23:42:13 2006 for ITK by doxygen 1.3.5 written by Dimitri van Heesch, © 1997-2000