version 1.4.2.3, 2006/07/28 20:46:41
|
version 1.12.2.1, 2008/08/20 23:05:50
|
|
|
// | // |
//============================================================================== | //============================================================================== |
// | // |
// 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 |
| |
|
|
{ | { |
public: | public: |
| |
|
enum RecursiveTag { RECURSIVE }; |
|
enum NonRecursiveTag { NON_RECURSIVE }; |
|
|
|
/** Default constructor creates a recursive mutex. |
|
*/ |
Mutex(); | Mutex(); |
| |
~Mutex(); |
/** Call as Mutex(Mutex::RECURSIVE) to create a recursive mutex. |
|
*/ |
|
Mutex(RecursiveTag); |
|
|
|
/** Call as Mutex(Mutex::NON_RECURSIVE) to create a non-recursive mutex. |
|
*/ |
|
Mutex(NonRecursiveTag); |
| |
void lock(ThreadType caller = Threads::self()); |
~Mutex(); |
| |
void try_lock(ThreadType caller = Threads::self()); |
void lock(); |
| |
void timed_lock(Uint32 milliseconds, ThreadType caller = Threads::self()); |
/** |
|
Attempts to lock the mutex without blocking. |
|
@return A Boolean indicating whether the lock was acquired. |
|
*/ |
|
Boolean try_lock(); |
|
|
|
/** |
|
Attempts to lock the mutex within the specified time. |
|
@param milliseconds The maximum time to block while attempting to |
|
acquire the lock. |
|
@return A Boolean indicating whether the lock was acquired. |
|
*/ |
|
Boolean timed_lock(Uint32 milliseconds); |
| |
void unlock(); | void unlock(); |
| |
ThreadType get_owner() { return _rep.owner; } |
#if defined(PEGASUS_OS_LINUX) || \ |
|
(defined(PEGASUS_OS_ZOS) && !(__TARGET_LIB__ < 0x41090000)) |
void set_owner(ThreadType caller) { _rep.owner = caller; } |
/** |
|
This method must only be called after a fork() to reset the mutex |
|
lock status in the new process. Any other use of this method is |
|
unsafe. |
|
*/ |
|
void reinitialize(); |
|
#endif |
| |
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_FORK_SAFE_MUTEX |
|
// |
|
//============================================================================== |
|
|
|
// Use of this macro ensures that a static Mutex is not locked across a fork(). |
|
|
|
#if !defined(PEGASUS_HAVE_PTHREADS) || \ |
|
(defined(PEGASUS_OS_ZOS) && (__TARGET_LIB__ < 0x41090000)) || \ |
|
defined(PEGASUS_OS_VMS) |
|
|
|
# define PEGASUS_FORK_SAFE_MUTEX(mutex) |
|
|
|
#elif defined(PEGASUS_OS_LINUX) || \ |
|
(defined(PEGASUS_OS_ZOS) && !(__TARGET_LIB__ < 0x41090000)) |
|
|
|
# define PEGASUS_FORK_SAFE_MUTEX(mutex) \ |
|
class ForkSafeMutex ## mutex \ |
|
{ \ |
|
public: \ |
|
ForkSafeMutex ## mutex() \ |
|
{ \ |
|
pthread_atfork( \ |
|
0, \ |
|
0, \ |
|
_reinitializeMutex); \ |
|
} \ |
|
\ |
|
private: \ |
|
static void _reinitializeMutex() \ |
|
{ \ |
|
mutex.reinitialize(); \ |
|
} \ |
|
}; \ |
|
\ |
|
static ForkSafeMutex ## mutex __forkSafeMutex ## mutex; |
|
|
|
#else |
|
|
|
# define PEGASUS_FORK_SAFE_MUTEX(mutex) \ |
|
class ForkSafeMutex ## mutex \ |
|
{ \ |
|
public: \ |
|
ForkSafeMutex ## mutex() \ |
|
{ \ |
|
pthread_atfork( \ |
|
_lockMutex, \ |
|
_unlockMutex, \ |
|
_unlockMutex); \ |
|
} \ |
|
\ |
|
private: \ |
|
static void _lockMutex() \ |
|
{ \ |
|
mutex.lock(); \ |
|
} \ |
|
\ |
|
static void _unlockMutex() \ |
|
{ \ |
|
mutex.unlock(); \ |
|
} \ |
|
}; \ |
|
\ |
|
static ForkSafeMutex ## mutex __forkSafeMutex ## mutex; |
|
|
|
#endif |
|
|
PEGASUS_NAMESPACE_END | PEGASUS_NAMESPACE_END |
| |
#endif /* Pegasus_Mutex_h */ | #endif /* Pegasus_Mutex_h */ |