VTK  9.1.0
vtkConditionVariable.h
Go to the documentation of this file.
1 /*=========================================================================
2 
3  Program: Visualization Toolkit
4  Module: vtkConditionVariable.h
5 
6  Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
7  All rights reserved.
8  See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
9 
10  This software is distributed WITHOUT ANY WARRANTY; without even
11  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
12  PURPOSE. See the above copyright notice for more information.
13 
14 =========================================================================*/
30 #ifndef vtkConditionVariable_h
31 #define vtkConditionVariable_h
32 
33 #include "vtkCommonCoreModule.h" // For export macro
34 #include "vtkDeprecation.h" // For VTK_DEPRECATED_IN_9_1_0
35 #include "vtkObject.h"
36 #include "vtkThreads.h" // for VTK_USE_PTHREADS and VTK_USE_WIN32_THREADS
37 
38 #include "vtkMutexLock.h" // Need for friend access to vtkSimpleMutexLock
39 
40 #if defined(VTK_USE_PTHREADS)
41 #include <pthread.h> // Need POSIX thread implementation of mutex (even win32 provides mutexes)
42 typedef pthread_cond_t vtkConditionType;
43 #endif
44 
45 // Typically a top level windows application sets _WIN32_WINNT. If it is not set we set it to
46 // 0x0501 (Windows XP)
47 #ifdef VTK_USE_WIN32_THREADS
48 #ifndef _WIN32_WINNT
49 #define _WIN32_WINNT 0x0501 // 0x0501 means target Windows XP or later
50 #endif
51 #include "vtkWindows.h" // Needed for win32 CRITICAL_SECTION, HANDLE, etc.
52 #endif
53 
54 #ifdef VTK_USE_WIN32_THREADS
55 #if 1
56 struct pthread_cond_t_t
57 {
58  // Number of threads waiting on condition.
59  int WaitingThreadCount;
60 
61  // Lock for WaitingThreadCount
62  CRITICAL_SECTION WaitingThreadCountCritSec;
63 
64  // Semaphore to block threads waiting for the condition to change.
65  vtkWindowsHANDLE Semaphore;
66 
67  // An event used to wake up thread(s) waiting on the semaphore
68  // when pthread_cond_signal or pthread_cond_broadcast is called.
69  vtkWindowsHANDLE DoneWaiting;
70 
71  // Was pthread_cond_broadcast called?
72  size_t WasBroadcast;
73 };
74 using pthread_cond_t = struct pthread_cond_t_t;
75 
76 typedef pthread_cond_t vtkConditionType;
77 #else // 0
78 struct pthread_cond_t_t
79 {
80  // Number of threads waiting on condition.
81  int WaitingThreadCount;
82 
83  // Lock for WaitingThreadCount
84  CRITICAL_SECTION WaitingThreadCountCritSec;
85 
86  // Number of threads to release when pthread_cond_broadcast()
87  // or pthread_cond_signal() is called.
88  int ReleaseCount;
89 
90  // Used to prevent one thread from decrementing ReleaseCount all
91  // by itself instead of letting others respond.
92  int NotifyCount;
93 
94  // A manual-reset event that's used to block and release waiting threads.
95  vtkWindowsHANDLE Event;
96 };
97 using pthread_cond_t = struct pthread_cond_t_t;
98 
99 typedef pthread_cond_t vtkConditionType;
100 #endif // 0
101 #endif // VTK_USE_WIN32_THREADS
102 
103 #ifndef VTK_USE_PTHREADS
104 #ifndef VTK_USE_WIN32_THREADS
105 typedef int vtkConditionType;
106 #endif
107 #endif
108 
109 // Condition variable that is not a vtkObject.
110 VTK_DEPRECATED_IN_9_1_0("Use std::condition_variable_any instead.")
111 class VTKCOMMONCORE_EXPORT vtkSimpleConditionVariable
112 {
113 public:
116 
118 
119  void Delete() { delete this; }
120 
124  void Signal();
125 
129  void Broadcast();
130 
143 
144 protected:
146 
147 private:
149  vtkSimpleConditionVariable& operator=(const vtkSimpleConditionVariable& rhs) = delete;
150 };
151 
152 VTK_DEPRECATED_IN_9_1_0("Use std::condition_variable_any instead.")
153 class VTKCOMMONCORE_EXPORT vtkConditionVariable : public vtkObject
154 {
155 public:
158  void PrintSelf(ostream& os, vtkIndent indent) override;
159 
163  void Signal();
164 
168  void Broadcast();
169 
181  int Wait(vtkMutexLock* mutex);
182 
183 protected:
184  vtkConditionVariable() = default;
185 
187 
188 private:
190  void operator=(const vtkConditionVariable&) = delete;
191 };
192 
194 {
196 }
197 
199 {
201 }
202 
204 {
205  return this->SimpleConditionVariable.Wait(lock->SimpleMutexLock);
206 }
207 
208 #endif // vtkConditionVariable_h
vtkSimpleConditionVariable::Broadcast
void Broadcast()
Wake all threads waiting for the condition to change.
vtkMutexLock.h
vtkConditionVariable::New
static vtkConditionVariable * New()
vtkMutexLock::SimpleMutexLock
vtkSimpleMutexLock SimpleMutexLock
Definition: vtkMutexLock.h:101
vtkMutexLock
mutual exclusion locking class
Definition: vtkMutexLock.h:81
vtkConditionVariable::Broadcast
void Broadcast()
Wake all threads waiting for the condition to change.
Definition: vtkConditionVariable.h:198
vtkObject
abstract base class for most VTK objects
Definition: vtkObject.h:82
VTK_DEPRECATED_IN_9_1_0
#define VTK_DEPRECATED_IN_9_1_0(reason)
Definition: vtkDeprecation.h:117
vtkSimpleConditionVariable::~vtkSimpleConditionVariable
~vtkSimpleConditionVariable()
vtkSimpleConditionVariable::Delete
void Delete()
Definition: vtkConditionVariable.h:119
vtkSimpleConditionVariable::vtkSimpleConditionVariable
vtkSimpleConditionVariable()
vtkSimpleConditionVariable::Wait
int Wait(vtkSimpleMutexLock &mutex)
Wait for the condition to change.
vtkConditionVariable::Signal
void Signal()
Wake one thread waiting for the condition to change.
Definition: vtkConditionVariable.h:193
vtkConditionVariable::SimpleConditionVariable
vtkSimpleConditionVariable SimpleConditionVariable
Definition: vtkConditionVariable.h:186
vtkSimpleConditionVariable::ConditionVariable
vtkConditionType ConditionVariable
Definition: vtkConditionVariable.h:145
vtkSimpleConditionVariable::New
static vtkSimpleConditionVariable * New()
vtkSimpleConditionVariable::Signal
void Signal()
Wake one thread waiting for the condition to change.
vtkIndent
a simple class to control print indentation
Definition: vtkIndent.h:113
vtkObject.h
vtkSimpleMutexLock
Definition: vtkMutexLock.h:50
vtkConditionType
int vtkConditionType
Definition: vtkConditionVariable.h:105
vtkConditionVariable
mutual exclusion locking class
Definition: vtkConditionVariable.h:154
vtkSimpleConditionVariable
Definition: vtkConditionVariable.h:112
vtkDeprecation.h
vtkConditionVariable::PrintSelf
void PrintSelf(ostream &os, vtkIndent indent) override
Methods invoked by print to print information about the object including superclasses.
vtkConditionVariable::Wait
int Wait(vtkMutexLock *mutex)
Wait for the condition to change.
Definition: vtkConditionVariable.h:203
vtkConditionVariable::vtkConditionVariable
vtkConditionVariable()=default