itkMultiThreader.h
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
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
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
00086 #ifdef __CYGWIN__
00087 #undef ITK_MAX_THREADS
00088 #define ITK_MAX_THREADS 128
00089 #endif
00090
00091
00092 #if defined(__MINGW32__)
00093 #undef ITK_MAX_THREADS
00094 #define ITK_MAX_THREADS 1
00095 #endif
00096
00097
00098 #if defined(__sgi) && defined(_COMPILER_VERSION) && _COMPILER_VERSION <= 730
00099 #undef ITK_MAX_THREADS
00100 #define ITK_MAX_THREADS 1
00101 #endif
00102
00103 #ifndef ITK_MAX_THREADS
00104 #define ITK_MAX_THREADS 1
00105 #endif
00106
00112 #ifdef ITK_USE_SPROC
00113 typedef int ThreadProcessIDType;
00114 #endif
00115
00116 #ifdef ITK_USE_PTHREADS
00117 typedef void *(*ThreadFunctionType)(void *);
00118 typedef pthread_t ThreadProcessIDType;
00119 #define ITK_THREAD_RETURN_VALUE NULL
00120 #define ITK_THREAD_RETURN_TYPE void *
00121 #endif
00122
00123 #ifdef ITK_USE_WIN32_THREADS
00124 typedef LPTHREAD_START_ROUTINE ThreadFunctionType;
00125 typedef HANDLE ThreadProcessIDType;
00126 #define ITK_THREAD_RETURN_VALUE 0
00127 #define ITK_THREAD_RETURN_TYPE DWORD __stdcall
00128 #endif
00129
00130 #ifndef ITK_THREAD_RETURN_VALUE
00131 typedef void (*ThreadFunctionType)(void *);
00132 typedef int ThreadProcessIDType;
00133 #define ITK_THREAD_RETURN_VALUE
00134 #define ITK_THREAD_RETURN_TYPE void
00135 #endif
00136
00137 class ITKCommon_EXPORT MultiThreader : public Object
00138 {
00139 public:
00141 typedef MultiThreader Self;
00142 typedef Object Superclass;
00143 typedef SmartPointer<Self> Pointer;
00144 typedef SmartPointer<const Self> ConstPointer;
00145
00147 itkNewMacro(Self);
00148
00150 itkTypeMacro(MultiThreader, Object);
00151
00155 void SetNumberOfThreads( int numberOfThreads );
00156 itkGetConstMacro( NumberOfThreads, int );
00158
00164 static void SetGlobalMaximumNumberOfThreads(int val);
00165 static int GetGlobalMaximumNumberOfThreads();
00167
00172 static void SetGlobalDefaultNumberOfThreads(int val);
00173 static int GetGlobalDefaultNumberOfThreads();
00175
00180 void SingleMethodExecute();
00181
00186 void MultipleMethodExecute();
00187
00193 void SetSingleMethod(ThreadFunctionType, void *data );
00194
00197 void SetMultipleMethod( int index, ThreadFunctionType, void *data );
00198
00202
00203 int SpawnThread( ThreadFunctionType, void *data );
00204
00206 void TerminateThread( int thread_id );
00207
00208 #ifdef ITK_USE_SPROC
00209 static bool GetInitialized()
00210 { return m_Initialized; }
00211 static usptr_t * GetThreadArena()
00212 { return m_ThreadArena; }
00213
00214 static void Initialize();
00215 #endif
00216
00228 #ifdef ThreadInfoStruct
00229 #undef ThreadInfoStruct
00230 #endif
00231 struct ThreadInfoStruct
00232 {
00233 #ifdef ITK_USE_SPROC
00234 char Pad1[128];
00235 #endif
00236 int ThreadID;
00237 int NumberOfThreads;
00238 int *ActiveFlag;
00239 MutexLock::Pointer ActiveFlagLock;
00240 void *UserData;
00241 ThreadFunctionType ThreadFunction;
00242 enum {SUCCESS, ITK_EXCEPTION, ITK_PROCESS_ABORTED_EXCEPTION, STD_EXCEPTION, UNKNOWN} ThreadExitCode;
00243 #ifdef ITK_USE_SPROC
00244 char Pad2[128];
00245 #endif
00246 };
00247
00248 protected:
00249 MultiThreader();
00250 ~MultiThreader();
00251 void PrintSelf(std::ostream& os, Indent indent) const;
00252
00253 private:
00254
00255 #ifdef ITK_USE_SPROC
00256 static bool m_Initialized;
00257 static usptr_t * m_ThreadArena;
00258 static int m_DevzeroFd;
00259 #endif
00260
00261 MultiThreader(const Self&);
00262 void operator=(const Self&);
00263
00267 ThreadInfoStruct m_ThreadInfoArray[ITK_MAX_THREADS];
00268
00270 ThreadFunctionType m_SingleMethod;
00271 ThreadFunctionType m_MultipleMethod[ITK_MAX_THREADS];
00272
00275 int m_SpawnedThreadActiveFlag [ITK_MAX_THREADS];
00276 MutexLock::Pointer m_SpawnedThreadActiveFlagLock[ITK_MAX_THREADS];
00277 ThreadProcessIDType m_SpawnedThreadProcessID [ITK_MAX_THREADS];
00278 ThreadInfoStruct m_SpawnedThreadInfoArray [ITK_MAX_THREADS];
00279
00281 void *m_SingleData;
00282 void *m_MultipleData[ITK_MAX_THREADS];
00283
00287 static int m_GlobalMaximumNumberOfThreads;
00288
00289
00290
00291
00292
00293
00294
00295 static int m_GlobalDefaultNumberOfThreads;
00296
00306 int m_NumberOfThreads;
00307
00308
00315 static ITK_THREAD_RETURN_TYPE SingleMethodProxy( void *arg );
00316
00323 ThreadProcessIDType DispatchSingleMethodThread(ThreadInfoStruct *);
00324
00328 void WaitForSingleMethodThread(ThreadProcessIDType);
00329
00330
00334 friend class ProcessObject;
00335 };
00336
00337 }
00338 #endif
00339