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 00142 //ETX 00143 00144 class VTKCOMMONCORE_EXPORT vtkConditionVariable : public vtkObject 00145 { 00146 public: 00147 static vtkConditionVariable* New(); 00148 vtkTypeMacro(vtkConditionVariable,vtkObject); 00149 void PrintSelf( ostream& os, vtkIndent indent ); 00150 00152 void Signal(); 00153 00155 void Broadcast(); 00156 00165 int Wait( vtkMutexLock* mutex ); 00166 00167 protected: 00168 vtkConditionVariable() { } 00169 00170 //BTX 00171 vtkSimpleConditionVariable SimpleConditionVariable; 00172 //ETX 00173 00174 private: 00175 vtkConditionVariable( const vtkConditionVariable& ); // Not implemented. 00176 void operator = ( const vtkConditionVariable& ); // Not implemented. 00177 }; 00178 00179 //BTX 00180 inline void vtkConditionVariable::Signal() 00181 { 00182 this->SimpleConditionVariable.Signal(); 00183 } 00184 00185 inline void vtkConditionVariable::Broadcast() 00186 { 00187 this->SimpleConditionVariable.Broadcast(); 00188 } 00189 00190 inline int vtkConditionVariable::Wait( vtkMutexLock* lock ) 00191 { 00192 return this->SimpleConditionVariable.Wait( lock->SimpleMutexLock ); 00193 } 00194 //ETX 00195 00196 #endif // __vtkConditionVariable_h