VTK  9.2.6
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)
45 typedef 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
59 struct 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 };
77 using pthread_cond_t = struct pthread_cond_t_t;
78 
79 typedef pthread_cond_t vtkConditionType;
80 #else // 0
81 struct 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 };
100 using pthread_cond_t = struct pthread_cond_t_t;
101 
102 typedef 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
108 typedef int vtkConditionType;
109 #endif
110 #endif
111 
112 // Condition variable that is not a vtkObject.
113 VTK_DEPRECATED_IN_9_1_0("Use std::condition_variable_any instead.")
114 class VTKCOMMONCORE_EXPORT vtkSimpleConditionVariable
115 {
116 public:
117  vtkSimpleConditionVariable();
118  ~vtkSimpleConditionVariable();
119 
120  static vtkSimpleConditionVariable* New();
121 
122  void Delete() { delete this; }
123 
127  void Signal();
128 
132  void Broadcast();
133 
145  int Wait(vtkSimpleMutexLock& mutex);
146 
147 protected:
149 
150 private:
151  vtkSimpleConditionVariable(const vtkSimpleConditionVariable& other) = delete;
152  vtkSimpleConditionVariable& operator=(const vtkSimpleConditionVariable& rhs) = delete;
153 };
154 
155 VTK_DEPRECATED_IN_9_1_0("Use std::condition_variable_any instead.")
156 class VTKCOMMONCORE_EXPORT vtkConditionVariable : public vtkObject
157 {
158 public:
159  static vtkConditionVariable* New();
160  vtkTypeMacro(vtkConditionVariable, vtkObject);
161  void PrintSelf(ostream& os, vtkIndent indent) override;
162 
166  void Signal();
167 
171  void Broadcast();
172 
184  int Wait(vtkMutexLock* mutex);
185 
186 protected:
187  vtkConditionVariable() = default;
188 
190 
191 private:
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
void Signal()
Wake one thread waiting for the condition to change.
#define VTK_DEPRECATED_IN_9_1_0(reason)
mutual exclusion locking class
abstract base class for most VTK objects
Definition: vtkObject.h:62
int Wait(vtkSimpleMutexLock &mutex)
Wait for the condition to change.
void Broadcast()
Wake all threads waiting for the condition to change.
void Signal()
Wake one thread waiting for the condition to change.
a simple class to control print indentation
Definition: vtkIndent.h:39
void Broadcast()
Wake all threads waiting for the condition to change.
int Wait(vtkMutexLock *mutex)
Wait for the condition to change.
vtkSimpleMutexLock SimpleMutexLock
Definition: vtkMutexLock.h:104
int vtkConditionType
vtkSimpleConditionVariable SimpleConditionVariable
mutual exclusion locking class
Definition: vtkMutexLock.h:82