File: itkThreadSupport.h

package info (click to toggle)
insighttoolkit5 5.4.5-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 704,588 kB
  • sloc: cpp: 784,579; ansic: 628,724; xml: 44,704; fortran: 34,250; python: 22,934; sh: 4,078; pascal: 2,636; lisp: 2,158; makefile: 461; yacc: 328; asm: 205; perl: 203; lex: 146; tcl: 132; javascript: 98; csh: 81
file content (137 lines) | stat: -rw-r--r-- 5,148 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/*=========================================================================
 *
 *  Copyright NumFOCUS
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *         https://www.apache.org/licenses/LICENSE-2.0.txt
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 *=========================================================================*/
#ifndef itkThreadSupport_h
#define itkThreadSupport_h

#include <cstdlib>

#include "itkConfigure.h" // For ITK_USE_WIN32_THREADS, ITK_USE_PTHREADS, etc.

// This implementation uses a routine called SignalObjectAndWait()
// which is only defined on WinNT 4.0 or greater systems.  We need to
// define this symbol in order to get the prototype for the
// routine. This needs to be done before we load any system headers.
#ifdef ITK_USE_WIN32_THREADS
#  ifndef _WIN32_WINNT
#    define _WIN32_WINNT 0x0501 // TBB 4.4 requires WinXP (0x0501 or greater)
#  endif
#endif

#if defined(ITK_USE_PTHREADS)
#  include <pthread.h>
#elif defined(ITK_USE_WIN32_THREADS)
#  include "itkWindows.h"
#  include <winbase.h>
#endif


namespace itk
{
/** Platform specific type alias for simple types
 */
#if defined(ITK_USE_PTHREADS)
constexpr size_t ITK_MAX_THREADS = ITK_DEFAULT_MAX_THREADS;
using MutexType = pthread_mutex_t;
using FastMutexType = pthread_mutex_t;
using ThreadFunctionType = void * (*)(void *);
using ThreadProcessIdType = pthread_t;
#  if !defined(ITK_FUTURE_LEGACY_REMOVE)
constexpr ThreadProcessIdType ITK_DEFAULT_THREAD_ID = 0;
#  endif
using ITK_THREAD_RETURN_TYPE = void *;

constexpr ITK_THREAD_RETURN_TYPE ITK_THREAD_RETURN_DEFAULT_VALUE = nullptr;
using itk::ITK_THREAD_RETURN_DEFAULT_VALUE; // We need this out of the itk namespace for #define to work below
using ITK_THREAD_RETURN_FUNCTION_CALL_CONVENTION = itk::ITK_THREAD_RETURN_TYPE;
#elif defined(ITK_USE_WIN32_THREADS)

constexpr size_t ITK_MAX_THREADS = ITK_DEFAULT_MAX_THREADS;
using MutexType = HANDLE;
using FastMutexType = CRITICAL_SECTION;
using ThreadFunctionType = unsigned int(__stdcall *)(void *);
using ThreadProcessIdType = HANDLE;
#  if !defined(ITK_FUTURE_LEGACY_REMOVE)
static const ThreadProcessIdType ITK_DEFAULT_THREAD_ID = INVALID_HANDLE_VALUE;
#  endif
using ITK_THREAD_RETURN_TYPE = unsigned int;
constexpr ITK_THREAD_RETURN_TYPE ITK_THREAD_RETURN_DEFAULT_VALUE = 0;
// WINAPI expands to __stdcall which specifies a function call convention and has little no meaning on variable
// declarations
#  define ITK_THREAD_RETURN_FUNCTION_CALL_CONVENTION itk::ITK_THREAD_RETURN_TYPE __stdcall
#else

constexpr size_t ITK_MAX_THREADS = 1;
using MutexType = int;
using FastMutexType = int;
using ThreadFunctionType = void (*)(void *);
using ThreadProcessIdType = int;
#  if !defined(ITK_FUTURE_LEGACY_REMOVE)
constexpr ThreadProcessIdType ITK_DEFAULT_THREAD_ID = 0;
#  endif
using ITK_THREAD_RETURN_TYPE = void;
#  define ITK_THREAD_RETURN_DEFAULT_VALUE
using ITK_THREAD_RETURN_FUNCTION_CALL_CONVENTION = itk::ITK_THREAD_RETURN_TYPE;
#endif


/** Platform specific Conditional Variable type
 */
struct ConditionVariableType
{
#if defined(ITK_USE_PTHREADS)
  pthread_cond_t m_ConditionVariable;
#elif defined(ITK_USE_WIN32_THREADS)
  int              m_NumberOfWaiters;     // number of waiting threads
  CRITICAL_SECTION m_NumberOfWaitersLock; // Serialize access to
                                          // m_NumberOfWaiters

  HANDLE m_Semaphore;      // Semaphore to queue threads
  HANDLE m_WaitersAreDone; // Auto-reset event used by the
                           // broadcast/signal thread to
                           // wait for all the waiting
                           // threads to wake up and
                           // release the semaphore

  int m_WasBroadcast;                       // Used as boolean. Keeps track of whether
                                            // we were broadcasting or signaling
#endif
};

} // namespace itk

// Compile-time conditional code for different threading models
// require that some items are #defines (always global scope) or
// can sometimes be rigorously typed.  When rigorously typed,
// we need to re-exposed to the global namespace to keep the
// use of these items consistent.
#if defined(ITK_USE_PTHREADS)
using itk::ITK_THREAD_RETURN_FUNCTION_CALL_CONVENTION;
using itk::ITK_THREAD_RETURN_DEFAULT_VALUE; // We need this out of the itk namespace for #define to work below
#elif defined(ITK_USE_WIN32_THREADS)
using itk::ITK_THREAD_RETURN_DEFAULT_VALUE; // We need this out of the itk namespace for #define to work below
#else
using itk::ITK_THREAD_RETURN_FUNCTION_CALL_CONVENTION;
#endif

// For backwards compatibility
#if !defined(ITK_FUTURE_LEGACY_REMOVE)
using itk::ITK_MAX_THREADS;
using itk::ITK_DEFAULT_THREAD_ID;
#endif

#endif