ITK  4.1.0
Insight Segmentation and Registration Toolkit
itkMultiThreader.h
Go to the documentation of this file.
00001 /*=========================================================================
00002  *
00003  *  Copyright Insight Software Consortium
00004  *
00005  *  Licensed under the Apache License, Version 2.0 (the "License");
00006  *  you may not use this file except in compliance with the License.
00007  *  You may obtain a copy of the License at
00008  *
00009  *         http://www.apache.org/licenses/LICENSE-2.0.txt
00010  *
00011  *  Unless required by applicable law or agreed to in writing, software
00012  *  distributed under the License is distributed on an "AS IS" BASIS,
00013  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00014  *  See the License for the specific language governing permissions and
00015  *  limitations under the License.
00016  *
00017  *=========================================================================*/
00018 /*=========================================================================
00019  *
00020  *  Portions of this file are subject to the VTK Toolkit Version 3 copyright.
00021  *
00022  *  Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
00023  *
00024  *  For complete copyright, license and disclaimer of warranty information
00025  *  please refer to the NOTICE file at the top of the ITK source tree.
00026  *
00027  *=========================================================================*/
00028 #ifndef __itkMultiThreader_h
00029 #define __itkMultiThreader_h
00030 
00031 #include "itkMutexLock.h"
00032 #include "itkThreadSupport.h"
00033 #include "itkIntTypes.h"
00034 
00035 namespace itk
00036 {
00053 class ITKCommon_EXPORT MultiThreader:public Object
00054 {
00055 public:
00057   typedef MultiThreader              Self;
00058   typedef Object                     Superclass;
00059   typedef SmartPointer< Self >       Pointer;
00060   typedef SmartPointer< const Self > ConstPointer;
00061 
00063   itkNewMacro(Self);
00064 
00066   itkTypeMacro(MultiThreader, Object);
00067 
00071   void SetNumberOfThreads(ThreadIdType numberOfThreads);
00072 
00073   itkGetConstMacro(NumberOfThreads, ThreadIdType);
00074 
00080   static void SetGlobalMaximumNumberOfThreads(ThreadIdType val);
00081 
00082   static ThreadIdType  GetGlobalMaximumNumberOfThreads();
00083 
00088   static void SetGlobalDefaultNumberOfThreads(ThreadIdType val);
00089 
00090   static ThreadIdType  GetGlobalDefaultNumberOfThreads();
00091 
00096   void SingleMethodExecute();
00097 
00102   void MultipleMethodExecute();
00103 
00109   void SetSingleMethod(ThreadFunctionType, void *data);
00110 
00113   void SetMultipleMethod(ThreadIdType index, ThreadFunctionType, void *data);
00114 
00118   int SpawnThread(ThreadFunctionType, void *data);
00119 
00121   void TerminateThread(ThreadIdType thread_id);
00122 
00134 #ifdef ThreadInfoStruct
00135 #undef ThreadInfoStruct
00136 #endif
00137   struct ThreadInfoStruct {
00138     ThreadIdType ThreadID;
00139     ThreadIdType NumberOfThreads;
00140     int *ActiveFlag;
00141     MutexLock::Pointer ActiveFlagLock;
00142     void *UserData;
00143     ThreadFunctionType ThreadFunction;
00144     enum { SUCCESS, ITK_EXCEPTION, ITK_PROCESS_ABORTED_EXCEPTION, STD_EXCEPTION, UNKNOWN } ThreadExitCode;
00145   };
00146 protected:
00147   MultiThreader();
00148   ~MultiThreader();
00149   void PrintSelf(std::ostream & os, Indent indent) const;
00151 
00152 private:
00153   MultiThreader(const Self &);  //purposely not implemented
00154   void operator=(const Self &); //purposely not implemented
00155 
00159   ThreadInfoStruct m_ThreadInfoArray[ITK_MAX_THREADS];
00160 
00162   ThreadFunctionType m_SingleMethod;
00163   ThreadFunctionType m_MultipleMethod[ITK_MAX_THREADS];
00164 
00167   int                 m_SpawnedThreadActiveFlag[ITK_MAX_THREADS];
00168   MutexLock::Pointer  m_SpawnedThreadActiveFlagLock[ITK_MAX_THREADS];
00169   ThreadProcessIDType m_SpawnedThreadProcessID[ITK_MAX_THREADS];
00170   ThreadInfoStruct    m_SpawnedThreadInfoArray[ITK_MAX_THREADS];
00171 
00173   void *m_SingleData;
00174   void *m_MultipleData[ITK_MAX_THREADS];
00175 
00179   static ThreadIdType m_GlobalMaximumNumberOfThreads;
00180 
00181   /*  Global variable defining the default number of threads to set at
00182    *  construction time of a MultiThreader instance.  The
00183    *  m_GlobalDefaultNumberOfThreads must always be less than or equal to the
00184    *  m_GlobalMaximumNumberOfThreads and larger or equal to 1 once it has been
00185    *  initialized in the constructor of the first MultiThreader instantiation.
00186    */
00187   static ThreadIdType m_GlobalDefaultNumberOfThreads;
00188 
00190   static ThreadIdType  GetGlobalDefaultNumberOfThreadsByPlatform();
00191 
00201   ThreadIdType m_NumberOfThreads;
00202 
00209   static ITK_THREAD_RETURN_TYPE SingleMethodProxy(void *arg);
00210 
00217   ThreadProcessIDType DispatchSingleMethodThread(ThreadInfoStruct *);
00218 
00222   void WaitForSingleMethodThread(ThreadProcessIDType);
00223 
00224