version 1.4.2.6, 2006/07/30 22:57:19
|
version 1.10, 2007/08/02 08:06:53
|
|
|
// | // |
//============================================================================== | //============================================================================== |
// | // |
// Author: 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 |
{ | { |
MutexType mutex; |
pthread_mutex_t mutex; |
|
int count; |
}; | }; |
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; |
|
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(); |
| |
|
/** 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(); |
{ |
|
mutex_lock(&_rep.mutex); |
|
} |
|
| |
void try_lock(); | void try_lock(); |
| |
void timed_lock(Uint32 milliseconds); | void timed_lock(Uint32 milliseconds); |
| |
void unlock() |
void unlock(); |
{ |
|
mutex_unlock(&_rep.mutex); |
#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: |
Mutex(const Mutex&); | Mutex(const Mutex&); |
Mutex& operator=(const Mutex&); | Mutex& operator=(const Mutex&); |
| |
|
|
MutexRep _rep; | MutexRep _rep; |
Magic<0x57D11485> _magic; | Magic<0x57D11485> _magic; |
| |
|
|
_mutex.unlock(); | _mutex.unlock(); |
} | } |
| |
void lock() |
|
{ |
|
_mutex.lock(); |
|
} |
|
|
|
void unlock() |
|
{ |
|
_mutex.unlock(); |
|
} |
|
|
|
private: | private: |
AutoMutex(); |
AutoMutex(); // Unimplemented |
AutoMutex(const AutoMutex& x); |
AutoMutex(const AutoMutex& x); // Unimplemented |
AutoMutex& operator=(const AutoMutex& x); |
AutoMutex& operator=(const AutoMutex& x); // Unimplemented |
| |
Mutex& _mutex; | 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 */ |