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)
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_USE_WIN32_THREADS
105 typedef int vtkConditionType;
106 #endif
107 #endif
108 
109 // Condition variable that is not a vtkObject.
110 class VTKCOMMONCORE_EXPORT vtkSimpleConditionVariable
111 {
112 public:
115 
116  static vtkSimpleConditionVariable* New();
117 
118  void Delete() { delete this; }
119 
123  void Signal();
124 
128  void Broadcast();
129 
139  int Wait( vtkSimpleMutexLock& mutex );
140 
141 protected:
143 
144 private:
146  vtkSimpleConditionVariable& operator=(const vtkSimpleConditionVariable& rhs) = delete;
147 };
148 
149 class VTKCOMMONCORE_EXPORT vtkConditionVariable : public vtkObject
150 {
151 public:
152  static vtkConditionVariable* New();
154  void PrintSelf( ostream& os, vtkIndent indent ) override;
155 
159  void Signal();
160 
164  void Broadcast();
165 
175  int Wait( vtkMutexLock* mutex );
176 
177 protected:
179 
181 
182 private:
183  vtkConditionVariable( const vtkConditionVariable& ) = delete;
184  void operator = ( const vtkConditionVariable& ) = delete;
185 };
186 
188 {
189  this->SimpleConditionVariable.Signal();
190 }
191 
193 {
194  this->SimpleConditionVariable.Broadcast();
195 }
196 
198 {
199  return this->SimpleConditionVariable.Wait( lock->SimpleMutexLock );
200 }
201 
202 #endif // vtkConditionVariable_h
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.
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:102
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:80