Orfeo Toolbox  3.16
itkMultiThreader.h
Go to the documentation of this file.
1 /*=========================================================================
2 
3  Program: Insight Segmentation & Registration Toolkit
4  Module: $RCSfile: itkMultiThreader.h,v $
5  Language: C++
6  Date: $Date: 2009-11-22 13:34:19 $
7  Version: $Revision: 1.34 $
8 
9  Copyright (c) Insight Software Consortium. All rights reserved.
10  See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm for details.
11 
12  Portions of this code are covered under the VTK copyright.
13  See VTKCopyright.txt or http://www.kitware.com/VTKCopyright.htm for details.
14 
15  This software is distributed WITHOUT ANY WARRANTY; without even
16  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
17  PURPOSE. See the above copyright notices for more information.
18 
19 =========================================================================*/
20 #ifndef __itkMultiThreader_h
21 #define __itkMultiThreader_h
22 
23 #include "itkObject.h"
24 #include "itkMutexLock.h"
25 
26 #ifdef ITK_USE_SPROC
27 #include <sys/types.h>
28 #include <sys/resource.h>
29 #include <sys/types.h>
30 #include <sys/prctl.h>
31 #include <wait.h>
32 #include <errno.h>
33 #include <ulocks.h>
34 #include <fcntl.h>
35 #include <unistd.h>
36 #include <stdlib.h>
37 #include <signal.h>
38 #include <sys/signal.h>
39 #include <sys/sysmp.h>
40 #include <sys/errno.h>
41 #include <sys/syssgi.h>
42 
43 extern "C" {
44 #include <sys/pmo.h>
45 #include <fetchop.h>
46 }
47 #endif
48 
49 #ifdef ITK_USE_PTHREADS
50 #include <pthread.h>
51 #endif
52 
53 namespace itk
54 {
72 // The maximum number of threads allowed
73 #ifdef ITK_USE_SPROC
74 #define ITK_MAX_THREADS 128
75 #endif
76 
77 #ifdef ITK_USE_PTHREADS
78 #define ITK_MAX_THREADS 128
79 #endif
80 
81 #ifdef ITK_USE_WIN32_THREADS
82 #define ITK_MAX_THREADS 128
83 #endif
84 
85 // cygwin threads are unreliable
86 #ifdef __CYGWIN__
87 #undef ITK_MAX_THREADS
88 #define ITK_MAX_THREADS 128
89 #endif
90 
91 // at one point, mingw threads caused crashes so limit was set to 1
92 // as of July 2009, all tests are passing with threads enabled
93 #if defined(__MINGW32__)
94 #undef ITK_MAX_THREADS
95 #define ITK_MAX_THREADS 128
96 #endif
97 
98 // On some sgi machines, threads and stl don't mix so limit to 1
99 #if defined(__sgi) && defined(_COMPILER_VERSION) && _COMPILER_VERSION <= 730
100 #undef ITK_MAX_THREADS
101 #define ITK_MAX_THREADS 1
102 #endif
103 
104 #ifndef ITK_MAX_THREADS
105 #define ITK_MAX_THREADS 1
106 #endif
107 
113 #ifdef ITK_USE_SPROC
114 typedef int ThreadProcessIDType;
115 #endif
116 
117 #ifdef ITK_USE_PTHREADS
118 typedef void *(*ThreadFunctionType)(void *);
119 typedef pthread_t ThreadProcessIDType;
120 #define ITK_THREAD_RETURN_VALUE NULL
121 #define ITK_THREAD_RETURN_TYPE void *
122 #endif
123 
124 #ifdef ITK_USE_WIN32_THREADS
125 typedef LPTHREAD_START_ROUTINE ThreadFunctionType;
126 typedef HANDLE ThreadProcessIDType;
127 #define ITK_THREAD_RETURN_VALUE 0
128 #define ITK_THREAD_RETURN_TYPE DWORD __stdcall
129 #endif
130 
131 #ifndef ITK_THREAD_RETURN_VALUE
132 typedef void (*ThreadFunctionType)(void *);
134 #define ITK_THREAD_RETURN_VALUE
135 #define ITK_THREAD_RETURN_TYPE void
136 #endif
137 
139 {
140 public:
146 
148  itkNewMacro(Self);
149 
151  itkTypeMacro(MultiThreader, Object);
152 
156  void SetNumberOfThreads( int numberOfThreads );
157  itkGetConstMacro( NumberOfThreads, int );
158 
164  static void SetGlobalMaximumNumberOfThreads(int val);
165  static int GetGlobalMaximumNumberOfThreads();
166 
171  static void SetGlobalDefaultNumberOfThreads(int val);
172  static int GetGlobalDefaultNumberOfThreads();
173 
178  void SingleMethodExecute();
179 
184  void MultipleMethodExecute();
185 
191  void SetSingleMethod(ThreadFunctionType, void *data );
192 
195  void SetMultipleMethod( int index, ThreadFunctionType, void *data );
196 
200  // FIXME: Doesn't seem to be called anywhere...
201  int SpawnThread( ThreadFunctionType, void *data );
202 
204  void TerminateThread( int thread_id );
205 
206 #ifdef ITK_USE_SPROC
207  static bool GetInitialized()
208  { return m_Initialized; }
209  static usptr_t * GetThreadArena()
210  { return m_ThreadArena; }
211 
212  static void Initialize();
213 #endif
214 
226 #ifdef ThreadInfoStruct
227 #undef ThreadInfoStruct
228 #endif
230  {
231 #ifdef ITK_USE_SPROC
232  char Pad1[128];
233 #endif
234  int ThreadID;
238  void *UserData;
240  enum {SUCCESS, ITK_EXCEPTION, ITK_PROCESS_ABORTED_EXCEPTION, STD_EXCEPTION, UNKNOWN} ThreadExitCode;
241 #ifdef ITK_USE_SPROC
242  char Pad2[128];
243 #endif
244  };
245 
246 protected:
247  MultiThreader();
248  ~MultiThreader();
249  void PrintSelf(std::ostream& os, Indent indent) const;
250 
251 private:
252 
253 #ifdef ITK_USE_SPROC
254  static bool m_Initialized;
255  static usptr_t * m_ThreadArena;
256  static int m_DevzeroFd;
257 #endif
258 
259  MultiThreader(const Self&); //purposely not implemented
260  void operator=(const Self&); //purposely not implemented
261 
265  ThreadInfoStruct m_ThreadInfoArray[ITK_MAX_THREADS];
266 
270 
273  int m_SpawnedThreadActiveFlag [ITK_MAX_THREADS];
274  MutexLock::Pointer m_SpawnedThreadActiveFlagLock[ITK_MAX_THREADS];
275  ThreadProcessIDType m_SpawnedThreadProcessID [ITK_MAX_THREADS];
276  ThreadInfoStruct m_SpawnedThreadInfoArray [ITK_MAX_THREADS];
277 
280  void *m_MultipleData[ITK_MAX_THREADS];
281 
286 
287  /* Global variable defining the default number of threads to set at
288  * construction time of a MultiThreader instance. The
289  * m_GlobalDefaultNumberOfThreads must always be less than or equal to the
290  * m_GlobalMaximumNumberOfThreads and larger or equal to 1 once it has been
291  * initialized in the constructor of the first MultiThreader instantiation.
292  */
294 
305 
306 
313  static ITK_THREAD_RETURN_TYPE SingleMethodProxy( void *arg );
314 
321  ThreadProcessIDType DispatchSingleMethodThread(ThreadInfoStruct *);
322 
326  void WaitForSingleMethodThread(ThreadProcessIDType);
327 
328 
332  friend class ProcessObject;
333 };
334 
335 } // end namespace itk
336 #endif

Generated at Sat Feb 2 2013 23:54:55 for Orfeo Toolbox with doxygen 1.8.1.1