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