ITK  4.9.0
Insight Segmentation and Registration Toolkit
itkThreadPool.h
Go to the documentation of this file.
1 /*=========================================================================
2  *
3  * Copyright Insight Software Consortium
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0.txt
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  *=========================================================================*/
18 #ifndef itkThreadPool_h
19 #define itkThreadPool_h
20 
21 #include "itkConfigure.h"
22 #include "itkIntTypes.h"
23 
24 #include "itkThreadSupport.h"
25 
26 #if defined(ITK_USE_PTHREADS)
27 #include <pthread.h>
28 #include <semaphore.h>
29 #include <unistd.h> // for sleep
30 #elif defined(ITK_USE_WIN32_THREADS)
31 #include <windows.h>
32 #endif
33 
34 #if defined __APPLE__
35 #include <mach/mach_init.h>
36 #include <mach/mach_error.h>
37 #include <mach/semaphore.h>
38 #include <mach/task.h>
39 #include <mach/task_info.h>
40 #endif
41 
42 #include <map>
43 #include <set>
44 
45 #include "itkThreadJob.h"
46 #include "itkObject.h"
47 #include "itkObjectFactory.h"
48 #include "itkSimpleFastMutexLock.h"
49 #include "itkMutexLockHolder.h"
50 
51 namespace itk
52 {
53 
72 class ITKCommon_EXPORT ThreadPool : public Object
73 {
74 public:
75 
77  typedef ThreadPool Self;
78  typedef Object Superclass;
81 
83  typedef int ThreadTimeType;
84  typedef unsigned int ThreadCountType;
86 
88  itkTypeMacro(ThreadPool, Object);
89 
91  static Pointer New();
92 
97  static Pointer GetInstance();
98 
100  ThreadProcessIdType AssignWork(ThreadJob worker);
101 
104  void InitializeThreads(ThreadCountType maxThreads);
105 
107  bool WaitForJobOnThreadHandle(ThreadProcessIdType handle);
108 
109 protected:
111  {
112  public:
114  int SemaphoreWait();
115  int SemaphorePost();
116 #if defined(__APPLE__)
117  semaphore_t m_Semaphore;
118 #elif defined(_WIN32) || defined(_WIN64)
119  HANDLE m_Semaphore;
120 #elif defined(ITK_USE_PTHREADS)
121  sem_t m_Semaphore;
122 #endif
123 
125 
126  private:
127  static int m_SemaphoreCount;
128  ThreadSemaphorePair(); //purposefully not implemented.
129  };
130 
131  ThreadPool(); // Protected so that only the GetThreadPool can create a thread
132  // pool
133  virtual ~ThreadPool();
134 
135 private:
136  ThreadPool(ThreadPool const &) ITK_DELETE_FUNCTION;
137 
138  ThreadPool & operator=(ThreadPool const &) ITK_DELETE_FUNCTION;
139 
141  bool m_ScheduleForDestruction;
142 
144  ThreadCountType m_ThreadCount;
145 
147  static SimpleFastMutexLock m_NumberOfPendingJobsToBeRunMutex;
148 
150  unsigned int m_IdCounter;
151 
153  bool m_ExceptionOccurred;
154 
156 
157  typedef std::pair<ThreadJobIdType,ThreadJob> ThreadJobContainerPairType;
158 
159  typedef std::set<ThreadJobIdType> ThreadJobIdsContainerType;
160 
162 
168  ThreadJobContainerType m_WorkerQueue;
169 
171  static SimpleFastMutexLock m_WorkerQueueMutex;
172 
174  ThreadProcessIdContainerType m_ThreadHandles;
175 
178  enum {
179  JOB_THREADHANDLE_JUST_ADDED=-3, //means thread just added
180  JOB_THREADHANDLE_IS_FREE=-2, // means this particular threadhandle
181  // (thread) is free
182  JOB_THREADHANDLE_IS_DONE=-1 // means the thread finished with the
183  // assigned job and is waiting until
184  // WaitForJobOnThreadHandle method is called
185  // otherwise threadhandle is actively
186  // running a job.
187  };
188 
197  {
198 public:
199 #if defined(ITK_USE_WIN32_THREADS)
200  typedef DWORD WinThreadIdType;
201 #else
202  typedef unsigned long WinThreadIdType;
203 #endif
204  ThreadProcessIdentifiers(const int tnid,
205  const ThreadProcessIdType tph,
206  const WinThreadIdType winThreadId) :
207  m_ThreadNumericId(tnid),
208  m_ThreadProcessHandle(tph),
209  m_WinThreadId(winThreadId)
210  {
211  }
213 
217 private:
218  ThreadProcessIdentifiers(); //purposefully not implemented.
219  };
220 
221  // the ThreadProcessIdentifiersVector ThreadSemHandlePairingQueue and
222  // ThreadSemHandlePairingForWaitQueue really want to be STL
223  // containers with more efficient searchability than O(N) But the
224  // pthread_t type isn't guaranteed to be an integral type so it
225  // can't be used as a Key on a keyed STL container.
226  typedef std::vector<ThreadProcessIdentifiers> ThreadProcessIdentifiersVecType;
228 
229  //ThreadSemaphorePair *m_ThreadSemHandlePairingQueue;
230  typedef std::vector<ThreadSemaphorePair *> ThreadSemHandlePairingQueueType;
231 
234 
237 
243  const ThreadJob &FetchWork(ThreadProcessIdType t);
244 
250  void RemoveActiveId(ThreadJobIdType id);
251 
255  void AddThread();
256 
260  ThreadProcessIdentifiers *FindThreadToRun();
261 
265 
267 
268  ThreadSemaphorePair* GetSemaphoreForThread(ThreadProcessIdType threadHandle);
269 
270  ThreadSemaphorePair* GetSemaphoreForThreadWait(ThreadProcessIdType threadHandle);
271 
272  void DeallocateThreadSemSet(ThreadSemHandlePairingQueueType &q);
273 
275  static void * ThreadExecute(void *param);
276 
278  static bool CompareThreadHandles(ThreadProcessIdType t1, ThreadProcessIdType t2);
279 
281  ThreadProcessIdType GetThreadHandleForThreadId(ThreadIdType id);
282 
283 
284 };
285 
286 }
287 #endif
std::pair< ThreadJobIdType, ThreadJob > ThreadJobContainerPairType
Critical section locking class that can be allocated on the stack.
ThreadSemHandlePairingQueueType m_ThreadSemHandlePairingForWaitQueue
ThreadPool Self
Definition: itkThreadPool.h:77
std::vector< ThreadProcessIdentifiers > ThreadProcessIdentifiersVecType
This class is used to submit jobs to the thread pool. The thread job maintains important information ...
Definition: itkThreadJob.h:39
SmartPointer< Self > Pointer
Definition: itkThreadPool.h:79
ThreadJob::JobIdType ThreadJobIdType
Definition: itkThreadPool.h:85
std::map< ThreadJobIdType, ThreadJob > ThreadJobContainerType
static SimpleFastMutexLock m_ThreadProcessIdentifiersVectorMutex
static Pointer m_ThreadPoolInstance
std::set< ThreadJobIdType > ThreadJobIdsContainerType
std::vector< ThreadSemaphorePair * > ThreadSemHandlePairingQueueType
SmartPointer< const Self > ConstPointer
Definition: itkThreadPool.h:80
std::set< ThreadProcessIdType > ThreadProcessIdContainerType
ThreadProcessIdentifiersVecType m_ThreadProcessIdentifiersVector
ThreadProcessIdentifiers(const int tnid, const ThreadProcessIdType tph, const WinThreadIdType winThreadId)
unsigned int ThreadIdType
Definition: itkIntTypes.h:159
ThreadSemHandlePairingQueueType m_ThreadSemHandlePairingQueue
int ThreadProcessIdType
Thread pool manages the threads for itk.
Definition: itkThreadPool.h:72
unsigned int ThreadCountType
Definition: itkThreadPool.h:84
Base class for most ITK classes.
Definition: itkObject.h:57
ThreadProcessIdType m_ThreadProcessHandle
static SimpleFastMutexLock m_ThreadPoolInstanceMutex