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 );
00157 
00161   static void SetGlobalMaximumNumberOfThreads(int val);
00162   static int  GetGlobalMaximumNumberOfThreads();
00164 
00168   static void SetGlobalDefaultNumberOfThreads(int val);
00169   static int  GetGlobalDefaultNumberOfThreads();
00171 
00174   void SingleMethodExecute();
00175 
00179   void MultipleMethodExecute();
00180 
00186   void SetSingleMethod(ThreadFunctionType, void *data );
00187 
00190   void SetMultipleMethod( int index, ThreadFunctionType, void *data ); 
00191 
00195   int SpawnThread( ThreadFunctionType, void *data );
00196 
00198   void TerminateThread( int thread_id );
00199 
00200 #ifdef ITK_USE_SPROC
00201   static bool GetInitialized()
00202   { return m_Initialized; }
00203   static usptr_t * GetThreadArena()
00204   { return m_ThreadArena; }
00205 
00206   static void Initialize();
00207 #endif
00208   
00220 #ifdef ThreadInfoStruct
00221 #undef ThreadInfoStruct
00222 #endif
00223   struct ThreadInfoStruct
00224   {
00225 #ifdef ITK_USE_SPROC
00226     char Pad1[128];
00227 #endif
00228     int                 ThreadID;
00229     int                 NumberOfThreads;
00230     int                 *ActiveFlag;
00231     MutexLock::Pointer  ActiveFlagLock;
00232     void                *UserData;
00233     ThreadFunctionType  ThreadFunction;
00234     enum {SUCCESS, ITK_EXCEPTION, ITK_PROCESS_ABORTED_EXCEPTION, STD_EXCEPTION, UNKNOWN} ThreadExitCode;
00235 #ifdef ITK_USE_SPROC
00236     char Pad2[128];
00237 #endif
00238   };
00239 
00240 protected:
00241   MultiThreader();
00242   ~MultiThreader();
00243   void PrintSelf(std::ostream& os, Indent indent) const;
00244 
00245 private:
00246   
00247 #ifdef ITK_USE_SPROC
00248   static bool m_Initialized;
00249   static usptr_t * m_ThreadArena;
00250   static int m_DevzeroFd;
00251 #endif
00252 
00253   MultiThreader(const Self&); //purposely not implemented
00254   void operator=(const Self&); //purposely not implemented
00255 
00257   int                        m_NumberOfThreads;
00258 
00262   ThreadInfoStruct           m_ThreadInfoArray[ITK_MAX_THREADS];
00263 
00265   ThreadFunctionType         m_SingleMethod;
00266   ThreadFunctionType         m_MultipleMethod[ITK_MAX_THREADS];
00267 
00270   int                        m_SpawnedThreadActiveFlag    [ITK_MAX_THREADS];
00271   MutexLock::Pointer         m_SpawnedThreadActiveFlagLock[ITK_MAX_THREADS];
00272   ThreadProcessIDType        m_SpawnedThreadProcessID     [ITK_MAX_THREADS];
00273   ThreadInfoStruct           m_SpawnedThreadInfoArray     [ITK_MAX_THREADS];
00274 
00276   void                       *m_SingleData;
00277   void                       *m_MultipleData[ITK_MAX_THREADS];
00278 
00280   static int                  m_GlobalMaximumNumberOfThreads;
00281   static int                  m_GlobalDefaultNumberOfThreads;
00282 
00289   static ITK_THREAD_RETURN_TYPE SingleMethodProxy( void *arg );
00290 
00297   ThreadProcessIDType DispatchSingleMethodThread(ThreadInfoStruct *);
00298 
00302   void WaitForSingleMethodThread(ThreadProcessIDType);
00303 
00304   
00308   friend class ProcessObject;
00309 }; 
00310 
00311 }  // end namespace itk
00312 #endif
00313 

Generated at Wed Nov 5 23:01:45 2008 for ITK by doxygen 1.5.1 written by Dimitri van Heesch, © 1997-2000