/usr/include/InsightToolkit/Common/itkMultiThreader.h is in libinsighttoolkit3-dev 3.20.1-1.
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 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 | /*=========================================================================
Program: Insight Segmentation & Registration Toolkit
Module: itkMultiThreader.h
Language: C++
Date: $Date$
Version: $Revision$
Copyright (c) Insight Software Consortium. All rights reserved.
See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm for details.
Portions of this code are covered under the VTK copyright.
See VTKCopyright.txt or http://www.kitware.com/VTKCopyright.htm for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notices for more information.
=========================================================================*/
#ifndef __itkMultiThreader_h
#define __itkMultiThreader_h
#include "itkObject.h"
#include "itkMutexLock.h"
#ifdef ITK_USE_SPROC
#include <sys/types.h>
#include <sys/resource.h>
#include <sys/types.h>
#include <sys/prctl.h>
#include <wait.h>
#include <errno.h>
#include <ulocks.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <signal.h>
#include <sys/signal.h>
#include <sys/sysmp.h>
#include <sys/errno.h>
#include <sys/syssgi.h>
extern "C" {
#include <sys/pmo.h>
#include <fetchop.h>
}
#endif
#ifdef ITK_USE_PTHREADS
#include <pthread.h>
#endif
namespace itk
{
/** \class MultiThreader
* \brief A class for performing multithreaded execution
*
* Multithreader is a class that provides support for multithreaded
* execution using sproc() on an SGI, or 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 OSSystemObejcts
*
* \par Note
* If ITK_USE_SPROC is defined, then sproc() will be used to create
* multiple threads on an SGI. If ITK_USE_PTHREADS is defined, then
* pthread_create() will be used to create multiple threads (on
* a sun, for example).
*/
// The maximum number of threads allowed
#ifdef ITK_USE_SPROC
#define ITK_MAX_THREADS 128
#endif
#ifdef ITK_USE_PTHREADS
#define ITK_MAX_THREADS 128
#endif
#ifdef ITK_USE_WIN32_THREADS
#define ITK_MAX_THREADS 128
#endif
// cygwin threads are unreliable
#ifdef __CYGWIN__
#undef ITK_MAX_THREADS
#define ITK_MAX_THREADS 128
#endif
// at one point, mingw threads caused crashes so limit was set to 1
// as of July 2009, all tests are passing with threads enabled
#if defined(__MINGW32__)
#undef ITK_MAX_THREADS
#define ITK_MAX_THREADS 128
#endif
// On some sgi machines, threads and stl don't mix so limit to 1
#if defined(__sgi) && defined(_COMPILER_VERSION) && _COMPILER_VERSION <= 730
#undef ITK_MAX_THREADS
#define ITK_MAX_THREADS 1
#endif
#ifndef ITK_MAX_THREADS
#define ITK_MAX_THREADS 1
#endif
/** \par Note
* If ITK_USE_PTHREADS is defined, then the multithreaded
* function is of type void *, and returns NULL
* Otherwise the type is void which is correct for WIN32
* and SPROC. */
#ifdef ITK_USE_SPROC
typedef int ThreadProcessIDType;
#endif
#ifdef ITK_USE_PTHREADS
typedef void *(*ThreadFunctionType)(void *);
typedef pthread_t ThreadProcessIDType;
#define ITK_THREAD_RETURN_VALUE NULL
#define ITK_THREAD_RETURN_TYPE void *
#endif
#ifdef ITK_USE_WIN32_THREADS
typedef LPTHREAD_START_ROUTINE ThreadFunctionType;
typedef HANDLE ThreadProcessIDType;
#define ITK_THREAD_RETURN_VALUE 0
#define ITK_THREAD_RETURN_TYPE DWORD __stdcall
#endif
#ifndef ITK_THREAD_RETURN_VALUE
typedef void (*ThreadFunctionType)(void *);
typedef int ThreadProcessIDType;
#define ITK_THREAD_RETURN_VALUE
#define ITK_THREAD_RETURN_TYPE void
#endif
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( int numberOfThreads );
itkGetConstMacro( NumberOfThreads, int );
/** 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(int val);
static int GetGlobalMaximumNumberOfThreads();
/** 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(int val);
static int 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( int 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. */
// FIXME: Doesn't seem to be called anywhere...
int SpawnThread( ThreadFunctionType, void *data );
/** Terminate the thread that was created with a SpawnThreadExecute() */
void TerminateThread( int thread_id );
#ifdef ITK_USE_SPROC
static bool GetInitialized()
{ return m_Initialized; }
static usptr_t * GetThreadArena()
{ return m_ThreadArena; }
static void Initialize();
#endif
/** 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
{
#ifdef ITK_USE_SPROC
char Pad1[128];
#endif
int ThreadID;
int NumberOfThreads;
int *ActiveFlag;
MutexLock::Pointer ActiveFlagLock;
void *UserData;
ThreadFunctionType ThreadFunction;
enum {SUCCESS, ITK_EXCEPTION, ITK_PROCESS_ABORTED_EXCEPTION, STD_EXCEPTION, UNKNOWN} ThreadExitCode;
#ifdef ITK_USE_SPROC
char Pad2[128];
#endif
};
protected:
MultiThreader();
~MultiThreader();
void PrintSelf(std::ostream& os, Indent indent) const;
private:
#ifdef ITK_USE_SPROC
static bool m_Initialized;
static usptr_t * m_ThreadArena;
static int m_DevzeroFd;
#endif
MultiThreader(const Self&); //purposely not implemented
void operator=(const Self&); //purposely not implemented
/** 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 int m_GlobalMaximumNumberOfThreads;
/* 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 int m_GlobalDefaultNumberOfThreads;
/** 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.
*/
int 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 );
/** 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
|