version 1.3, 2006/01/30 16:17:05
|
version 1.3.14.5, 2006/07/28 23:52:11
|
|
|
// | // |
//============================================================================== | //============================================================================== |
// | // |
// Author: Mike Day (mdday@us.ibm.com) |
// Author: Mike Brasher (m.brasher@inovadevelopment.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 |
|
// | // |
//%///////////////////////////////////////////////////////////////////////////// | //%///////////////////////////////////////////////////////////////////////////// |
| |
#include "Mutex.h" | #include "Mutex.h" |
|
#include "Time.h" |
|
#include "PegasusAssert.h" |
| |
PEGASUS_NAMESPACE_BEGIN | PEGASUS_NAMESPACE_BEGIN |
| |
|
//============================================================================== |
|
// |
|
// PEGASUS_HAVE_PTHREADS |
|
// |
|
//============================================================================== |
|
|
|
#if defined(PEGASUS_HAVE_PTHREADS) |
|
|
|
Mutex::Mutex() |
|
{ |
|
pthread_mutexattr_init(&_rep.attr); |
|
|
|
// If your platform does not define PTHREAD_MUTEX_RECURSIVE, try |
|
// PTHREAD_MUTEX_RECURSIVE_NP. |
|
pthread_mutexattr_settype(&_rep.attr, PTHREAD_MUTEX_RECURSIVE); |
|
|
|
pthread_mutex_init(&_rep.mutex, &_rep.attr); |
|
} |
|
|
|
Mutex::~Mutex() |
|
{ |
|
PEGASUS_DEBUG_ASSERT(_magic); |
|
|
|
if (pthread_mutex_destroy(&_rep.mutex) == 0) |
|
pthread_mutexattr_destroy(&_rep.attr); |
|
} |
|
|
|
void Mutex::lock() |
|
{ |
|
PEGASUS_DEBUG_ASSERT(_magic); |
|
|
|
switch (pthread_mutex_lock(&_rep.mutex)) |
|
{ |
|
case 0: |
|
break; |
|
|
|
case EDEADLK: |
|
throw Deadlock(ThreadType()); |
|
|
|
default: |
|
throw WaitFailed(ThreadType()); |
|
} |
|
} |
|
|
|
void Mutex::try_lock() |
|
{ |
|
PEGASUS_DEBUG_ASSERT(_magic); |
|
|
|
switch (pthread_mutex_trylock(&_rep.mutex)) |
|
{ |
|
case 0: |
|
break; |
|
|
|
case EBUSY: |
|
throw AlreadyLocked(ThreadType()); |
|
|
|
case EDEADLK: |
|
throw Deadlock(ThreadType()); |
|
|
|
default: |
|
throw WaitFailed(ThreadType()); |
|
} |
|
} |
|
|
|
void Mutex::timed_lock(Uint32 milliseconds) |
|
{ |
|
PEGASUS_DEBUG_ASSERT(_magic); |
|
|
|
struct timeval now; |
|
struct timeval finish; |
|
struct timeval remaining; |
|
{ |
|
Uint32 usec; |
|
gettimeofday(&finish, NULL); |
|
finish.tv_sec += (milliseconds / 1000 ); |
|
milliseconds %= 1000; |
|
usec = finish.tv_usec + ( milliseconds * 1000 ); |
|
finish.tv_sec += (usec / 1000000); |
|
finish.tv_usec = usec % 1000000; |
|
} |
|
|
|
for (;;) |
|
{ |
|
switch (pthread_mutex_trylock(&_rep.mutex)) |
|
{ |
|
case 0: |
|
return; |
|
|
|
case EBUSY: |
|
{ |
|
gettimeofday(&now, NULL); |
|
|
|
if (Time::subtract(&remaining, &finish, &now)) |
|
throw TimeOut(Threads::self()); |
|
|
|
Threads::yield(); |
|
break; |
|
} |
|
|
|
case EDEADLK: |
|
throw Deadlock(Threads::self()); |
|
|
|
default: |
|
throw WaitFailed(Threads::self()); |
|
} |
|
} |
|
} |
|
|
|
void Mutex::unlock() |
|
{ |
|
PEGASUS_DEBUG_ASSERT(_magic); |
|
|
|
if (pthread_mutex_unlock(&_rep.mutex) != 0) |
|
throw Permission(ThreadType()); |
|
} |
|
|
|
#endif /* PEGASUS_HAVE_PTHREADS */ |
|
|
|
//============================================================================== |
|
// |
|
// PEGASUS_HAVE_WINDOWS_THREADS |
|
// |
|
//============================================================================== |
|
|
|
#if defined(PEGASUS_HAVE_WINDOWS_THREADS) |
|
|
|
Mutex::Mutex() |
|
{ |
|
_rep.handle = CreateMutex(NULL, FALSE, NULL); |
|
Threads::clear(ThreadType()); |
|
_rep.count = 0; |
|
} |
|
|
|
Mutex::~Mutex() |
|
{ |
|
PEGASUS_DEBUG_ASSERT(_magic); |
|
|
|
WaitForSingleObject(_rep.handle, INFINITE); |
|
CloseHandle(_rep.handle); |
|
} |
|
|
|
void Mutex::lock() |
|
{ |
|
PEGASUS_DEBUG_ASSERT(_magic); |
|
|
|
DWORD rc = WaitForSingleObject(_rep.handle, INFINITE); |
|
|
|
if (rc == WAIT_FAILED) |
|
throw WaitFailed(ThreadType()); |
|
|
|
_rep.count++; |
|
} |
|
|
|
void Mutex::try_lock() |
|
{ |
|
PEGASUS_DEBUG_ASSERT(_magic); |
|
|
|
DWORD rc = WaitForSingleObject(_rep.handle, 0); |
|
|
|
if (rc == WAIT_TIMEOUT) |
|
throw AlreadyLocked(ThreadType()); |
|
|
|
if (rc == WAIT_FAILED) |
|
throw WaitFailed(ThreadType()); |
|
|
|
_rep.count++; |
|
} |
|
|
|
void Mutex::timed_lock(Uint32 milliseconds) |
|
{ |
|
PEGASUS_DEBUG_ASSERT(_magic); |
|
|
|
DWORD rc = WaitForSingleObject(_rep.handle, milliseconds); |
|
|
|
if (rc == WAIT_TIMEOUT) |
|
throw TimeOut(ThreadType()); |
|
|
|
if (rc == WAIT_FAILED) |
|
throw WaitFailed(ThreadType()); |
|
|
|
_rep.count++; |
|
} |
|
|
|
void Mutex::unlock() |
|
{ |
|
PEGASUS_DEBUG_ASSERT(_magic); |
|
|
|
Threads::clear(ThreadType()); |
|
_rep.count--; |
|
ReleaseMutex(_rep.handle); |
|
} |
|
|
|
#endif /* PEGASUS_HAVE_WINDOWS_THREADS */ |
|
|
PEGASUS_NAMESPACE_END | PEGASUS_NAMESPACE_END |