VTK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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.
112 class VTKCOMMONCORE_EXPORT vtkSimpleConditionVariable
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 
142 //ETX
143 
144 class VTKCOMMONCORE_EXPORT vtkConditionVariable : public vtkObject
145 {
146 public:
147  static vtkConditionVariable* New();
149  void PrintSelf( ostream& os, vtkIndent indent );
150 
152  void Signal();
153 
155  void Broadcast();
156 
165  int Wait( vtkMutexLock* mutex );
166 
167 protected:
169 
170  //BTX
172  //ETX
173 
174 private:
175  vtkConditionVariable( const vtkConditionVariable& ); // Not implemented.
176  void operator = ( const vtkConditionVariable& ); // Not implemented.
177 };
178 
179 //BTX
181 {
183 }
184 
186 {
188 }
189 
191 {
192  return this->SimpleConditionVariable.Wait( lock->SimpleMutexLock );
193 }
194 //ETX
195 
196 #endif // __vtkConditionVariable_h
mutual exclusion locking class
abstract base class for most VTK objects
Definition: vtkObject.h:61
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:101
int vtkConditionType
static vtkObject * New()
vtkSimpleConditionVariable SimpleConditionVariable
mutual exclusion locking class
Definition: vtkMutexLock.h:82