version 1.4.2.3, 2006/07/28 20:46:41
|
version 1.4.2.10, 2006/08/01 20:29:59
|
|
|
// | // |
//============================================================================== | //============================================================================== |
// | // |
// Author: Mike Day (mdday@us.ibm.com) |
|
// |
|
// Modified By: Markus Mueller |
|
// Ramnath Ravindran (Ramnath.Ravindran@compaq.com) |
|
// David Eger (dteger@us.ibm.com) |
|
// Amit K Arora, IBM (amita@in.ibm.com) for PEP#101 |
|
// Sean Keenan, Hewlett-Packard Company (sean.keenan@hp.com) |
|
// Roger Kumpf, Hewlett-Packard Company (roger_kumpf@hp.com) |
|
// David Dillard, VERITAS Software Corp. |
|
// (david.dillard@veritas.com) |
|
// Aruran, IBM (ashanmug@in.ibm.com) for BUG# 3518 |
|
// |
|
// Reworked By: Mike Brasher (m.brasher@inovadevelopment.com) |
|
// |
|
//%///////////////////////////////////////////////////////////////////////////// | //%///////////////////////////////////////////////////////////////////////////// |
| |
#ifndef Pegasus_Mutex_h | #ifndef Pegasus_Mutex_h |
|
|
| |
#if defined(PEGASUS_HAVE_PTHREADS) | #if defined(PEGASUS_HAVE_PTHREADS) |
typedef pthread_mutex_t MutexType; | typedef pthread_mutex_t MutexType; |
|
inline void mutex_lock(MutexType* mutex) { pthread_mutex_lock(mutex); } |
|
inline void mutex_unlock(MutexType* mutex) { pthread_mutex_unlock(mutex); } |
struct MutexRep | struct MutexRep |
{ | { |
pthread_mutex_t mutex; | pthread_mutex_t mutex; |
pthread_mutexattr_t attr; |
int count; |
ThreadType owner; |
|
}; | }; |
inline void mutex_lock(MutexType* mutex) { pthread_mutex_lock(mutex); } |
|
inline void mutex_unlock(MutexType* mutex) { pthread_mutex_unlock(mutex); } |
|
# define PEGASUS_MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER | # define PEGASUS_MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER |
#endif | #endif |
| |
#if defined(PEGASUS_HAVE_WINDOWS_THREADS) | #if defined(PEGASUS_HAVE_WINDOWS_THREADS) |
typedef HANDLE MutexType; | typedef HANDLE MutexType; |
|
inline void mutex_lock(MutexType* m) { WaitForSingleObject(*m, INFINITE); } |
|
inline void mutex_unlock(MutexType* m) { ReleaseMutex(*m); } |
struct MutexRep | struct MutexRep |
{ | { |
MutexType handle; | MutexType handle; |
ThreadType owner; |
|
size_t count; | size_t count; |
}; | }; |
inline void mutex_lock(MutexType* m) { WaitForSingleObject(*m, INFINITE); } |
|
inline void mutex_unlock(MutexType* m) { ReleaseMutex(*m); } |
|
# define PEGASUS_MUTEX_INITIALIZER (CreateMutex(NULL, FALSE, NULL)) | # define PEGASUS_MUTEX_INITIALIZER (CreateMutex(NULL, FALSE, NULL)) |
#endif | #endif |
| |
|
|
| |
~Mutex(); | ~Mutex(); |
| |
void lock(ThreadType caller = Threads::self()); |
void lock(); |
| |
void try_lock(ThreadType caller = Threads::self()); |
void try_lock(); |
| |
void timed_lock(Uint32 milliseconds, ThreadType caller = Threads::self()); |
void timed_lock(Uint32 milliseconds); |
| |
void unlock(); | void unlock(); |
| |
ThreadType get_owner() { return _rep.owner; } |
|
|
|
void set_owner(ThreadType caller) { _rep.owner = caller; } |
|
|
|
private: | private: |
Mutex(const Mutex&); | Mutex(const Mutex&); |
Mutex& operator=(const Mutex&); | Mutex& operator=(const Mutex&); |
| |
|
|
MutexRep _rep; | MutexRep _rep; |
Magic<0x57D11485> _magic; | Magic<0x57D11485> _magic; |
| |
|
|
{ | { |
public: | public: |
| |
AutoMutex(Mutex& mutex, Boolean autoLock = true) : |
AutoMutex(Mutex& mutex) : _mutex(mutex) |
_mutex(mutex), _locked(autoLock) |
|
{ | { |
if (autoLock) |
|
_mutex.lock(); | _mutex.lock(); |
} | } |
| |
~AutoMutex() | ~AutoMutex() |
{ | { |
try |
|
{ |
|
if (_locked) |
|
unlock(); |
|
} |
|
catch (...) |
|
{ |
|
// Do not propagate exception from destructor |
|
} |
|
} |
|
|
|
void lock() |
|
{ |
|
if (_locked) |
|
throw AlreadyLocked(Threads::self()); |
|
|
|
_mutex.lock(); |
|
_locked = true; |
|
} |
|
|
|
void unlock() |
|
{ |
|
if (!_locked) |
|
throw Permission(Threads::self()); |
|
|
|
_mutex.unlock(); | _mutex.unlock(); |
_locked = false; |
|
} |
|
|
|
Boolean isLocked() const |
|
{ |
|
return _locked; |
|
} | } |
| |
private: | private: |
|
|
AutoMutex& operator=(const AutoMutex& x); // Unimplemented | AutoMutex& operator=(const AutoMutex& x); // Unimplemented |
| |
Mutex& _mutex; | Mutex& _mutex; |
Boolean _locked; |
|
}; | }; |
| |
PEGASUS_NAMESPACE_END | PEGASUS_NAMESPACE_END |