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 =========================================================================*/
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) || defined(VTK_HP_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 
44 // Typically a top level windows application sets _WIN32_WINNT. If it is not set we set it to
45 // 0x0501 (Windows XP)
46 #ifdef VTK_USE_WIN32_THREADS
47 # ifndef _WIN32_WINNT
48 # define _WIN32_WINNT 0x0501 // 0x0501 means target Windows XP or later
49 # endif
50 # include "vtkWindows.h" // Needed for win32 CRITICAL_SECTION, HANDLE, etc.
51 #endif
52 
53 #ifdef VTK_USE_WIN32_THREADS
54 #if 1
55 typedef struct
56 {
57  // Number of threads waiting on condition.
58  int WaitingThreadCount;
59 
60  // Lock for WaitingThreadCount
61  CRITICAL_SECTION WaitingThreadCountCritSec;
62 
63  // Semaphore to block threads waiting for the condition to change.
64  vtkWindowsHANDLE Semaphore;
65 
66  // An event used to wake up thread(s) waiting on the semaphore
67  // when pthread_cond_signal or pthread_cond_broadcast is called.
68  vtkWindowsHANDLE DoneWaiting;
69 
70  // Was pthread_cond_broadcast called?
71  size_t WasBroadcast;
72 } pthread_cond_t;
73 
74 typedef pthread_cond_t vtkConditionType;
75 # else // 0
76 typedef struct
77 {
78  // Number of threads waiting on condition.
79  int WaitingThreadCount;
80 
81  // Lock for WaitingThreadCount
82  CRITICAL_SECTION WaitingThreadCountCritSec;
83 
84  // Number of threads to release when pthread_cond_broadcast()
85  // or pthread_cond_signal() is called.
86  int ReleaseCount;
87 
88  // Used to prevent one thread from decrementing ReleaseCount all
89  // by itself instead of letting others respond.
90  int NotifyCount;
91 
92  // A manual-reset event that's used to block and release waiting threads.
93  vtkWindowsHANDLE Event;
94 } pthread_cond_t;
95 
96 typedef pthread_cond_t vtkConditionType;
97 # endif // 0
98 #endif // VTK_USE_WIN32_THREADS
99 
100 #ifndef VTK_USE_PTHREADS
101 #ifndef VTK_HP_PTHREADS
102 #ifndef VTK_USE_WIN32_THREADS
103 typedef int vtkConditionType;
104 #endif
105 #endif
106 #endif
107 
108 // Condition variable that is not a vtkObject.
109 class VTKCOMMONCORE_EXPORT vtkSimpleConditionVariable
110 {
111 public:
114 
115  static vtkSimpleConditionVariable* New();
116 
117  void Delete() { delete this; }
118 
122  void Signal();
123 
127  void Broadcast();
128 
138  int Wait( vtkSimpleMutexLock& mutex );
139 
140 protected:
142 
143 private:
144  vtkSimpleConditionVariable(const vtkSimpleConditionVariable& other) VTK_DELETE_FUNCTION;
145  vtkSimpleConditionVariable& operator=(const vtkSimpleConditionVariable& rhs) VTK_DELETE_FUNCTION;
146 };
147 
148 class VTKCOMMONCORE_EXPORT vtkConditionVariable : public vtkObject
149 {
150 public:
151  static vtkConditionVariable* New();
153  void PrintSelf( ostream& os, vtkIndent indent ) VTK_OVERRIDE;
154 
158  void Signal();
159 
163  void Broadcast();
164 
174  int Wait( vtkMutexLock* mutex );
175 
176 protected:
178 
180 
181 private:
182  vtkConditionVariable( const vtkConditionVariable& ) VTK_DELETE_FUNCTION;
183  void operator = ( const vtkConditionVariable& ) VTK_DELETE_FUNCTION;
184 };
185 
187 {
189 }
190 
192 {
194 }
195 
197 {
198  return this->SimpleConditionVariable.Wait( lock->SimpleMutexLock );
199 }
200 
201 #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:53
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.
void Signal()
Wake one thread waiting for the condition to change.
a simple class to control print indentation
Definition: vtkIndent.h:33
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:106
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:84