version 1.4, 2006/07/11 18:39:28
|
version 1.4.2.1, 2006/07/27 23:11:52
|
|
|
// (david.dillard@veritas.com) | // (david.dillard@veritas.com) |
// Aruran, IBM (ashanmug@in.ibm.com) for BUG# 3518 | // 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 |
|
|
| |
#include <Pegasus/Common/Config.h> | #include <Pegasus/Common/Config.h> |
#include <Pegasus/Common/Linkage.h> | #include <Pegasus/Common/Linkage.h> |
#include <Pegasus/Common/IPCTypes.h> |
#include <Pegasus/Common/IPCExceptions.h> |
#include <Pegasus/Common/Magic.h> | #include <Pegasus/Common/Magic.h> |
|
#include <Pegasus/Common/Threads.h> |
| |
PEGASUS_NAMESPACE_BEGIN | PEGASUS_NAMESPACE_BEGIN |
| |
|
//============================================================================== |
|
// |
|
// MutexRep |
|
// |
|
//============================================================================== |
|
|
|
#if defined(PEGASUS_HAVE_PTHREADS) |
|
struct MutexRep |
|
{ |
|
pthread_mutex_t mutex; |
|
pthread_mutexattr_t attr; |
|
pthread_t owner; |
|
}; |
|
#endif |
|
|
|
#if defined(PEGASUS_HAVE_WINDOWS_THREADS) |
|
struct MutexRep |
|
{ |
|
HANDLE handle; |
|
HANDLE owner; |
|
size_t count; |
|
}; |
|
#endif |
|
|
|
//============================================================================== |
|
// |
|
// Mutex |
|
// |
|
//============================================================================== |
|
|
class PEGASUS_COMMON_LINKAGE Mutex | class PEGASUS_COMMON_LINKAGE Mutex |
{ | { |
public: | public: |
| |
Mutex(); | Mutex(); |
Mutex(int type); |
|
| |
~Mutex(); | ~Mutex(); |
| |
// block until gaining the lock - throw a deadlock |
void lock(ThreadType caller = Threads::self()); |
// exception if process already holds the lock |
|
// @exception Deadlock |
void try_lock(ThreadType caller = Threads::self()); |
// @exception WaitFailed |
|
void lock(PEGASUS_THREAD_TYPE caller = pegasus_thread_self()); |
void timed_lock(Uint32 milliseconds, ThreadType caller = Threads::self()); |
|
|
// try to gain the lock - lock succeeds immediately if the |
|
// mutex is not already locked. throws an exception and returns |
|
// immediately if the mutex is currently locked. |
|
// @exception Deadlock |
|
// @exception AlreadyLocked |
|
// @exception WaitFailed |
|
void try_lock(PEGASUS_THREAD_TYPE caller = pegasus_thread_self()); |
|
|
|
// wait for milliseconds and throw an exception then return if the wait |
|
// expires without gaining the lock. Otherwise return without throwing an |
|
// exception. |
|
// @exception Deadlock |
|
// @exception TimeOut |
|
// @exception WaitFailed |
|
void timed_lock( |
|
Uint32 milliseconds, |
|
PEGASUS_THREAD_TYPE caller = pegasus_thread_self()); |
|
| |
// unlock the semaphore |
|
// @exception Permission |
|
void unlock(); | void unlock(); |
| |
inline PEGASUS_THREAD_TYPE get_owner() { return(_mutex.owner); } |
ThreadType get_owner() { return _rep.owner; } |
|
|
|
void set_owner(ThreadType caller) { _rep.owner = caller; } |
| |
private: | private: |
inline void _set_owner(PEGASUS_THREAD_TYPE owner) { _mutex.owner = owner; } |
Mutex(const Mutex&); |
PEGASUS_MUTEX_HANDLE _mutex; |
Mutex& operator=(const Mutex&); |
PEGASUS_MUTEX_HANDLE & _get_handle() |
|
|
|
|
MutexRep _rep; |
|
Magic<0x57D11485> _magic; |
|
|
|
friend class Condition; |
|
}; |
|
|
|
//============================================================================== |
|
// |
|
// AutoMutex |
|
// |
|
//============================================================================== |
|
|
|
class PEGASUS_COMMON_LINKAGE AutoMutex |
|
{ |
|
public: |
|
|
|
AutoMutex(Mutex& mutex, Boolean autoLock = true) : |
|
_mutex(mutex), _locked(autoLock) |
|
{ |
|
if (autoLock) |
|
_mutex.lock(); |
|
} |
|
|
|
~AutoMutex() |
|
{ |
|
try |
|
{ |
|
if (_locked) |
|
unlock(); |
|
} |
|
catch (...) |
|
{ |
|
// Do not propagate exception from destructor |
|
} |
|
} |
|
|
|
void lock() |
{ | { |
return _mutex; |
if (_locked) |
|
throw AlreadyLocked(Threads::self()); |
|
|
|
_mutex.lock(); |
|
_locked = true; |
} | } |
| |
// Hide the assignment operator to avoid implicit use of the default |
void unlock() |
// assignment operator. Do not use this method. |
{ |
Mutex& operator=(const Mutex& original) {return *this;} |
if (!_locked) |
|
throw Permission(Threads::self()); |
|
|
|
_mutex.unlock(); |
|
_locked = false; |
|
} |
| |
// Hide the copy constructor to avoid implicit use of the default |
Boolean isLocked() const |
// copy constructor. Do not use this method. |
{ |
Mutex(const Mutex& _mutex); |
return _locked; |
|
} |
| |
friend class Condition; |
private: |
|
AutoMutex(); // Unimplemented |
|
AutoMutex(const AutoMutex& x); // Unimplemented |
|
AutoMutex& operator=(const AutoMutex& x); // Unimplemented |
| |
Magic<0x57D11485> _magic; |
Mutex& _mutex; |
|
Boolean _locked; |
}; | }; |
| |
PEGASUS_NAMESPACE_END | PEGASUS_NAMESPACE_END |