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: 2003/09/10 14:29:17 $
00007   Version:   $Revision: 1.23 $
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 1
00088 #endif
00089 
00090 #ifndef ITK_MAX_THREADS
00091 #define ITK_MAX_THREADS 1
00092 #endif
00093 
00099 #ifdef ITK_USE_SPROC
00100 typedef int ThreadProcessIDType;
00101 #endif
00102 
00103 #ifdef ITK_USE_PTHREADS
00104 typedef void *(*ThreadFunctionType)(void *);
00105 typedef pthread_t ThreadProcessIDType;
00106 #define ITK_THREAD_RETURN_VALUE  NULL
00107 #define ITK_THREAD_RETURN_TYPE   void *
00108 #endif
00109 
00110 #ifdef ITK_USE_WIN32_THREADS
00111 typedef LPTHREAD_START_ROUTINE ThreadFunctionType;
00112 typedef HANDLE ThreadProcessIDType;
00113 #define ITK_THREAD_RETURN_VALUE 0
00114 #define ITK_THREAD_RETURN_TYPE DWORD __stdcall
00115 #endif
00116 
00117 #ifndef ITK_THREAD_RETURN_VALUE
00118 typedef void (*ThreadFunctionType)(void *);
00119 typedef int ThreadProcessIDType;
00120 #define ITK_THREAD_RETURN_VALUE
00121 #define ITK_THREAD_RETURN_TYPE void
00122 #endif
00123   
00124 class ITKCommon_EXPORT MultiThreader : public Object 
00125 {
00126 public:
00128   typedef MultiThreader         Self;
00129   typedef Object  Superclass;
00130   typedef SmartPointer<Self>  Pointer;
00131   typedef SmartPointer<const Self>  ConstPointer;
00132   
00134   itkNewMacro(Self);  
00135 
00137   itkTypeMacro(MultiThreader, Object);
00138 
00142   itkSetClampMacro( NumberOfThreads, int, 1, ITK_MAX_THREADS );
00143   itkGetMacro( NumberOfThreads, int );
00144     
00148   static void SetGlobalMaximumNumberOfThreads(int val);
00149   static int  GetGlobalMaximumNumberOfThreads();
00150   
00154   static void SetGlobalDefaultNumberOfThreads(int val);
00155   static int  GetGlobalDefaultNumberOfThreads();
00156   
00159   void SingleMethodExecute();
00160 
00164   void MultipleMethodExecute();
00165   
00171   void SetSingleMethod(ThreadFunctionType, void *data );
00172   
00175   void SetMultipleMethod( int index, ThreadFunctionType, void *data ); 
00176 
00180   int SpawnThread( ThreadFunctionType, void *data );
00181 
00183   void TerminateThread( int thread_id );
00184   
00185 #ifdef ITK_USE_SPROC
00186   static bool GetInitialized()
00187   { return m_Initialized; }
00188   static usptr_t * GetThreadArena()
00189   { return m_ThreadArena; }
00190 
00191   static void Initialize();
00192 #endif
00193   
00205   struct ThreadInfoStruct
00206   {
00207 #ifdef ITK_USE_SPROC
00208     char Pad1[128];
00209 #endif
00210     int                 ThreadID;
00211     int                 NumberOfThreads;
00212     int                 *ActiveFlag;
00213     MutexLock::Pointer  ActiveFlagLock;
00214     void                *UserData;
00215 #ifdef ITK_USE_SPROC
00216     char Pad2[128];
00217 #endif
00218   };
00219   
00220 protected:
00221   MultiThreader();
00222   ~MultiThreader();
00223   void PrintSelf(std::ostream& os, Indent indent) const;
00224 
00225 private:
00226   
00227 #ifdef ITK_USE_SPROC
00228   static bool m_Initialized;
00229   static usptr_t * m_ThreadArena;
00230   static int m_DevzeroFd;
00231 #endif
00232 
00233   MultiThreader(const Self&); //purposely not implemented
00234   void operator=(const Self&); //purposely not implemented
00235 
00237   int                        m_NumberOfThreads;
00238 
00242   ThreadInfoStruct           m_ThreadInfoArray[ITK_MAX_THREADS];
00243 
00245   ThreadFunctionType         m_SingleMethod;
00246   ThreadFunctionType         m_MultipleMethod[ITK_MAX_THREADS];
00247   
00250   int                        m_SpawnedThreadActiveFlag    [ITK_MAX_THREADS];
00251   MutexLock::Pointer         m_SpawnedThreadActiveFlagLock[ITK_MAX_THREADS];
00252   ThreadProcessIDType        m_SpawnedThreadProcessID     [ITK_MAX_THREADS];
00253   ThreadInfoStruct           m_SpawnedThreadInfoArray     [ITK_MAX_THREADS];
00254   
00256   void                       *m_SingleData;
00257   void                       *m_MultipleData[ITK_MAX_THREADS];
00258 
00260   static int                  m_GlobalMaximumNumberOfThreads;
00261   static int                  m_GlobalDefaultNumberOfThreads;
00262   
00266   friend class ProcessObject;
00267 }; 
00268  
00269 }  // end namespace itk
00270 #endif

Generated at Sun Jan 25 13:19:41 2004 for ITK by doxygen 1.3.3 written by Dimitri van Heesch, © 1997-2000