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
33namespace itk
34{
37#if defined(ITK_USE_PTHREADS)
38constexpr size_t ITK_MAX_THREADS = ITK_DEFAULT_MAX_THREADS;
39using MutexType = pthread_mutex_t;
40using FastMutexType = pthread_mutex_t;
41using ThreadFunctionType = void * (*)(void *);
42using ThreadProcessIdType = pthread_t;
43# if !defined(ITK_FUTURE_LEGACY_REMOVE)
44constexpr ThreadProcessIdType ITK_DEFAULT_THREAD_ID = 0;
45# endif
46using 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
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)
116using 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)
118using 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)
126using itk::ITK_DEFAULT_THREAD_ID;
127#endif
128
129#endif
#define ITK_THREAD_RETURN_DEFAULT_VALUE
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....
void ITK_THREAD_RETURN_TYPE
constexpr vcl_size_t ITK_MAX_THREADS
void(*)(void *) ThreadFunctionType
int MutexType
itk::ITK_THREAD_RETURN_TYPE ITK_THREAD_RETURN_FUNCTION_CALL_CONVENTION
int ThreadProcessIdType
int FastMutexType