VTK
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 "vtkObject.h"
38 
39 #include "vtkMutexLock.h" // Need for friend access to vtkSimpleMutexLock
40 
41 #if defined(VTK_USE_PTHREADS) || defined(VTK_HP_PTHREADS)
42 # include <pthread.h> // Need POSIX thread implementation of mutex (even win32 provides mutexes)
43 typedef pthread_cond_t vtkConditionType;
44 #endif
45 
46 
47 // Typically a top level windows application sets _WIN32_WINNT. If it is not set we set it to
48 // 0x0501 (Windows XP)
49 #ifdef VTK_USE_WIN32_THREADS
50 # ifndef _WIN32_WINNT
51 # define _WIN32_WINNT 0x0501 // 0x0501 means target Windows XP or later
52 # endif
53 # include "vtkWindows.h" // Needed for win32 CRITICAL_SECTION, HANDLE, etc.
54 #endif
55 
56 #ifdef VTK_USE_WIN32_THREADS
57 #if 1
58 typedef struct
59 {
60  // Number of threads waiting on condition.
61  int WaitingThreadCount;
62 
63  // Lock for WaitingThreadCount
64  CRITICAL_SECTION WaitingThreadCountCritSec;
65 
66  // Semaphore to block threads waiting for the condition to change.
67  vtkWindowsHANDLE Semaphore;
68 
69  // An event used to wake up thread(s) waiting on the semaphore
70  // when pthread_cond_signal or pthread_cond_broadcast is called.
71  vtkWindowsHANDLE DoneWaiting;
72 
73  // Was pthread_cond_broadcast called?
74  size_t WasBroadcast;
75 } pthread_cond_t;
76 
77 typedef pthread_cond_t vtkConditionType;
78 # else // 0
79 typedef struct
80 {
81  // Number of threads waiting on condition.
82  int WaitingThreadCount;
83 
84  // Lock for WaitingThreadCount
85  CRITICAL_SECTION WaitingThreadCountCritSec;
86 
87  // Number of threads to release when pthread_cond_broadcast()
88  // or pthread_cond_signal() is called.
89  int ReleaseCount;
90 
91  // Used to prevent one thread from decrementing ReleaseCount all
92  // by itself instead of letting others respond.
93  int NotifyCount;
94 
95  // A manual-reset event that's used to block and release waiting threads.
96  vtkWindowsHANDLE Event;
97 } pthread_cond_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_HP_PTHREADS
105 #ifndef VTK_USE_WIN32_THREADS
106 typedef int vtkConditionType;
107 #endif
108 #endif
109 #endif
110 
111 // Condition variable that is not a vtkObject.
112 class VTKCOMMONCORE_EXPORT vtkSimpleConditionVariable
113 {
114 public:
117 
118  static vtkSimpleConditionVariable* New();
119 
120  void Delete() { delete this; }
121 
125  void Signal();
126 
130  void Broadcast();
131 
141  int Wait( vtkSimpleMutexLock& mutex );
142 
143 protected:
145 
146 private:
147  vtkSimpleConditionVariable(const vtkSimpleConditionVariable& other) VTK_DELETE_FUNCTION;
148  vtkSimpleConditionVariable& operator=(const vtkSimpleConditionVariable& rhs) VTK_DELETE_FUNCTION;
149 };
150 
151 class VTKCOMMONCORE_EXPORT vtkConditionVariable : public vtkObject
152 {
153 public:
154  static vtkConditionVariable* New();
156  void PrintSelf( ostream& os, vtkIndent indent ) VTK_OVERRIDE;
157 
161  void Signal();
162 
166  void Broadcast();
167 
177  int Wait( vtkMutexLock* mutex );
178 
179 protected:
181 
183 
184 private:
185  vtkConditionVariable( const vtkConditionVariable& ) VTK_DELETE_FUNCTION;
186  void operator = ( const vtkConditionVariable& ) VTK_DELETE_FUNCTION;
187 };
188 
189 inline void vtkConditionVariable::Signal()
190 {
191  this->SimpleConditionVariable.Signal();
192 }
193 
195 {
196  this->SimpleConditionVariable.Broadcast();
197 }
198 
200 {
201  return this->SimpleConditionVariable.Wait( lock->SimpleMutexLock );
202 }
203 
204 #endif // vtkConditionVariable_h
void Signal()
Wake one thread waiting for the condition to change.
mutual exclusion locking class
abstract base class for most VTK objects
Definition: vtkObject.h:59
void PrintSelf(ostream &os, vtkIndent indent) override
Methods invoked by print to print information about the object including superclasses.
int Wait(vtkSimpleMutexLock &mutex)
Wait for the condition to change.
void Broadcast()
Wake all threads 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:109
int vtkConditionType
static vtkObject * New()
Create an object with Debug turned off, modified time initialized to zero, and reference counting on...
vtkSimpleConditionVariable SimpleConditionVariable
mutual exclusion locking class
Definition: vtkMutexLock.h:87