VTK  9.2.5
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=========================================================================*/
33#ifndef vtkConditionVariable_h
34#define vtkConditionVariable_h
35
36#include "vtkCommonCoreModule.h" // For export macro
37#include "vtkDeprecation.h" // For VTK_DEPRECATED_IN_9_1_0
38#include "vtkObject.h"
39#include "vtkThreads.h" // for VTK_USE_PTHREADS and VTK_USE_WIN32_THREADS
40
41#include "vtkMutexLock.h" // Need for friend access to vtkSimpleMutexLock
42
43#if defined(VTK_USE_PTHREADS)
44#include <pthread.h> // Need POSIX thread implementation of mutex (even win32 provides mutexes)
45typedef pthread_cond_t vtkConditionType;
46#endif
47
48// Typically a top level windows application sets _WIN32_WINNT. If it is not set we set it to
49// 0x0501 (Windows XP)
50#ifdef VTK_USE_WIN32_THREADS
51#ifndef _WIN32_WINNT
52#define _WIN32_WINNT 0x0501 // 0x0501 means target Windows XP or later
53#endif
54#include "vtkWindows.h" // Needed for win32 CRITICAL_SECTION, HANDLE, etc.
55#endif
56
57#ifdef VTK_USE_WIN32_THREADS
58#if 1
59struct pthread_cond_t_t
60{
61 // Number of threads waiting on condition.
62 int WaitingThreadCount;
63
64 // Lock for WaitingThreadCount
65 CRITICAL_SECTION WaitingThreadCountCritSec;
66
67 // Semaphore to block threads waiting for the condition to change.
68 vtkWindowsHANDLE Semaphore;
69
70 // An event used to wake up thread(s) waiting on the semaphore
71 // when pthread_cond_signal or pthread_cond_broadcast is called.
72 vtkWindowsHANDLE DoneWaiting;
73
74 // Was pthread_cond_broadcast called?
75 size_t WasBroadcast;
76};
77using pthread_cond_t = struct pthread_cond_t_t;
78
79typedef pthread_cond_t vtkConditionType;
80#else // 0
81struct pthread_cond_t_t
82{
83 // Number of threads waiting on condition.
84 int WaitingThreadCount;
85
86 // Lock for WaitingThreadCount
87 CRITICAL_SECTION WaitingThreadCountCritSec;
88
89 // Number of threads to release when pthread_cond_broadcast()
90 // or pthread_cond_signal() is called.
91 int ReleaseCount;
92
93 // Used to prevent one thread from decrementing ReleaseCount all
94 // by itself instead of letting others respond.
95 int NotifyCount;
96
97 // A manual-reset event that's used to block and release waiting threads.
98 vtkWindowsHANDLE Event;
99};
100using pthread_cond_t = struct pthread_cond_t_t;
101
102typedef pthread_cond_t vtkConditionType;
103#endif // 0
104#endif // VTK_USE_WIN32_THREADS
105
106#ifndef VTK_USE_PTHREADS
107#ifndef VTK_USE_WIN32_THREADS
109#endif
110#endif
111
112// Condition variable that is not a vtkObject.
113VTK_DEPRECATED_IN_9_1_0("Use std::condition_variable_any instead.")
114class VTKCOMMONCORE_EXPORT vtkSimpleConditionVariable
115{
116public:
119
121
122 void Delete() { delete this; }
123
127 void Signal();
128
132 void Broadcast();
133
146
147protected:
149
150private:
152 vtkSimpleConditionVariable& operator=(const vtkSimpleConditionVariable& rhs) = delete;
153};
154
155VTK_DEPRECATED_IN_9_1_0("Use std::condition_variable_any instead.")
156class VTKCOMMONCORE_EXPORT vtkConditionVariable : public vtkObject
157{
158public:
161 void PrintSelf(ostream& os, vtkIndent indent) override;
162
166 void Signal();
167
171 void Broadcast();
172
184 int Wait(vtkMutexLock* mutex);
185
186protected:
188
190
191private:
193 void operator=(const vtkConditionVariable&) = delete;
194};
195
197{
199}
200
202{
204}
205
207{
208 return this->SimpleConditionVariable.Wait(lock->SimpleMutexLock);
209}
210
211#endif // vtkConditionVariable_h
mutual exclusion locking class
void PrintSelf(ostream &os, vtkIndent indent) override
Methods invoked by print to print information about the object including superclasses.
int Wait(vtkMutexLock *mutex)
Wait for the condition to change.
void Broadcast()
Wake all threads waiting for the condition to change.
vtkSimpleConditionVariable SimpleConditionVariable
void Signal()
Wake one thread waiting for the condition to change.
static vtkConditionVariable * New()
vtkConditionVariable()=default
a simple class to control print indentation
Definition: vtkIndent.h:40
mutual exclusion locking class
Definition: vtkMutexLock.h:84
vtkSimpleMutexLock SimpleMutexLock
Definition: vtkMutexLock.h:104
abstract base class for most VTK objects
Definition: vtkObject.h:63
void Signal()
Wake one thread waiting for the condition to change.
static vtkSimpleConditionVariable * New()
void Broadcast()
Wake all threads waiting for the condition to change.
int Wait(vtkSimpleMutexLock &mutex)
Wait for the condition to change.
int vtkConditionType
#define VTK_DEPRECATED_IN_9_1_0(reason)