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 =========================================================================*/
32 #ifndef vtkConditionVariable_h
33 #define vtkConditionVariable_h
34 
35 #include "vtkCommonCoreModule.h" // For export macro
36 #include "vtkObject.h"
37 
38 #include "vtkMutexLock.h" // Need for friend access to vtkSimpleMutexLock
39 
40 //BTX
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.
113 {
114 public:
117 
118  static vtkSimpleConditionVariable* New();
119 
120  void Delete() { delete this; }
121 
123  void Signal();
124 
126  void Broadcast();
127 
136  int Wait( vtkSimpleMutexLock& mutex );
137 
138 protected:
140 
141 private:
142  vtkSimpleConditionVariable(const vtkSimpleConditionVariable& other); // no copy constructor
143  vtkSimpleConditionVariable& operator=(const vtkSimpleConditionVariable& rhs); // no copy assignment
144 };
145 
146 //ETX
147 
149 {
150 public:
151  static vtkConditionVariable* New();
153  void PrintSelf( ostream& os, vtkIndent indent );
154 
156  void Signal();
157 
159  void Broadcast();
160 
169  int Wait( vtkMutexLock* mutex );
170 
171 protected:
173 
174  //BTX
176  //ETX
177 
178 private:
179  vtkConditionVariable( const vtkConditionVariable& ); // Not implemented.
180  void operator = ( const vtkConditionVariable& ); // Not implemented.
181 };
182 
183 //BTX
185 {
187 }
188 
190 {
192 }
193 
195 {
196  return this->SimpleConditionVariable.Wait( lock->SimpleMutexLock );
197 }
198 //ETX
199 
200 #endif // vtkConditionVariable_h
mutual exclusion locking class
abstract base class for most VTK objects
Definition: vtkObject.h:61
#define VTKCOMMONCORE_EXPORT
int Wait(vtkSimpleMutexLock &mutex)
virtual void PrintSelf(ostream &os, vtkIndent indent)
a simple class to control print indentation
Definition: vtkIndent.h:38
int Wait(vtkMutexLock *mutex)
vtkSimpleMutexLock SimpleMutexLock
Definition: vtkMutexLock.h:105
int vtkConditionType
static vtkObject * New()
vtkSimpleConditionVariable SimpleConditionVariable
mutual exclusion locking class
Definition: vtkMutexLock.h:86