ITK 6.0.0
Insight Toolkit
 
Loading...
Searching...
No Matches
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
33namespace itk
34{
38#if defined(ITK_USE_PTHREADS)
39constexpr size_t ITK_MAX_THREADS = ITK_DEFAULT_MAX_THREADS;
40using MutexType = pthread_mutex_t;
41using FastMutexType = pthread_mutex_t;
42using ThreadFunctionType = void * (*)(void *);
43using ThreadProcessIdType = pthread_t;
44# if !defined(ITK_FUTURE_LEGACY_REMOVE)
45constexpr ThreadProcessIdType ITK_DEFAULT_THREAD_ID = 0;
46# endif
47using ITK_THREAD_RETURN_TYPE = void *;
50using 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
54constexpr size_t ITK_MAX_THREADS = ITK_DEFAULT_MAX_THREADS;
55using MutexType = HANDLE;
56using FastMutexType = CRITICAL_SECTION;
57using ThreadFunctionType = unsigned int(__stdcall *)(void *);
58using ThreadProcessIdType = HANDLE;
59# if !defined(ITK_FUTURE_LEGACY_REMOVE)
60static const ThreadProcessIdType ITK_DEFAULT_THREAD_ID = INVALID_HANDLE_VALUE;
61# endif
62using 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
69constexpr size_t ITK_MAX_THREADS = 1;
70using MutexType = int;
71using FastMutexType = int;
72using ThreadFunctionType = void (*)(void *);
74# if !defined(ITK_FUTURE_LEGACY_REMOVE)
75constexpr 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
93
94 HANDLE m_Semaphore; // Semaphore to queue threads
95 HANDLE m_WaitersAreDone; // Auto-reset event used by the
96 // broadcast/signal thread to
97 // wait for all the waiting
98 // threads to wake up and
99 // release the semaphore
100
101 int m_WasBroadcast; // Used as boolean. Keeps track of whether
102 // we were broadcasting or signaling
103#endif
104};
105
106} // namespace itk
107
108// Compile-time conditional code for different threading models
109// require that some items are #defines (always global scope) or
110// can sometimes be rigorously typed. When rigorously typed,
111// we need to re-expose to the global namespace to keep the
112// use of these items consistent.
113#if defined(ITK_USE_PTHREADS)
115using itk::ITK_THREAD_RETURN_DEFAULT_VALUE; // We need this out of the itk namespace for #define to work below
116#elif defined(ITK_USE_WIN32_THREADS)
117using itk::ITK_THREAD_RETURN_DEFAULT_VALUE; // We need this out of the itk namespace for #define to work below
118#else
120#endif
121
122// For backwards compatibility
123#if !defined(ITK_FUTURE_LEGACY_REMOVE)
125using itk::ITK_DEFAULT_THREAD_ID;
126#endif
127
128#endif
#define ITK_THREAD_RETURN_DEFAULT_VALUE
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....
void ITK_THREAD_RETURN_TYPE
int FastMutexType
int MutexType
constexpr vcl_size_t ITK_MAX_THREADS
itk::ITK_THREAD_RETURN_TYPE ITK_THREAD_RETURN_FUNCTION_CALL_CONVENTION
void(*)(void *) ThreadFunctionType
int ThreadProcessIdType