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