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 "vtkObject.h"
36 
37 #include "vtkMutexLock.h" // Need for friend access to vtkSimpleMutexLock
38 
39 //BTX
40 #if defined(VTK_USE_PTHREADS) || defined(VTK_HP_PTHREADS)
41 # include <pthread.h> // Need POSIX thread implementation of mutex (even win32 provides mutexes)
42 typedef pthread_cond_t vtkConditionType;
43 #endif
44 
45 
46 // Typically a top level windows application set _WIN32_WINNT. If it is not set we set it to
47 // 0x0500 (Windows 2000) because visual studio 6...
48 #ifdef VTK_USE_WIN32_THREADS
49 # ifndef _WIN32_WINNT
50 # define _WIN32_WINNT 0x0500 // Minimum windows version supported is Windows 2000.
51 # endif
52 # include "vtkWindows.h" // Needed for win32 CRITICAL_SECTION, HANDLE, etc.
53 #endif
54 
55 #ifdef VTK_USE_WIN32_THREADS
56 #if 1
57 typedef struct
58 {
59  // Number of threads waiting on condition.
60  int WaitingThreadCount;
61 
62  // Lock for WaitingThreadCount
63  CRITICAL_SECTION WaitingThreadCountCritSec;
64 
65  // Semaphore to block threads waiting for the condition to change.
66  vtkWindowsHANDLE Semaphore;
67 
68  // An event used to wake up thread(s) waiting on the semaphore
69  // when pthread_cond_signal or pthread_cond_broadcast is called.
70  vtkWindowsHANDLE DoneWaiting;
71 
72  // Was pthread_cond_broadcast called?
73  size_t WasBroadcast;
74 } pthread_cond_t;
75 
76 typedef pthread_cond_t vtkConditionType;
77 # else // 0
78 typedef struct
79 {
80  // Number of threads waiting on condition.
81  int WaitingThreadCount;
82 
83  // Lock for WaitingThreadCount
84  CRITICAL_SECTION WaitingThreadCountCritSec;
85 
86  // Number of threads to release when pthread_cond_broadcast()
87  // or pthread_cond_signal() is called.
88  int ReleaseCount;
89 
90  // Used to prevent one thread from decrementing ReleaseCount all
91  // by itself instead of letting others respond.
92  int NotifyCount;
93 
94  // A manual-reset event that's used to block and release waiting threads.
95  vtkWindowsHANDLE Event;
96 } pthread_cond_t;
97 
98 typedef pthread_cond_t vtkConditionType;
99 # endif // 0
100 #endif // VTK_USE_WIN32_THREADS
101 
102 #ifndef VTK_USE_PTHREADS
103 #ifndef VTK_HP_PTHREADS
104 #ifndef VTK_USE_WIN32_THREADS
105 typedef int vtkConditionType;
106 #endif
107 #endif
108 #endif
109 
110 // Condition variable that is not a vtkObject.
112 {
113 public:
116 
117  static vtkSimpleConditionVariable* New();
118 
119  void Delete() { delete this; }
120 
122  void Signal();
123 
125  void Broadcast();
126 
135  int Wait( vtkSimpleMutexLock& mutex );
136 
137 protected:
139 };
140 
141 //ETX
142 
144 {
145 public:
146  static vtkConditionVariable* New();
148  void PrintSelf( ostream& os, vtkIndent indent );
149 
151  void Signal();
152 
154  void Broadcast();
155 
164  int Wait( vtkMutexLock* mutex );
165 
166 protected:
168 
169  //BTX
171  //ETX
172 
173 private:
174  vtkConditionVariable( const vtkConditionVariable& ); // Not implemented.
175  void operator = ( const vtkConditionVariable& ); // Not implemented.
176 };
177 
178 //BTX
180 {
182 }
183 
185 {
187 }
188 
190 {
191  return this->SimpleConditionVariable.Wait( lock->SimpleMutexLock );
192 }
193 //ETX
194 
195 #endif // __vtkConditionVariable_h
mutual exclusion locking class
abstract base class for most VTK objects
Definition: vtkObject.h:60
int Wait(vtkSimpleMutexLock &mutex)
virtual void PrintSelf(ostream &os, vtkIndent indent)
a simple class to control print indentation
Definition: vtkIndent.h:37
int Wait(vtkMutexLock *mutex)
#define VTK_COMMON_EXPORT
vtkSimpleMutexLock SimpleMutexLock
Definition: vtkMutexLock.h:100
int vtkConditionType
static vtkObject * New()
vtkSimpleConditionVariable SimpleConditionVariable
mutual exclusion locking class
Definition: vtkMutexLock.h:81