version 1.4.2.7, 2006/07/30 23:11:55
|
version 1.12.2.1, 2008/08/20 23:05:50
|
|
|
// | // |
//============================================================================== | //============================================================================== |
// | // |
// Author: Mike Brasher (m.brasher@inovadevelopment.com) |
|
// |
|
//%///////////////////////////////////////////////////////////////////////////// | //%///////////////////////////////////////////////////////////////////////////// |
| |
#ifndef Pegasus_Mutex_h | #ifndef Pegasus_Mutex_h |
|
|
struct MutexRep | struct MutexRep |
{ | { |
pthread_mutex_t mutex; | pthread_mutex_t mutex; |
|
int count; |
}; | }; |
# define PEGASUS_MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER | # define PEGASUS_MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER |
#endif | #endif |
|
|
{ | { |
public: | public: |
| |
|
enum RecursiveTag { RECURSIVE }; |
|
enum NonRecursiveTag { NON_RECURSIVE }; |
|
|
|
/** Default constructor creates a recursive 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); |
|
|
~Mutex(); | ~Mutex(); |
| |
void lock(); | void lock(); |
| |
void try_lock(); |
/** |
|
Attempts to lock the mutex without blocking. |
void timed_lock(Uint32 milliseconds); |
@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(); |
| |
|
#if defined(PEGASUS_OS_LINUX) || \ |
|
(defined(PEGASUS_OS_ZOS) && !(__TARGET_LIB__ < 0x41090000)) |
|
/** |
|
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&); |
|
|
{ | { |
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(ThreadType()); |
|
|
|
_mutex.lock(); |
|
_locked = true; |
|
} |
|
|
|
void unlock() |
|
{ |
|
if (!_locked) |
|
throw Permission(ThreadType()); |
|
|
|
_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 */ |