1 /*=========================================================================
3 Program: Visualization Toolkit
4 Module: $RCSfile: vtkCriticalSection.h,v $
6 Date: $Date: 2003/01/29 21:14:52 $
7 Version: $Revision: 1.15 $
9 Copyright (c) 1993-2002 Ken Martin, Will Schroeder, Bill Lorensen
11 See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
13 This software is distributed WITHOUT ANY WARRANTY; without even
14 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 PURPOSE. See the above copyright notice for more information.
17 =========================================================================*/
18 // .NAME vtkCriticalSection - critical section locking class
19 // .SECTION Description
20 // vtkCriticalSection allows the locking of variables which are accessed
21 // through different threads. This header file also defines
22 // vtkSimpleCriticalSection which is not a subclass of vtkObject.
23 // The API is identical to that of vtkMutexLock, and the behavior is
24 // identical as well, except on Windows 9x/NT platforms. The only difference
25 // on these platforms is that vtkMutexLock is more flexible, in that
26 // it works across processes as well as across threads, but also costs
27 // more, in that it evokes a 600-cycle x86 ring transition. The
28 // vtkCriticalSection provides a higher-performance equivalent (on
29 // Windows) but won't work across processes. Since it is unclear how,
30 // in vtk, an object at the vtk level can be shared across processes
31 // in the first place, one should use vtkCriticalSection unless one has
32 // a very good reason to use vtkMutexLock. If higher-performance equivalents
33 // for non-Windows platforms (Irix, SunOS, etc) are discovered, they
34 // should replace the implementations in this class
36 #ifndef __vtkCriticalSection_h
37 #define __vtkCriticalSection_h
39 #include "vtkObject.h"
44 #include <abi_mutex.h> // Needed for sproc implementation of mutex
45 typedef abilock_t vtkCritSecType;
48 #if defined(VTK_USE_PTHREADS) || defined(VTK_HP_PTHREADS)
49 #include <pthread.h> // Needed for pthreads implementation of mutex
50 typedef pthread_mutex_t vtkCritSecType;
53 #ifdef VTK_USE_WIN32_THREADS
54 #include <winbase.h> // Needed for win32 implementation of mutex
55 typedef CRITICAL_SECTION vtkCritSecType;
59 #ifndef VTK_USE_PTHREADS
60 #ifndef VTK_USE_WIN32_THREADS
61 typedef int vtkCritSecType;
66 // Critical Section object that is not a vtkObject.
67 class VTK_COMMON_EXPORT vtkSimpleCriticalSection
70 vtkSimpleCriticalSection()
75 vtkSimpleCriticalSection(int isLocked)
86 virtual ~vtkSimpleCriticalSection();
88 static vtkSimpleCriticalSection *New();
90 // What's the point of these (here and in MutexLock)? This class
91 // is not part of the hierarchy!! -CRV
92 virtual const char *GetClassName() {return "vtkSimpleCriticalSection";};
93 virtual int IsA(const char *name);
94 static vtkSimpleCriticalSection *SafeDownCast(vtkSimpleCriticalSection *o);
96 void Delete() {delete this;}
99 // Lock the vtkCriticalSection
103 // Unlock the vtkCriticalSection
107 vtkCritSecType CritSec;
112 class VTK_COMMON_EXPORT vtkCriticalSection : public vtkObject
115 static vtkCriticalSection *New();
117 vtkTypeRevisionMacro(vtkCriticalSection,vtkObject);
118 void PrintSelf(ostream& os, vtkIndent indent);
121 // Lock the vtkCriticalSection
125 // Unlock the vtkCriticalSection
129 vtkSimpleCriticalSection SimpleCriticalSection;
130 vtkCriticalSection() {};
132 vtkCriticalSection(const vtkCriticalSection&); // Not implemented.
133 void operator=(const vtkCriticalSection&); // Not implemented.
137 inline void vtkCriticalSection::Lock( void )
139 this->SimpleCriticalSection.Lock();
142 inline void vtkCriticalSection::Unlock( void )
144 this->SimpleCriticalSection.Unlock();