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  * 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 // 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 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;
52 # if !defined(ITK_FUTURE_LEGACY_REMOVE)
53 constexpr ThreadProcessIdType ITK_DEFAULT_THREAD_ID = 0;
54 # endif
55 using ITK_THREAD_RETURN_TYPE = void *;
59 using itk::ITK_THREAD_RETURN_DEFAULT_VALUE; // We need this out of the itk namespace for #define to work below
61 #elif defined(ITK_USE_WIN32_THREADS)
62 
63 constexpr size_t ITK_MAX_THREADS = ITK_DEFAULT_MAX_THREADS;
64 using MutexType = HANDLE;
65 using FastMutexType = CRITICAL_SECTION;
66 using ThreadFunctionType = unsigned int(__stdcall *)(void *);
67 using ThreadProcessIdType = HANDLE;
68 # if !defined(ITK_FUTURE_LEGACY_REMOVE)
69 static const ThreadProcessIdType ITK_DEFAULT_THREAD_ID = INVALID_HANDLE_VALUE;
70 # endif
71 using ITK_THREAD_RETURN_TYPE = unsigned int;
73 // WINAPI expands to __stdcall which specifies a function call convention and has little no meaning on variable
74 // declarations
75 # define ITK_THREAD_RETURN_FUNCTION_CALL_CONVENTION itk::ITK_THREAD_RETURN_TYPE __stdcall
76 #else
77 
78 constexpr size_t ITK_MAX_THREADS = 1;
79 using MutexType = int;
80 using FastMutexType = int;
81 using ThreadFunctionType = void (*)(void *);
82 using ThreadProcessIdType = int;
83 # if !defined(ITK_FUTURE_LEGACY_REMOVE)
84 constexpr ThreadProcessIdType ITK_DEFAULT_THREAD_ID = 0;
85 # endif
87 # define ITK_THREAD_RETURN_DEFAULT_VALUE
89 #endif
90 
91 
95 {
96 #if defined(ITK_USE_PTHREADS)
97  pthread_cond_t m_ConditionVariable;
98 #elif defined(ITK_USE_WIN32_THREADS)
99  int m_NumberOfWaiters; // number of waiting threads
100  CRITICAL_SECTION m_NumberOfWaitersLock; // Serialize access to
101  // m_NumberOfWaiters
104  HANDLE m_Semaphore; // Semaphore to queue threads
105  HANDLE m_WaitersAreDone; // Auto-reset event used by the
106  // broadcast/signal thread to
107  // wait for all the waiting
108  // threads to wake up and
109  // release the semaphore
110 
111  int m_WasBroadcast; // Used as boolean. Keeps track of whether
112  // we were broadcasting or signaling
113 #endif
114 };
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)
135 using itk::ITK_DEFAULT_THREAD_ID;
136 #endif
137 
138 #endif
itk::ITK_THREAD_RETURN_TYPE
void ITK_THREAD_RETURN_TYPE
Definition: itkThreadSupport.h:86
itk::ConditionVariableType
Definition: itkThreadSupport.h:94
itk::FastMutexType
int FastMutexType
Definition: itkThreadSupport.h:80
itk::ITK_MAX_THREADS
constexpr vcl_size_t ITK_MAX_THREADS
Definition: itkThreadSupport.h:78
itk::ThreadFunctionType
void(*)(void *) ThreadFunctionType
Definition: itkThreadSupport.h:81
itk::ITK_THREAD_RETURN_FUNCTION_CALL_CONVENTION
itk::ITK_THREAD_RETURN_TYPE ITK_THREAD_RETURN_FUNCTION_CALL_CONVENTION
Definition: itkThreadSupport.h:88
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:87
itk::MutexType
int MutexType
Definition: itkThreadSupport.h:79
itk::ThreadProcessIdType
int ThreadProcessIdType
Definition: itkThreadSupport.h:82