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-11-22 13:34:19 $
00007   Version:   $Revision: 1.34 $
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 {
00072 // The maximum number of threads allowed
00073 #ifdef ITK_USE_SPROC
00074 #define ITK_MAX_THREADS              128
00075 #endif
00076 
00077 #ifdef ITK_USE_PTHREADS
00078 #define ITK_MAX_THREADS              128
00079 #endif
00080 
00081 #ifdef ITK_USE_WIN32_THREADS
00082 #define ITK_MAX_THREADS              128
00083 #endif
00084 
00085 // cygwin threads are unreliable
00086 #ifdef __CYGWIN__
00087 #undef ITK_MAX_THREADS
00088 #define ITK_MAX_THREADS 128 
00089 #endif
00090 
00091 // at one point, mingw threads caused crashes so limit was set to 1
00092 // as of July 2009, all tests are passing with threads enabled
00093 #if defined(__MINGW32__)
00094 #undef ITK_MAX_THREADS
00095 #define ITK_MAX_THREADS 128
00096 #endif
00097   
00098 // On some sgi machines, threads and stl don't mix so limit to 1
00099 #if defined(__sgi) && defined(_COMPILER_VERSION) && _COMPILER_VERSION <= 730
00100 #undef ITK_MAX_THREADS
00101 #define ITK_MAX_THREADS 1 
00102 #endif
00103   
00104 #ifndef ITK_MAX_THREADS
00105 #define ITK_MAX_THREADS 1
00106 #endif
00107 
00113 #ifdef ITK_USE_SPROC
00114 typedef int ThreadProcessIDType;
00115 #endif
00116 
00117 #ifdef ITK_USE_PTHREADS
00118 typedef void *(*ThreadFunctionType)(void *);
00119 typedef pthread_t ThreadProcessIDType;
00120 #define ITK_THREAD_RETURN_VALUE  NULL
00121 #define ITK_THREAD_RETURN_TYPE   void *
00122 #endif
00123 
00124 #ifdef ITK_USE_WIN32_THREADS
00125 typedef LPTHREAD_START_ROUTINE ThreadFunctionType;
00126 typedef HANDLE                 ThreadProcessIDType;
00127 #define ITK_THREAD_RETURN_VALUE 0
00128 #define ITK_THREAD_RETURN_TYPE DWORD __stdcall
00129 #endif
00130 
00131 #ifndef ITK_THREAD_RETURN_VALUE
00132 typedef void (*ThreadFunctionType)(void *);
00133 typedef int ThreadProcessIDType;
00134 #define ITK_THREAD_RETURN_VALUE
00135 #define ITK_THREAD_RETURN_TYPE void
00136 #endif
00137   
00138 class ITKCommon_EXPORT MultiThreader : public Object 
00139 {
00140 public:
00142   typedef MultiThreader             Self;
00143   typedef Object                    Superclass;
00144   typedef SmartPointer<Self>        Pointer;
00145   typedef SmartPointer<const Self>  ConstPointer;
00146 
00148   itkNewMacro(Self);  
00149 
00151   itkTypeMacro(MultiThreader, Object);
00152 
00156   void SetNumberOfThreads( int numberOfThreads );
00157   itkGetConstMacro( NumberOfThreads, int );
00159 
00165   static void SetGlobalMaximumNumberOfThreads(int val);
00166   static int  GetGlobalMaximumNumberOfThreads();
00168 
00173   static void SetGlobalDefaultNumberOfThreads(int val);
00174   static int  GetGlobalDefaultNumberOfThreads();
00176 
00181   void SingleMethodExecute();
00182 
00187   void MultipleMethodExecute();
00188 
00194   void SetSingleMethod(ThreadFunctionType, void *data );
00195 
00198   void SetMultipleMethod( int index, ThreadFunctionType, void *data ); 
00199 
00203   // FIXME: Doesn't seem to be called anywhere...
00204   int SpawnThread( ThreadFunctionType, void *data );
00205 
00207   void TerminateThread( int thread_id );
00208 
00209 #ifdef ITK_USE_SPROC
00210   static bool GetInitialized()
00211     { return m_Initialized; }
00212   static usptr_t * GetThreadArena()
00213     { return m_ThreadArena; }
00214 
00215   static void Initialize();
00216 #endif
00217   
00229 #ifdef ThreadInfoStruct
00230 #undef ThreadInfoStruct
00231 #endif
00232   struct ThreadInfoStruct
00233     {
00234 #ifdef ITK_USE_SPROC
00235     char Pad1[128];
00236 #endif
00237     int                 ThreadID;
00238     int                 NumberOfThreads;
00239     int                 *ActiveFlag;
00240     MutexLock::Pointer  ActiveFlagLock;
00241     void                *UserData;
00242     ThreadFunctionType  ThreadFunction;
00243     enum {SUCCESS, ITK_EXCEPTION, ITK_PROCESS_ABORTED_EXCEPTION, STD_EXCEPTION, UNKNOWN} ThreadExitCode;
00244 #ifdef ITK_USE_SPROC
00245     char Pad2[128];
00246 #endif
00247     };
00248 
00249 protected:
00250   MultiThreader();
00251   ~MultiThreader();
00252   void PrintSelf(std::ostream& os, Indent indent) const;
00253 
00254 private:
00255   
00256 #ifdef ITK_USE_SPROC
00257   static bool m_Initialized;
00258   static usptr_t * m_ThreadArena;
00259   static int m_DevzeroFd;
00260 #endif
00261 
00262   MultiThreader(const Self&); //purposely not implemented
00263   void operator=(const Self&); //purposely not implemented
00264 
00268   ThreadInfoStruct           m_ThreadInfoArray[ITK_MAX_THREADS];
00269 
00271   ThreadFunctionType         m_SingleMethod;
00272   ThreadFunctionType         m_MultipleMethod[ITK_MAX_THREADS];
00273 
00276   int                        m_SpawnedThreadActiveFlag    [ITK_MAX_THREADS];
00277   MutexLock::Pointer         m_SpawnedThreadActiveFlagLock[ITK_MAX_THREADS];
00278   ThreadProcessIDType        m_SpawnedThreadProcessID     [ITK_MAX_THREADS];
00279   ThreadInfoStruct           m_SpawnedThreadInfoArray     [ITK_MAX_THREADS];
00280 
00282   void                       *m_SingleData;
00283   void                       *m_MultipleData[ITK_MAX_THREADS];
00284 
00288   static int                  m_GlobalMaximumNumberOfThreads;
00289 
00290   /*  Global variable defining the default number of threads to set at
00291    *  construction time of a MultiThreader instance.  The
00292    *  m_GlobalDefaultNumberOfThreads must always be less than or equal to the
00293    *  m_GlobalMaximumNumberOfThreads and larger or equal to 1 once it has been
00294    *  initialized in the constructor of the first MultiThreader instantiation.
00295    */
00296   static int                  m_GlobalDefaultNumberOfThreads;
00297 
00307   int                        m_NumberOfThreads;
00308 
00309   
00316   static ITK_THREAD_RETURN_TYPE SingleMethodProxy( void *arg );
00317 
00324   ThreadProcessIDType DispatchSingleMethodThread(ThreadInfoStruct *);
00325 
00329   void WaitForSingleMethodThread(ThreadProcessIDType);
00330 
00331   
00335   friend class ProcessObject;
00336 }; 
00337 
00338 }  // end namespace itk
00339 #endif
00340 

Generated at Mon Jul 12 2010 19:14:31 for ITK by doxygen 1.7.1 written by Dimitri van Heesch, © 1997-2000