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: 2009-04-25 12:24:09 $
00007   Version:   $Revision: 1.31 $
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   void SetNumberOfThreads( int numberOfThreads );
00155   itkGetConstMacro( NumberOfThreads, int );
00157 
00163   static void SetGlobalMaximumNumberOfThreads(int val);
00164   static int  GetGlobalMaximumNumberOfThreads();
00166 
00171   static void SetGlobalDefaultNumberOfThreads(int val);
00172   static int  GetGlobalDefaultNumberOfThreads();
00174 
00179   void SingleMethodExecute();
00180 
00185   void MultipleMethodExecute();
00186 
00192   void SetSingleMethod(ThreadFunctionType, void *data );
00193 
00196   void SetMultipleMethod( int index, ThreadFunctionType, void *data ); 
00197 
00201   // FIXME: Doesn't seem to be called anywhere...
00202   int SpawnThread( ThreadFunctionType, void *data );
00203 
00205   void TerminateThread( int thread_id );
00206 
00207 #ifdef ITK_USE_SPROC
00208   static bool GetInitialized()
00209   { return m_Initialized; }
00210   static usptr_t * GetThreadArena()
00211   { return m_ThreadArena; }
00212 
00213   static void Initialize();
00214 #endif
00215   
00227 #ifdef ThreadInfoStruct
00228 #undef ThreadInfoStruct
00229 #endif
00230   struct ThreadInfoStruct
00231     {
00232 #ifdef ITK_USE_SPROC
00233     char Pad1[128];
00234 #endif
00235     int                 ThreadID;
00236     int                 NumberOfThreads;
00237     int                 *ActiveFlag;
00238     MutexLock::Pointer  ActiveFlagLock;
00239     void                *UserData;
00240     ThreadFunctionType  ThreadFunction;
00241     enum {SUCCESS, ITK_EXCEPTION, ITK_PROCESS_ABORTED_EXCEPTION, STD_EXCEPTION, UNKNOWN} ThreadExitCode;
00242 #ifdef ITK_USE_SPROC
00243     char Pad2[128];
00244 #endif
00245     };
00246 
00247 protected:
00248   MultiThreader();
00249   ~MultiThreader();
00250   void PrintSelf(std::ostream& os, Indent indent) const;
00251 
00252 private:
00253   
00254 #ifdef ITK_USE_SPROC
00255   static bool m_Initialized;
00256   static usptr_t * m_ThreadArena;
00257   static int m_DevzeroFd;
00258 #endif
00259 
00260   MultiThreader(const Self&); //purposely not implemented
00261   void operator=(const Self&); //purposely not implemented
00262 
00266   ThreadInfoStruct           m_ThreadInfoArray[ITK_MAX_THREADS];
00267 
00269   ThreadFunctionType         m_SingleMethod;
00270   ThreadFunctionType         m_MultipleMethod[ITK_MAX_THREADS];
00271 
00274   int                        m_SpawnedThreadActiveFlag    [ITK_MAX_THREADS];
00275   MutexLock::Pointer         m_SpawnedThreadActiveFlagLock[ITK_MAX_THREADS];
00276   ThreadProcessIDType        m_SpawnedThreadProcessID     [ITK_MAX_THREADS];
00277   ThreadInfoStruct           m_SpawnedThreadInfoArray     [ITK_MAX_THREADS];
00278 
00280   void                       *m_SingleData;
00281   void                       *m_MultipleData[ITK_MAX_THREADS];
00282 
00286   static int                  m_GlobalMaximumNumberOfThreads;
00287 
00288   /*  Global variable defining the default number of threads to set at
00289    *  construction time of a MultiThreader instance.  The
00290    *  m_GlobalDefaultNumberOfThreads must always be less than or equal to the
00291    *  m_GlobalMaximumNumberOfThreads and larger or equal to 1 once it has been
00292    *  initialized in the constructor of the first MultiThreader instantiation.
00293    */
00294   static int                  m_GlobalDefaultNumberOfThreads;
00295 
00305   int                        m_NumberOfThreads;
00306 
00307   
00314   static ITK_THREAD_RETURN_TYPE SingleMethodProxy( void *arg );
00315 
00322   ThreadProcessIDType DispatchSingleMethodThread(ThreadInfoStruct *);
00323 
00327   void WaitForSingleMethodThread(ThreadProcessIDType);
00328 
00329   
00333   friend class ProcessObject;
00334 }; 
00335 
00336 }  // end namespace itk
00337 #endif
00338 

Generated at Thu May 28 10:53:48 2009 for ITK by doxygen 1.5.5 written by Dimitri van Heesch, © 1997-2000