version 1.3.12.1, 2006/06/29 17:33:12
|
version 1.10, 2007/08/02 08:06:53
|
|
|
// | // |
//============================================================================== | //============================================================================== |
// | // |
// 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 |
|
// |
|
//%///////////////////////////////////////////////////////////////////////////// | //%///////////////////////////////////////////////////////////////////////////// |
| |
#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/Threads.h> |
| |
PEGASUS_NAMESPACE_BEGIN | PEGASUS_NAMESPACE_BEGIN |
| |
|
//============================================================================== |
|
// |
|
// MutexRep |
|
// |
|
//============================================================================== |
|
|
|
#if defined(PEGASUS_HAVE_PTHREADS) |
|
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 |
|
{ |
|
pthread_mutex_t mutex; |
|
int count; |
|
}; |
|
# define PEGASUS_MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER |
|
#endif |
|
|
|
#if defined(PEGASUS_HAVE_WINDOWS_THREADS) |
|
typedef HANDLE MutexType; |
|
inline void mutex_lock(MutexType* m) { WaitForSingleObject(*m, INFINITE); } |
|
inline void mutex_unlock(MutexType* m) { ReleaseMutex(*m); } |
|
struct MutexRep |
|
{ |
|
MutexType handle; |
|
size_t count; |
|
}; |
|
# define PEGASUS_MUTEX_INITIALIZER (CreateMutex(NULL, FALSE, NULL)) |
|
#endif |
|
|
|
//============================================================================== |
|
// |
|
// Mutex |
|
// |
|
//============================================================================== |
|
|
class PEGASUS_COMMON_LINKAGE Mutex | class PEGASUS_COMMON_LINKAGE Mutex |
{ | { |
public: | public: |
| |
|
enum RecursiveTag { RECURSIVE }; |
|
enum NonRecursiveTag { NON_RECURSIVE }; |
|
|
|
/** Default constructor creates a recursive mutex. |
|
*/ |
Mutex(); | Mutex(); |
Mutex(int type); |
|
|
/** 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); |
| |
~Mutex(); | ~Mutex(); |
| |
// block until gaining the lock - throw a deadlock |
void lock(); |
// exception if process already holds the lock |
|
// @exception Deadlock |
void try_lock(); |
// @exception WaitFailed |
|
void lock(PEGASUS_THREAD_TYPE caller = pegasus_thread_self()); |
void timed_lock(Uint32 milliseconds); |
|
|
// 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); } |
#if defined(PEGASUS_OS_LINUX) |
|
/** |
|
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: |
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) : _mutex(mutex) |
|
{ |
|
_mutex.lock(); |
|
} |
|
|
|
~AutoMutex() |
{ | { |
return _mutex; |
_mutex.unlock(); |
} | } |
| |
// Hide the assignment operator to avoid implicit use of the default |
private: |
// assignment operator. Do not use this method. |
AutoMutex(); // Unimplemented |
Mutex& operator=(const Mutex& original) {return *this;} |
AutoMutex(const AutoMutex& x); // Unimplemented |
|
AutoMutex& operator=(const AutoMutex& x); // Unimplemented |
// Hide the copy constructor to avoid implicit use of the default |
|
// copy constructor. Do not use this method. |
|
Mutex(const Mutex& _mutex); |
|
| |
friend class Condition; |
Mutex& _mutex; |
}; | }; |
| |
|
//============================================================================== |
|
// |
|
// 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) |
|
|
|
# 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 */ |