00001 /*========================================================================= 00002 00003 Program: Visualization Toolkit 00004 Module: $RCSfile: vtkCriticalSection.h,v $ 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 =========================================================================*/ 00035 #ifndef __vtkCriticalSection_h 00036 #define __vtkCriticalSection_h 00037 00038 #include "vtkObject.h" 00039 00040 //BTX 00041 00042 #ifdef VTK_USE_SPROC 00043 #include <abi_mutex.h> // Needed for sproc implementation of mutex 00044 typedef abilock_t vtkCritSecType; 00045 #endif 00046 00047 #if defined(VTK_USE_PTHREADS) || defined(VTK_HP_PTHREADS) 00048 #include <pthread.h> // Needed for pthreads implementation of mutex 00049 typedef pthread_mutex_t vtkCritSecType; 00050 #endif 00051 00052 #ifdef VTK_USE_WIN32_THREADS 00053 # include "vtkWindows.h" // Needed for win32 implementation of mutex 00054 typedef CRITICAL_SECTION vtkCritSecType; 00055 #endif 00056 00057 #ifndef VTK_USE_SPROC 00058 #ifndef VTK_USE_PTHREADS 00059 #ifndef VTK_USE_WIN32_THREADS 00060 typedef int vtkCritSecType; 00061 #endif 00062 #endif 00063 #endif 00064 00065 // Critical Section object that is not a vtkObject. 00066 class VTK_COMMON_EXPORT vtkSimpleCriticalSection 00067 { 00068 public: 00069 // Default cstor 00070 vtkSimpleCriticalSection() 00071 { 00072 this->Init(); 00073 } 00074 // Construct object locked if isLocked is different from 0 00075 vtkSimpleCriticalSection(int isLocked) 00076 { 00077 this->Init(); 00078 if(isLocked) 00079 { 00080 this->Lock(); 00081 } 00082 } 00083 // Destructor 00084 virtual ~vtkSimpleCriticalSection(); 00085 00086 // Default vtkObject API 00087 static vtkSimpleCriticalSection *New(); 00088 void Delete() 00089 { 00090 delete this; 00091 } 00092 00093 void Init(); 00094 00096 void Lock(); 00097 00099 void Unlock(); 00100 00101 protected: 00102 vtkCritSecType CritSec; 00103 }; 00104 00105 //ETX 00106 00107 class VTK_COMMON_EXPORT vtkCriticalSection : public vtkObject 00108 { 00109 public: 00110 static vtkCriticalSection *New(); 00111 00112 vtkTypeRevisionMacro(vtkCriticalSection,vtkObject); 00113 void PrintSelf(ostream& os, vtkIndent indent); 00114 00116 void Lock(); 00117 00119 void Unlock(); 00120 00121 protected: 00122 vtkSimpleCriticalSection SimpleCriticalSection; 00123 vtkCriticalSection() {} 00124 ~vtkCriticalSection() {} 00125 00126 private: 00127 vtkCriticalSection(const vtkCriticalSection&); // Not implemented. 00128 void operator=(const vtkCriticalSection&); // Not implemented. 00129 }; 00130 00131 00132 inline void vtkCriticalSection::Lock() 00133 { 00134 this->SimpleCriticalSection.Lock(); 00135 } 00136 00137 inline void vtkCriticalSection::Unlock() 00138 { 00139 this->SimpleCriticalSection.Unlock(); 00140 } 00141 00142 #endif