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 #if defined(ITK_USE_PTHREADS)
26 # include <pthread.h>
27 #elif defined(ITK_USE_WIN32_THREADS)
28 # include "itkWindows.h"
29 # include <winbase.h>
30 #endif
31 
32 
33 namespace itk
34 {
37 #if defined(ITK_USE_PTHREADS)
38 constexpr size_t ITK_MAX_THREADS = ITK_DEFAULT_MAX_THREADS;
39 using MutexType = pthread_mutex_t;
40 using FastMutexType = pthread_mutex_t;
41 using ThreadFunctionType = void * (*)(void *);
42 using ThreadProcessIdType = pthread_t;
43 # if !defined(ITK_FUTURE_LEGACY_REMOVE)
44 constexpr ThreadProcessIdType ITK_DEFAULT_THREAD_ID = 0;
45 # endif
46 using ITK_THREAD_RETURN_TYPE = void *;
50 using itk::ITK_THREAD_RETURN_DEFAULT_VALUE; // We need this out of the itk namespace for #define to work below
52 #elif defined(ITK_USE_WIN32_THREADS)
53 
54 constexpr size_t ITK_MAX_THREADS = ITK_DEFAULT_MAX_THREADS;
55 using MutexType = HANDLE;
56 using FastMutexType = CRITICAL_SECTION;
57 using ThreadFunctionType = unsigned int(__stdcall *)(void *);
58 using ThreadProcessIdType = HANDLE;
59 # if !defined(ITK_FUTURE_LEGACY_REMOVE)
60 static const ThreadProcessIdType ITK_DEFAULT_THREAD_ID = INVALID_HANDLE_VALUE;
61 # endif
62 using ITK_THREAD_RETURN_TYPE = unsigned int;
64 // WINAPI expands to __stdcall which specifies a function call convention and has little no meaning on variable
65 // declarations
66 # define ITK_THREAD_RETURN_FUNCTION_CALL_CONVENTION itk::ITK_THREAD_RETURN_TYPE __stdcall
67 #else
68 
69 constexpr size_t ITK_MAX_THREADS = 1;
70 using MutexType = int;
71 using FastMutexType = int;
72 using ThreadFunctionType = void (*)(void *);
73 using ThreadProcessIdType = int;
74 # if !defined(ITK_FUTURE_LEGACY_REMOVE)
75 constexpr ThreadProcessIdType ITK_DEFAULT_THREAD_ID = 0;
76 # endif
78 # define ITK_THREAD_RETURN_DEFAULT_VALUE
80 #endif
81 
82 
86 {
87 #if defined(ITK_USE_PTHREADS)
88  pthread_cond_t m_ConditionVariable;
89 #elif defined(ITK_USE_WIN32_THREADS)
90  int m_NumberOfWaiters; // number of waiting threads
91  CRITICAL_SECTION m_NumberOfWaitersLock; // Serialize access to
92  // m_NumberOfWaiters
95  HANDLE m_Semaphore; // Semaphore to queue threads
96  HANDLE m_WaitersAreDone; // Auto-reset event used by the
97  // broadcast/signal thread to
98  // wait for all the waiting
99  // threads to wake up and
100  // release the semaphore
101 
102  int m_WasBroadcast; // Used as boolean. Keeps track of whether
103  // we were broadcasting or signaling
104 #endif
105 };
106 
107 } // namespace itk
108 
109 // Compile-time conditional code for different threading models
110 // require that some items are #defines (always global scope) or
111 // can sometimes be rigorously typed. When rigorously typed,
112 // we need to re-expose to the global namespace to keep the
113 // use of these items consistent.
114 #if defined(ITK_USE_PTHREADS)
116 using itk::ITK_THREAD_RETURN_DEFAULT_VALUE; // We need this out of the itk namespace for #define to work below
117 #elif defined(ITK_USE_WIN32_THREADS)
118 using itk::ITK_THREAD_RETURN_DEFAULT_VALUE; // We need this out of the itk namespace for #define to work below
119 #else
121 #endif
122 
123 // For backwards compatibility
124 #if !defined(ITK_FUTURE_LEGACY_REMOVE)
126 using itk::ITK_DEFAULT_THREAD_ID;
127 #endif
128 
129 #endif
itk::ITK_THREAD_RETURN_TYPE
void ITK_THREAD_RETURN_TYPE
Definition: itkThreadSupport.h:77
itk::ConditionVariableType
Definition: itkThreadSupport.h:85
itk::FastMutexType
int FastMutexType
Definition: itkThreadSupport.h:71
itk::ITK_MAX_THREADS
constexpr vcl_size_t ITK_MAX_THREADS
Definition: itkThreadSupport.h:69
itk::ThreadFunctionType
void(*)(void *) ThreadFunctionType
Definition: itkThreadSupport.h:72
itk::ITK_THREAD_RETURN_FUNCTION_CALL_CONVENTION
itk::ITK_THREAD_RETURN_TYPE ITK_THREAD_RETURN_FUNCTION_CALL_CONVENTION
Definition: itkThreadSupport.h:79
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:78
itk::MutexType
int MutexType
Definition: itkThreadSupport.h:70
itk::ThreadProcessIdType
int ThreadProcessIdType
Definition: itkThreadSupport.h:73