/usr/include/ITK-4.9/itkMultiThreader.h is in libinsighttoolkit4-dev 4.9.0-4ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 | /*=========================================================================
*
* Copyright Insight Software Consortium
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0.txt
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*=========================================================================*/
/*=========================================================================
*
* Portions of this file are subject to the VTK Toolkit Version 3 copyright.
*
* Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
*
* For complete copyright, license and disclaimer of warranty information
* please refer to the NOTICE file at the top of the ITK source tree.
*
*=========================================================================*/
#ifndef itkMultiThreader_h
#define itkMultiThreader_h
#include "itkMutexLock.h"
#include "itkThreadSupport.h"
#include "itkIntTypes.h"
#include "itkThreadPool.h"
namespace itk
{
/** \class MultiThreader
* \brief A class for performing multithreaded execution
*
* Multithreader is a class that provides support for multithreaded
* execution using pthread_create on any platform
* supporting POSIX threads. This class can be used to execute a single
* method on multiple threads, or to specify a method per thread.
*
* \ingroup OSSystemObjects
*
* If ITK_USE_PTHREADS is defined, then
* pthread_create() will be used to create multiple threads (on
* a sun, for example).
* \ingroup ITKCommon
*/
class ITKCommon_EXPORT MultiThreader : public Object
{
public:
/** Standard class typedefs. */
typedef MultiThreader Self;
typedef Object Superclass;
typedef SmartPointer<Self> Pointer;
typedef SmartPointer<const Self> ConstPointer;
/** Method for creation through the object factory. */
itkNewMacro(Self);
/** Run-time type information (and related methods). */
itkTypeMacro(MultiThreader, Object);
/** Get/Set the number of threads to create. It will be clamped to the range
* [ 1, m_GlobalMaximumNumberOfThreads ], so the caller of this method should
* check that the requested number of threads was accepted. */
void SetNumberOfThreads(ThreadIdType numberOfThreads);
itkGetConstMacro(NumberOfThreads, ThreadIdType);
/** Set/Get the maximum number of threads to use when multithreading. It
* will be clamped to the range [ 1, ITK_MAX_THREADS ] because several arrays
* are already statically allocated using the ITK_MAX_THREADS number.
* Therefore the caller of this method should check that the requested number
* of threads was accepted. */
static void SetGlobalMaximumNumberOfThreads(ThreadIdType val);
static ThreadIdType GetGlobalMaximumNumberOfThreads();
/** Set/Get whether to use the to use the thread pool
* implementation or the spawing implementation of
* starting threads.
*/
static void SetGlobalDefaultUseThreadPool( const bool GlobalDefaultUseThreadPool );
static bool GetGlobalDefaultUseThreadPool( );
/** Set/Get the value which is used to initialize the NumberOfThreads in the
* constructor. It will be clamped to the range [1, m_GlobalMaximumNumberOfThreads ].
* Therefore the caller of this method should check that the requested number
* of threads was accepted. */
static void SetGlobalDefaultNumberOfThreads(ThreadIdType val);
static ThreadIdType GetGlobalDefaultNumberOfThreads();
/** Execute the SingleMethod (as define by SetSingleMethod) using
* m_NumberOfThreads threads. As a side effect the m_NumberOfThreads will be
* checked against the current m_GlobalMaximumNumberOfThreads and clamped if
* necessary. */
void SingleMethodExecute();
/** Execute the MultipleMethods (as define by calling SetMultipleMethod for
* each of the required m_NumberOfThreads methods) using m_NumberOfThreads
* threads. As a side effect the m_NumberOfThreads will be checked against the
* current m_GlobalMaximumNumberOfThreads and clamped if necessary. */
void MultipleMethodExecute();
/** Set the SingleMethod to f() and the UserData field of the
* ThreadInfoStruct that is passed to it will be data.
* This method (and all the methods passed to SetMultipleMethod)
* must be of type itkThreadFunctionType and must take a single argument of
* type void *. */
void SetSingleMethod(ThreadFunctionType, void *data);
/** Set the MultipleMethod at the given index to f() and the UserData
* field of the ThreadInfoStruct that is passed to it will be data. */
void SetMultipleMethod(ThreadIdType index, ThreadFunctionType, void *data);
/** Create a new thread for the given function. Return a thread id
* which is a number between 0 and ITK_MAX_THREADS - 1. This
* id should be used to kill the thread at a later time. */
ThreadIdType SpawnThread(ThreadFunctionType, void *data);
/** Terminate the thread that was created with a SpawnThreadExecute() */
void TerminateThread(ThreadIdType thread_id);
/** Set the ThreadPool used by this MultiThreader. If not set,
* the default ThreadPool will be used. Currently ThreadPool
* is only used in SingleMethodExecute. */
itkSetObjectMacro(ThreadPool, ThreadPool);
/** Get the ThreadPool used by this MultiThreader */
itkGetModifiableObjectMacro(ThreadPool, ThreadPool);
/** Set the flag to use a threadpool instead of spawning individual
* threads
*/
itkSetMacro(UseThreadPool,bool);
/** Get the UseThreadPool flag*/
itkGetMacro(UseThreadPool,bool);
/** This is the structure that is passed to the thread that is
* created from the SingleMethodExecute, MultipleMethodExecute or
* the SpawnThread method. It is passed in as a void *, and it is up
* to the method to cast correctly and extract the information. The
* ThreadID is a number between 0 and NumberOfThreads-1 that
* indicates the id of this thread. The NumberOfThreads is
* this->NumberOfThreads for threads created from
* SingleMethodExecute or MultipleMethodExecute, and it is 1 for
* threads created from SpawnThread. The UserData is the (void
* *)arg passed into the SetSingleMethod, SetMultipleMethod, or
* SpawnThread method. */
#ifdef ThreadInfoStruct
#undef ThreadInfoStruct
#endif
struct ThreadInfoStruct
{
ThreadIdType ThreadID;
ThreadIdType NumberOfThreads;
int *ActiveFlag;
MutexLock::Pointer ActiveFlagLock;
void *UserData;
ThreadFunctionType ThreadFunction;
enum { SUCCESS, ITK_EXCEPTION, ITK_PROCESS_ABORTED_EXCEPTION, STD_EXCEPTION, UNKNOWN } ThreadExitCode;
};
protected:
MultiThreader();
~MultiThreader();
virtual void PrintSelf(std::ostream & os, Indent indent) const ITK_OVERRIDE;
private:
MultiThreader(const Self &) ITK_DELETE_FUNCTION;
void operator=(const Self &) ITK_DELETE_FUNCTION;
// Thread pool instance and factory
ThreadPool::Pointer m_ThreadPool;
// choose whether to use Spawn or ThreadPool methods
bool m_UseThreadPool;
/** An array of thread info containing a thread id
* (0, 1, 2, .. ITK_MAX_THREADS-1), the thread count, and a pointer
* to void so that user data can be passed to each thread. */
ThreadInfoStruct m_ThreadInfoArray[ITK_MAX_THREADS];
/** The methods to invoke. */
ThreadFunctionType m_SingleMethod;
ThreadFunctionType m_MultipleMethod[ITK_MAX_THREADS];
/** Storage of MutexFunctions and ints used to control spawned
* threads and the spawned thread ids. */
int m_SpawnedThreadActiveFlag[ITK_MAX_THREADS];
MutexLock::Pointer m_SpawnedThreadActiveFlagLock[ITK_MAX_THREADS];
ThreadProcessIdType m_SpawnedThreadProcessID[ITK_MAX_THREADS];
ThreadInfoStruct m_SpawnedThreadInfoArray[ITK_MAX_THREADS];
/** Internal storage of the data. */
void *m_SingleData;
void *m_MultipleData[ITK_MAX_THREADS];
/** Global variable defining the maximum number of threads that can be used.
* The m_GlobalMaximumNumberOfThreads must always be less than or equal to
* ITK_MAX_THREADS and greater than zero. */
static ThreadIdType m_GlobalMaximumNumberOfThreads;
/** Global value to effect weather the threadpool implementation should
* be used. This defaults to the environmental variable "ITK_USE_THREADPOOL"
* if set, else it default to false and new threads are spawned.
*/
static bool m_GlobalDefaultUseThreadPool;
/* Global variable defining the default number of threads to set at
* construction time of a MultiThreader instance. The
* m_GlobalDefaultNumberOfThreads must always be less than or equal to the
* m_GlobalMaximumNumberOfThreads and larger or equal to 1 once it has been
* initialized in the constructor of the first MultiThreader instantiation.
*/
static ThreadIdType m_GlobalDefaultNumberOfThreads;
/** Platform specific number of threads */
static ThreadIdType GetGlobalDefaultNumberOfThreadsByPlatform();
/** The number of threads to use.
* The m_NumberOfThreads must always be less than or equal to
* the m_GlobalMaximumNumberOfThreads before it is used during the execution
* of a threaded method. Its value is clamped in the SingleMethodExecute()
* and MultipleMethodExecute(). Its value is initialized to
* m_GlobalDefaultNumberOfThreads at construction time. Its value is clamped
* to the current m_GlobalMaximumNumberOfThreads in the
* SingleMethodExecute() and MultipleMethodExecute() methods.
*/
ThreadIdType m_NumberOfThreads;
/** Static function used as a "proxy callback" by the MultiThreader. The
* threading library will call this routine for each thread, which
* will delegate the control to the prescribed SingleMethod. This
* routine acts as an intermediary between the MultiThreader and the
* user supplied callback (SingleMethod) in order to catch any
* exceptions thrown by the threads. */
static ITK_THREAD_RETURN_TYPE SingleMethodProxy(void *arg);
/** Assign work to a thread in the thread pool */
ThreadProcessIdType ThreadPoolDispatchSingleMethodThread(ThreadInfoStruct *);
/** wait for a thread in the threadpool to finish work */
void ThreadPoolWaitForSingleMethodThread(ThreadProcessIdType);
/** spawn a new thread for the SingleMethod */
ThreadProcessIdType SpawnDispatchSingleMethodThread(ThreadInfoStruct *);
/** wait for a thread in the threadpool to finish work */
void SpawnWaitForSingleMethodThread(ThreadProcessIdType);
/** Spawn a thread for the prescribed SingleMethod. This routine
* spawns a thread to the SingleMethodProxy which runs the
* prescribed SingleMethod. The SingleMethodProxy allows for
* exceptions within a thread to be naively handled. A similar
* abstraction needs to be added for MultipleMethod and
* SpawnThread. */
ThreadProcessIdType DispatchSingleMethodThread(ThreadInfoStruct *);
/** Wait for a thread running the prescribed SingleMethod. A similar
* abstraction needs to be added for MultipleMethod (SpawnThread
* already has a routine to do this. */
void WaitForSingleMethodThread(ThreadProcessIdType);
/** Friends of Multithreader.
* ProcessObject is a friend so that it can call PrintSelf() on its
* Multithreader. */
friend class ProcessObject;
};
} // end namespace itk
#endif
|