VTK
|
00001 /*========================================================================= 00002 00003 Program: Visualization Toolkit 00004 Module: vtkConditionVariable.h 00005 00006 Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen 00007 All rights reserved. 00008 See Copyright.txt or http://www.kitware.com/Copyright.htm for details. 00009 00010 This software is distributed WITHOUT ANY WARRANTY; without even 00011 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 00012 PURPOSE. See the above copyright notice for more information. 00013 00014 =========================================================================*/ 00032 #ifndef vtkConditionVariable_h 00033 #define vtkConditionVariable_h 00034 00035 #include "vtkCommonCoreModule.h" // For export macro 00036 #include "vtkObject.h" 00037 00038 #include "vtkMutexLock.h" // Need for friend access to vtkSimpleMutexLock 00039 00040 //BTX 00041 #if defined(VTK_USE_PTHREADS) || defined(VTK_HP_PTHREADS) 00042 # include <pthread.h> // Need POSIX thread implementation of mutex (even win32 provides mutexes) 00043 typedef pthread_cond_t vtkConditionType; 00044 #endif 00045 00046 00047 // Typically a top level windows application sets _WIN32_WINNT. If it is not set we set it to 00048 // 0x0501 (Windows XP) 00049 #ifdef VTK_USE_WIN32_THREADS 00050 # ifndef _WIN32_WINNT 00051 # define _WIN32_WINNT 0x0501 // 0x0501 means target Windows XP or later 00052 # endif 00053 # include "vtkWindows.h" // Needed for win32 CRITICAL_SECTION, HANDLE, etc. 00054 #endif 00055 00056 #ifdef VTK_USE_WIN32_THREADS 00057 #if 1 00058 typedef struct 00059 { 00060 // Number of threads waiting on condition. 00061 int WaitingThreadCount; 00062 00063 // Lock for WaitingThreadCount 00064 CRITICAL_SECTION WaitingThreadCountCritSec; 00065 00066 // Semaphore to block threads waiting for the condition to change. 00067 vtkWindowsHANDLE Semaphore; 00068 00069 // An event used to wake up thread(s) waiting on the semaphore 00070 // when pthread_cond_signal or pthread_cond_broadcast is called. 00071 vtkWindowsHANDLE DoneWaiting; 00072 00073 // Was pthread_cond_broadcast called? 00074 size_t WasBroadcast; 00075 } pthread_cond_t; 00076 00077 typedef pthread_cond_t vtkConditionType; 00078 # else // 0 00079 typedef struct 00080 { 00081 // Number of threads waiting on condition. 00082 int WaitingThreadCount; 00083 00084 // Lock for WaitingThreadCount 00085 CRITICAL_SECTION WaitingThreadCountCritSec; 00086 00087 // Number of threads to release when pthread_cond_broadcast() 00088 // or pthread_cond_signal() is called. 00089 int ReleaseCount; 00090 00091 // Used to prevent one thread from decrementing ReleaseCount all 00092 // by itself instead of letting others respond. 00093 int NotifyCount; 00094 00095 // A manual-reset event that's used to block and release waiting threads. 00096 vtkWindowsHANDLE Event; 00097 } pthread_cond_t; 00098 00099 typedef pthread_cond_t vtkConditionType; 00100 # endif // 0 00101 #endif // VTK_USE_WIN32_THREADS 00102 00103 #ifndef VTK_USE_PTHREADS 00104 #ifndef VTK_HP_PTHREADS 00105 #ifndef VTK_USE_WIN32_THREADS 00106 typedef int vtkConditionType; 00107 #endif 00108 #endif 00109 #endif 00110 00111 // Condition variable that is not a vtkObject. 00112 class VTKCOMMONCORE_EXPORT vtkSimpleConditionVariable 00113 { 00114 public: 00115 vtkSimpleConditionVariable(); 00116 ~vtkSimpleConditionVariable(); 00117 00118 static vtkSimpleConditionVariable* New(); 00119 00120 void Delete() { delete this; } 00121 00123 void Signal(); 00124 00126 void Broadcast(); 00127 00136 int Wait( vtkSimpleMutexLock& mutex ); 00137 00138 protected: 00139 vtkConditionType ConditionVariable; 00140 00141 private: 00142 vtkSimpleConditionVariable(const vtkSimpleConditionVariable& other); // no copy constructor 00143 vtkSimpleConditionVariable& operator=(const vtkSimpleConditionVariable& rhs); // no copy assignment 00144 }; 00145 00146 //ETX 00147 00148 class VTKCOMMONCORE_EXPORT vtkConditionVariable : public vtkObject 00149 { 00150 public: 00151 static vtkConditionVariable* New(); 00152 vtkTypeMacro(vtkConditionVariable,vtkObject); 00153 void PrintSelf( ostream& os, vtkIndent indent ); 00154 00156 void Signal(); 00157 00159 void Broadcast(); 00160 00169 int Wait( vtkMutexLock* mutex ); 00170 00171 protected: 00172 vtkConditionVariable() { } 00173 00174 //BTX 00175 vtkSimpleConditionVariable SimpleConditionVariable; 00176 //ETX 00177 00178 private: 00179 vtkConditionVariable( const vtkConditionVariable& ); // Not implemented. 00180 void operator = ( const vtkConditionVariable& ); // Not implemented. 00181 }; 00182 00183 //BTX 00184 inline void vtkConditionVariable::Signal() 00185 { 00186 this->SimpleConditionVariable.Signal(); 00187 } 00188 00189 inline void vtkConditionVariable::Broadcast() 00190 { 00191 this->SimpleConditionVariable.Broadcast(); 00192 } 00193 00194 inline int vtkConditionVariable::Wait( vtkMutexLock* lock ) 00195 { 00196 return this->SimpleConditionVariable.Wait( lock->SimpleMutexLock ); 00197 } 00198 //ETX 00199 00200 #endif // vtkConditionVariable_h