ITK  6.0.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 
43 namespace itk
44 {
47 #if defined(ITK_USE_PTHREADS)
48 constexpr size_t ITK_MAX_THREADS = ITK_DEFAULT_MAX_THREADS;
49 using MutexType = pthread_mutex_t;
50 using FastMutexType = pthread_mutex_t;
51 using ThreadFunctionType = void * (*)(void *);
52 using ThreadProcessIdType = pthread_t;
53 # if !defined(ITK_FUTURE_LEGACY_REMOVE)
54 constexpr ThreadProcessIdType ITK_DEFAULT_THREAD_ID = 0;
55 # endif
56 using ITK_THREAD_RETURN_TYPE = void *;
60 using 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 
64 constexpr size_t ITK_MAX_THREADS = ITK_DEFAULT_MAX_THREADS;
65 using MutexType = HANDLE;
66 using FastMutexType = CRITICAL_SECTION;
67 using ThreadFunctionType = unsigned int(__stdcall *)(void *);
68 using ThreadProcessIdType = HANDLE;
69 # if !defined(ITK_FUTURE_LEGACY_REMOVE)
70 static const ThreadProcessIdType ITK_DEFAULT_THREAD_ID = INVALID_HANDLE_VALUE;
71 # endif
72 using 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 
79 constexpr size_t ITK_MAX_THREADS = 1;
80 using MutexType = int;
81 using FastMutexType = int;
82 using ThreadFunctionType = void (*)(void *);
83 using ThreadProcessIdType = int;
84 # if !defined(ITK_FUTURE_LEGACY_REMOVE)
85 constexpr 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)
126 using 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)
128 using 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)
136 using itk::ITK_DEFAULT_THREAD_ID;
137 #endif
138 
139 #endif
itk::ITK_THREAD_RETURN_TYPE
void ITK_THREAD_RETURN_TYPE
Definition: itkThreadSupport.h:87
itk::ConditionVariableType
Definition: itkThreadSupport.h:95
itk::FastMutexType
int FastMutexType
Definition: itkThreadSupport.h:81
itk::ITK_MAX_THREADS
constexpr vcl_size_t ITK_MAX_THREADS
Definition: itkThreadSupport.h:79
itk::ThreadFunctionType
void(*)(void *) ThreadFunctionType
Definition: itkThreadSupport.h:82
itk::ITK_THREAD_RETURN_FUNCTION_CALL_CONVENTION
itk::ITK_THREAD_RETURN_TYPE ITK_THREAD_RETURN_FUNCTION_CALL_CONVENTION
Definition: itkThreadSupport.h:89
itkWindows.h
itk
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....
Definition: itkAnatomicalOrientation.h:29
ITK_THREAD_RETURN_DEFAULT_VALUE
#define ITK_THREAD_RETURN_DEFAULT_VALUE
Definition: itkThreadSupport.h:88
itk::MutexType
int MutexType
Definition: itkThreadSupport.h:80
itk::ThreadProcessIdType
int ThreadProcessIdType
Definition: itkThreadSupport.h:83