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)
42typedef 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
56struct 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};
74using pthread_cond_t = struct pthread_cond_t_t;
75
76typedef pthread_cond_t vtkConditionType;
77#else // 0
78struct 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};
97using pthread_cond_t = struct pthread_cond_t_t;
98
99typedef 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
106#endif
107#endif
108
109// Condition variable that is not a vtkObject.
110VTK_DEPRECATED_IN_9_1_0("Use std::condition_variable_any instead.")
111class VTKCOMMONCORE_EXPORT vtkSimpleConditionVariable
112{
113public:
116
118
119 void Delete() { delete this; }
120
124 void Signal();
125
129 void Broadcast();
130
143
144protected:
146
147private:
149 vtkSimpleConditionVariable& operator=(const vtkSimpleConditionVariable& rhs) = delete;
150};
151
152VTK_DEPRECATED_IN_9_1_0("Use std::condition_variable_any instead.")
153class VTKCOMMONCORE_EXPORT vtkConditionVariable : public vtkObject
154{
155public:
158 void PrintSelf(ostream& os, vtkIndent indent) override;
159
163 void Signal();
164
168 void Broadcast();
169
181 int Wait(vtkMutexLock* mutex);
182
183protected:
185
187
188private:
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
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:113
mutual exclusion locking class
Definition: vtkMutexLock.h:81
vtkSimpleMutexLock SimpleMutexLock
Definition: vtkMutexLock.h:101
abstract base class for most VTK objects
Definition: vtkObject.h:82
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)