ITK  5.4.0
Insight Toolkit
itkThreadSupport.h
Go to the documentation of this file.
1/*=========================================================================
2 *
3 * Copyright NumFOCUS
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 * https://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 itkThreadSupport_h
19#define itkThreadSupport_h
20
21#include <cstdlib>
22
23#include "itkConfigure.h" // For ITK_USE_WIN32_THREADS, ITK_USE_PTHREADS, etc.
24
25// This implementation uses a routine called SignalObjectAndWait()
26// which is only defined on WinNT 4.0 or greater systems. We need to
27// define this symbol in order to get the prototype for the
28// routine. This needs to be done before we load any system headers.
29#ifdef ITK_USE_WIN32_THREADS
30# ifndef _WIN32_WINNT
31# define _WIN32_WINNT 0x0501 // TBB 4.4 requires WinXP (0x0501 or greater)
32# endif
33#endif
34
35#if defined(ITK_USE_PTHREADS)
36# include <pthread.h>
37#elif defined(ITK_USE_WIN32_THREADS)
38# include "itkWindows.h"
39# include <winbase.h>
40#endif
41
42
43namespace itk
44{
47#if defined(ITK_USE_PTHREADS)
48constexpr size_t ITK_MAX_THREADS = ITK_DEFAULT_MAX_THREADS;
49using MutexType = pthread_mutex_t;
50using FastMutexType = pthread_mutex_t;
51using ThreadFunctionType = void * (*)(void *);
52using ThreadProcessIdType = pthread_t;
53# if !defined(ITK_FUTURE_LEGACY_REMOVE)
54constexpr ThreadProcessIdType ITK_DEFAULT_THREAD_ID = 0;
55# endif
56using ITK_THREAD_RETURN_TYPE = void *;
60using itk::ITK_THREAD_RETURN_DEFAULT_VALUE; // We need this out of the itk namespace for #define to work below
62#elif defined(ITK_USE_WIN32_THREADS)
63
64constexpr size_t ITK_MAX_THREADS = ITK_DEFAULT_MAX_THREADS;
65using MutexType = HANDLE;
66using FastMutexType = CRITICAL_SECTION;
67using ThreadFunctionType = unsigned int(__stdcall *)(void *);
68using ThreadProcessIdType = HANDLE;
69# if !defined(ITK_FUTURE_LEGACY_REMOVE)
70static const ThreadProcessIdType ITK_DEFAULT_THREAD_ID = INVALID_HANDLE_VALUE;
71# endif
72using ITK_THREAD_RETURN_TYPE = unsigned int;
74// WINAPI expands to __stdcall which specifies a function call convention and has little no meaning on variable
75// declarations
76# define ITK_THREAD_RETURN_FUNCTION_CALL_CONVENTION itk::ITK_THREAD_RETURN_TYPE __stdcall
77#else
78
79constexpr size_t ITK_MAX_THREADS = 1;
80using MutexType = int;
81using FastMutexType = int;
82using ThreadFunctionType = void (*)(void *);
84# if !defined(ITK_FUTURE_LEGACY_REMOVE)
85constexpr ThreadProcessIdType ITK_DEFAULT_THREAD_ID = 0;
86# endif
88# define ITK_THREAD_RETURN_DEFAULT_VALUE
90#endif
91
92
96{
97#if defined(ITK_USE_PTHREADS)
98 pthread_cond_t m_ConditionVariable;
99#elif defined(ITK_USE_WIN32_THREADS)
100 int m_NumberOfWaiters; // number of waiting threads
101 CRITICAL_SECTION m_NumberOfWaitersLock; // Serialize access to
102 // m_NumberOfWaiters
105 HANDLE m_Semaphore; // Semaphore to queue threads
106 HANDLE m_WaitersAreDone; // Auto-reset event used by the
107 // broadcast/signal thread to
108 // wait for all the waiting
109 // threads to wake up and
110 // release the semaphore
111
112 int m_WasBroadcast; // Used as boolean. Keeps track of whether
113 // we were broadcasting or signaling
114#endif
115};
116
117} // namespace itk
118
119// Compile-time conditional code for different threading models
120// require that some items are #defines (always global scope) or
121// can sometimes be rigorously typed. When rigorously typed,
122// we need to re-exposed to the global namespace to keep the
123// use of these items consistent.
124#if defined(ITK_USE_PTHREADS)
126using itk::ITK_THREAD_RETURN_DEFAULT_VALUE; // We need this out of the itk namespace for #define to work below
127#elif defined(ITK_USE_WIN32_THREADS)
128using itk::ITK_THREAD_RETURN_DEFAULT_VALUE; // We need this out of the itk namespace for #define to work below
129#else
131#endif
132
133// For backwards compatibility
134#if !defined(ITK_FUTURE_LEGACY_REMOVE)
136using itk::ITK_DEFAULT_THREAD_ID;
137#endif
138
139#endif
#define ITK_THREAD_RETURN_DEFAULT_VALUE
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....
void ITK_THREAD_RETURN_TYPE
constexpr vcl_size_t ITK_MAX_THREADS
void(*)(void *) ThreadFunctionType
int MutexType
itk::ITK_THREAD_RETURN_TYPE ITK_THREAD_RETURN_FUNCTION_CALL_CONVENTION
int ThreadProcessIdType
int FastMutexType