VTK
dox/Common/vtkConditionVariable.h
Go to the documentation of this file.
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 "vtkObject.h"
00036 
00037 #include "vtkMutexLock.h" // Need for friend access to vtkSimpleMutexLock
00038 
00039 //BTX
00040 #if defined(VTK_USE_PTHREADS) || defined(VTK_HP_PTHREADS)
00041 #  include <pthread.h> // Need POSIX thread implementation of mutex (even win32 provides mutexes)
00042 typedef pthread_cond_t vtkConditionType;
00043 #endif
00044 
00045 
00046 // Typically a top level windows application set _WIN32_WINNT. If it is not set we set it to
00047 // 0x0500 (Windows 2000) because visual studio 6...
00048 #ifdef VTK_USE_WIN32_THREADS
00049 #  ifndef _WIN32_WINNT
00050 #    define _WIN32_WINNT 0x0500 // Minimum windows version supported is Windows 2000.
00051 #  endif
00052 #  include "vtkWindows.h" // Needed for win32 CRITICAL_SECTION, HANDLE, etc.
00053 #endif
00054 
00055 #ifdef VTK_USE_WIN32_THREADS
00056 #if 1
00057 typedef struct
00058 {
00059   // Number of threads waiting on condition.
00060   int WaitingThreadCount;
00061 
00062   // Lock for WaitingThreadCount
00063   CRITICAL_SECTION WaitingThreadCountCritSec;
00064 
00065   // Semaphore to block threads waiting for the condition to change.
00066   vtkWindowsHANDLE Semaphore;
00067 
00068   // An event used to wake up thread(s) waiting on the semaphore
00069   // when pthread_cond_signal or pthread_cond_broadcast is called.
00070   vtkWindowsHANDLE DoneWaiting;
00071 
00072   // Was pthread_cond_broadcast called?
00073   size_t WasBroadcast;
00074 } pthread_cond_t;
00075 
00076 typedef pthread_cond_t vtkConditionType;
00077 #  else // 0
00078 typedef struct
00079 {
00080   // Number of threads waiting on condition.
00081   int WaitingThreadCount;
00082 
00083   // Lock for WaitingThreadCount
00084   CRITICAL_SECTION WaitingThreadCountCritSec;
00085 
00086   // Number of threads to release when pthread_cond_broadcast()
00087   // or pthread_cond_signal() is called. 
00088   int ReleaseCount;
00089 
00090   // Used to prevent one thread from decrementing ReleaseCount all
00091   // by itself instead of letting others respond.
00092   int NotifyCount;
00093 
00094   // A manual-reset event that's used to block and release waiting threads. 
00095   vtkWindowsHANDLE Event;
00096 } pthread_cond_t;
00097 
00098 typedef pthread_cond_t vtkConditionType;
00099 #  endif // 0
00100 #endif // VTK_USE_WIN32_THREADS
00101 
00102 #ifndef VTK_USE_PTHREADS
00103 #ifndef VTK_HP_PTHREADS
00104 #ifndef VTK_USE_WIN32_THREADS
00105 typedef int vtkConditionType;
00106 #endif
00107 #endif
00108 #endif
00109 
00110 // Condition variable that is not a vtkObject.
00111 class VTK_COMMON_EXPORT vtkSimpleConditionVariable
00112 {
00113 public:
00114   vtkSimpleConditionVariable();
00115   ~vtkSimpleConditionVariable();
00116 
00117   static vtkSimpleConditionVariable* New();
00118 
00119   void Delete() { delete this; }
00120   
00122   void Signal();
00123 
00125   void Broadcast();
00126 
00135   int Wait( vtkSimpleMutexLock& mutex );
00136 
00137 protected:
00138   vtkConditionType   ConditionVariable;
00139 };
00140 
00141 //ETX
00142 
00143 class VTK_COMMON_EXPORT vtkConditionVariable : public vtkObject
00144 {
00145 public:
00146   static vtkConditionVariable* New();
00147   vtkTypeMacro(vtkConditionVariable,vtkObject);
00148   void PrintSelf( ostream& os, vtkIndent indent );
00149   
00151   void Signal();
00152 
00154   void Broadcast();
00155 
00164   int Wait( vtkMutexLock* mutex );
00165 
00166 protected:
00167   vtkConditionVariable() { }
00168 
00169   //BTX
00170   vtkSimpleConditionVariable SimpleConditionVariable;
00171   //ETX
00172 
00173 private:
00174   vtkConditionVariable( const vtkConditionVariable& ); // Not implemented.
00175   void operator = ( const vtkConditionVariable& ); // Not implemented.
00176 };
00177 
00178 //BTX
00179 inline void vtkConditionVariable::Signal()
00180 {
00181   this->SimpleConditionVariable.Signal();
00182 }
00183 
00184 inline void vtkConditionVariable::Broadcast()
00185 {
00186   this->SimpleConditionVariable.Broadcast();
00187 }
00188 
00189 inline int vtkConditionVariable::Wait( vtkMutexLock* lock )
00190 {
00191   return this->SimpleConditionVariable.Wait( lock->SimpleMutexLock );
00192 }
00193 //ETX
00194 
00195 #endif // __vtkConditionVariable_h