VTK  9.0.3
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 "vtkObject.h"
35 
36 #include "vtkMutexLock.h" // Need for friend access to vtkSimpleMutexLock
37 
38 #if defined(VTK_USE_PTHREADS)
39 #include <pthread.h> // Need POSIX thread implementation of mutex (even win32 provides mutexes)
40 typedef pthread_cond_t vtkConditionType;
41 #endif
42 
43 // Typically a top level windows application sets _WIN32_WINNT. If it is not set we set it to
44 // 0x0501 (Windows XP)
45 #ifdef VTK_USE_WIN32_THREADS
46 #ifndef _WIN32_WINNT
47 #define _WIN32_WINNT 0x0501 // 0x0501 means target Windows XP or later
48 #endif
49 #include "vtkWindows.h" // Needed for win32 CRITICAL_SECTION, HANDLE, etc.
50 #endif
51 
52 #ifdef VTK_USE_WIN32_THREADS
53 #if 1
54 typedef struct
55 {
56  // Number of threads waiting on condition.
57  int WaitingThreadCount;
58 
59  // Lock for WaitingThreadCount
60  CRITICAL_SECTION WaitingThreadCountCritSec;
61 
62  // Semaphore to block threads waiting for the condition to change.
63  vtkWindowsHANDLE Semaphore;
64 
65  // An event used to wake up thread(s) waiting on the semaphore
66  // when pthread_cond_signal or pthread_cond_broadcast is called.
67  vtkWindowsHANDLE DoneWaiting;
68 
69  // Was pthread_cond_broadcast called?
70  size_t WasBroadcast;
71 } pthread_cond_t;
72 
73 typedef pthread_cond_t vtkConditionType;
74 #else // 0
75 typedef struct
76 {
77  // Number of threads waiting on condition.
78  int WaitingThreadCount;
79 
80  // Lock for WaitingThreadCount
81  CRITICAL_SECTION WaitingThreadCountCritSec;
82 
83  // Number of threads to release when pthread_cond_broadcast()
84  // or pthread_cond_signal() is called.
85  int ReleaseCount;
86 
87  // Used to prevent one thread from decrementing ReleaseCount all
88  // by itself instead of letting others respond.
89  int NotifyCount;
90 
91  // A manual-reset event that's used to block and release waiting threads.
92  vtkWindowsHANDLE Event;
93 } pthread_cond_t;
94 
95 typedef pthread_cond_t vtkConditionType;
96 #endif // 0
97 #endif // VTK_USE_WIN32_THREADS
98 
99 #ifndef VTK_USE_PTHREADS
100 #ifndef VTK_USE_WIN32_THREADS
101 typedef int vtkConditionType;
102 #endif
103 #endif
104 
105 // Condition variable that is not a vtkObject.
106 class VTKCOMMONCORE_EXPORT vtkSimpleConditionVariable
107 {
108 public:
111 
113 
114  void Delete() { delete this; }
115 
119  void Signal();
120 
124  void Broadcast();
125 
138 
139 protected:
141 
142 private:
144  vtkSimpleConditionVariable& operator=(const vtkSimpleConditionVariable& rhs) = delete;
145 };
146 
147 class VTKCOMMONCORE_EXPORT vtkConditionVariable : public vtkObject
148 {
149 public:
152  void PrintSelf(ostream& os, vtkIndent indent) override;
153 
157  void Signal();
158 
162  void Broadcast();
163 
175  int Wait(vtkMutexLock* mutex);
176 
177 protected:
179 
181 
182 private:
184  void operator=(const vtkConditionVariable&) = delete;
185 };
186 
188 {
190 }
191 
193 {
195 }
196 
198 {
199  return this->SimpleConditionVariable.Wait(lock->SimpleMutexLock);
200 }
201 
202 #endif // vtkConditionVariable_h
mutual exclusion locking class
static vtkConditionVariable * New()
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.
a simple class to control print indentation
Definition: vtkIndent.h:34
mutual exclusion locking class
Definition: vtkMutexLock.h:77
vtkSimpleMutexLock SimpleMutexLock
Definition: vtkMutexLock.h:97
abstract base class for most VTK objects
Definition: vtkObject.h:54
void Signal()
Wake one thread waiting for the condition to change.
void Broadcast()
Wake all threads waiting for the condition to change.
static vtkSimpleConditionVariable * New()
int Wait(vtkSimpleMutexLock &mutex)
Wait for the condition to change.
int vtkConditionType