| 1 |
|
/*========================================================================= |
| 2 |
|
|
| 3 |
|
Program: Insight Segmentation & Registration Toolkit |
| 4 |
|
Module: $RCSfile: itkMutexLock.h.html,v $ |
| 5 |
|
Language: C++ |
| 6 |
|
Date: $Date: 2006/01/17 19:15:42 $ |
| 7 |
|
Version: $Revision: 1.4 $ |
| 8 |
|
|
| 9 |
|
Copyright (c) Insight Software Consortium. All rights reserved. |
| 10 |
|
See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm for details. |
| 11 |
|
|
| 12 |
|
Portions of this code are covered under the VTK copyright. |
| 13 |
|
See VTKCopyright.txt or http://www.kitware.com/VTKCopyright.htm for details. |
| 14 |
|
|
| 15 |
|
This software is distributed WITHOUT ANY WARRANTY; without even |
| 16 |
|
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 17 |
IND |
*****PURPOSE. See the above copyright notices for more information. |
| 18 |
|
|
| 19 |
|
=========================================================================*/ |
| 20 |
|
#ifndef __itkMutexLock_h |
| 21 |
|
#define __itkMutexLock_h |
| 22 |
|
|
| 23 |
|
#include "itkObject.h" |
| 24 |
|
#include "itkObjectFactory.h" |
| 25 |
|
|
| 26 |
|
#ifdef ITK_USE_SPROC |
| 27 |
|
#include <abi_mutex.h> |
| 28 |
|
#endif |
| 29 |
|
|
| 30 |
|
#ifdef ITK_USE_PTHREADS |
| 31 |
|
#include <pthread.h> |
| 32 |
|
#endif |
| 33 |
|
|
| 34 |
|
#ifdef ITK_USE_WIN32_THREADS |
| 35 |
|
#include "itkWindows.h" |
| 36 |
|
#endif |
| 37 |
|
|
| 38 |
|
namespace itk |
| 39 |
|
{ |
| 40 |
|
|
| 41 |
|
#ifdef ITK_USE_SPROC |
| 42 |
|
typedef abilock_t MutexType; |
| 43 |
|
#endif |
| 44 |
|
|
| 45 |
|
#ifdef ITK_USE_PTHREADS |
| 46 |
|
typedef pthread_mutex_t MutexType; |
| 47 |
|
#endif |
| 48 |
|
|
| 49 |
|
#ifdef ITK_USE_WIN32_THREADS |
| 50 |
|
typedef HANDLE MutexType; |
| 51 |
|
#endif |
| 52 |
|
|
| 53 |
|
#ifndef ITK_USE_SPROC |
| 54 |
|
#ifndef ITK_USE_PTHREADS |
| 55 |
|
#ifndef ITK_USE_WIN32_THREADS |
| 56 |
|
typedef int MutexType; |
| 57 |
|
#endif |
| 58 |
|
#endif |
| 59 |
|
#endif |
| 60 |
|
|
| 61 |
|
/** \class SimpleMutexLock |
| 62 |
|
* \brief Simple mutual exclusion locking class. |
| 63 |
|
|
| 64 |
|
* SimpleMutexLock allows the locking of variables which are accessed |
| 65 |
|
* through different threads. This header file also defines |
| 66 |
|
* SimpleMutexLock which is not a subclass of Object. |
| 67 |
|
* |
| 68 |
|
* \ingroup OSSystemObjects |
| 69 |
|
*/ |
| 70 |
|
class ITKCommon_EXPORT SimpleMutexLock |
| 71 |
|
{ |
| 72 |
|
public: |
| 73 |
|
/** Standard class typedefs. */ |
| 74 |
|
typedef SimpleMutexLock Self; |
| 75 |
|
|
| 76 |
|
/** Constructor and destructor left public purposely. */ |
| 77 |
|
SimpleMutexLock(); |
| 78 |
|
virtual ~SimpleMutexLock(); |
| 79 |
|
|
| 80 |
|
/** Methods for creation and destruction through the object factory. */ |
| 81 |
|
static SimpleMutexLock *New(); |
| 82 |
|
void Delete() {delete this;} |
| 83 |
|
|
| 84 |
|
/** Used for debugging and other run-time purposes. */ |
| 85 |
|
virtual const char *GetNameOfClass() {return "itkSimpleMutexLock";}; |
| 86 |
|
|
| 87 |
|
/** Lock the MutexLock. */ |
| 88 |
|
void Lock( void ); |
| 89 |
|
|
| 90 |
|
/** Unlock the MutexLock. */ |
| 91 |
|
void Unlock( void ); |
| 92 |
|
|
| 93 |
|
/** Access the MutexType member variable from outside this class */ |
| 94 |
|
MutexType& GetMutexLock() |
| 95 |
IND |
**{ |
| 96 |
|
return m_MutexLock; |
| 97 |
IND |
**} |
| 98 |
|
const MutexType GetMutexLock() const |
| 99 |
IND |
**{ |
| 100 |
|
return m_MutexLock; |
| 101 |
IND |
**} |
| 102 |
|
|
| 103 |
|
protected: |
| 104 |
|
MutexType m_MutexLock; |
| 105 |
|
}; |
| 106 |
|
|
| 107 |
|
/** \class MutexLock |
| 108 |
|
* \brief Mutual exclusion locking class. |
| 109 |
|
* |
| 110 |
|
* MutexLock allows the locking of variables which are accessed |
| 111 |
|
* through different threads. This header file also defines |
| 112 |
|
* SimpleMutexLock which is not a subclass of itkObject. |
| 113 |
|
* |
| 114 |
|
* \ingroup OSSystemObjects |
| 115 |
|
*/ |
| 116 |
|
class ITKCommon_EXPORT MutexLock : public Object |
| 117 |
|
{ |
| 118 |
|
public: |
| 119 |
|
/** Standard class typedefs. */ |
| 120 |
|
typedef MutexLock Self; |
| 121 |
TDA |
typedef Object Superclass; |
| 122 |
TDA |
typedef SmartPointer<Self> Pointer; |
| 123 |
TDA |
typedef SmartPointer<const Self> ConstPointer; |
| 124 |
|
|
| 125 |
|
/** Method for creation. */ |
| 126 |
|
itkNewMacro(Self); |
| 127 |
|
|
| 128 |
|
/** Run-time information. */ |
| 129 |
|
itkTypeMacro(MutexLock,Object); |
| 130 |
|
|
| 131 |
|
/** Lock the itkMutexLock. */ |
| 132 |
|
void Lock( void ); |
| 133 |
|
|
| 134 |
|
/** Unlock the MutexLock. */ |
| 135 |
|
void Unlock( void ); |
| 136 |
|
|
| 137 |
|
protected: |
| 138 |
|
MutexLock() {} |
| 139 |
|
~MutexLock() {} |
| 140 |
|
|
| 141 |
|
SimpleMutexLock m_SimpleMutexLock; |
| 142 |
|
void PrintSelf(std::ostream& os, Indent indent) const; |
| 143 |
|
|
| 144 |
|
private: |
| 145 |
|
MutexLock(const Self&); //purposely not implemented |
| 146 |
|
void operator=(const Self&); //purposely not implemented |
| 147 |
|
}; |
| 148 |
|
|
| 149 |
|
|
| 150 |
|
inline void MutexLock::Lock( void ) |
| 151 |
|
{ |
| 152 |
|
m_SimpleMutexLock.Lock(); |
| 153 |
|
} |
| 154 |
|
|
| 155 |
|
inline void MutexLock::Unlock( void ) |
| 156 |
|
{ |
| 157 |
|
m_SimpleMutexLock.Unlock(); |
| 158 |
|
} |
| 159 |
|
|
| 160 |
|
|
| 161 |
|
}//end itk namespace |
| 162 |
|
#endif |
| 163 |
|
|