version 1.3.14.9, 2006/07/31 17:37:18
|
version 1.3.14.10, 2006/08/01 20:29:59
|
|
|
// | // |
//============================================================================== | //============================================================================== |
// | // |
// Author: Mike Brasher (m.brasher@inovadevelopment.com) |
|
// |
|
//%///////////////////////////////////////////////////////////////////////////// | //%///////////////////////////////////////////////////////////////////////////// |
| |
#include "Mutex.h" | #include "Mutex.h" |
|
|
{ | { |
once(&_once, _init_attr); | once(&_once, _init_attr); |
pthread_mutex_init(&_rep.mutex, &_attr); | pthread_mutex_init(&_rep.mutex, &_attr); |
|
#if defined(PEGASUS_DEBUG) |
_rep.count = 0; | _rep.count = 0; |
|
#endif |
} | } |
| |
Mutex::~Mutex() | Mutex::~Mutex() |
{ | { |
PEGASUS_DEBUG_ASSERT(_magic); | PEGASUS_DEBUG_ASSERT(_magic); |
pthread_mutex_destroy(&_rep.mutex); | pthread_mutex_destroy(&_rep.mutex); |
_rep.count = -1; |
|
} | } |
| |
void Mutex::lock() | void Mutex::lock() |
|
|
switch (pthread_mutex_lock(&_rep.mutex)) | switch (pthread_mutex_lock(&_rep.mutex)) |
{ | { |
case 0: | case 0: |
|
#if defined(PEGASUS_DEBUG) |
_rep.count++; | _rep.count++; |
|
#endif |
break; | break; |
| |
case EDEADLK: |
|
throw Deadlock(ThreadType()); |
|
|
|
default: | default: |
throw WaitFailed(ThreadType()); |
throw WaitFailed(Threads::self()); |
} | } |
} | } |
| |
|
|
switch (pthread_mutex_trylock(&_rep.mutex)) | switch (pthread_mutex_trylock(&_rep.mutex)) |
{ | { |
case 0: | case 0: |
|
#if defined(PEGASUS_DEBUG) |
_rep.count++; | _rep.count++; |
|
#endif |
break; | break; |
| |
case EBUSY: | case EBUSY: |
throw AlreadyLocked(ThreadType()); |
throw AlreadyLocked(Threads::self()); |
|
|
case EDEADLK: |
|
throw Deadlock(ThreadType()); |
|
| |
default: | default: |
throw WaitFailed(ThreadType()); |
throw WaitFailed(Threads::self()); |
} | } |
} | } |
| |
|
|
switch (pthread_mutex_trylock(&_rep.mutex)) | switch (pthread_mutex_trylock(&_rep.mutex)) |
{ | { |
case 0: | case 0: |
|
#if defined(PEGASUS_DEBUG) |
_rep.count++; | _rep.count++; |
|
#endif |
return; | return; |
| |
case EBUSY: | case EBUSY: |
|
|
break; | break; |
} | } |
| |
case EDEADLK: |
|
throw Deadlock(Threads::self()); |
|
|
|
default: | default: |
throw WaitFailed(Threads::self()); | throw WaitFailed(Threads::self()); |
} | } |
|
|
PEGASUS_DEBUG_ASSERT(_magic); | PEGASUS_DEBUG_ASSERT(_magic); |
PEGASUS_DEBUG_ASSERT(_rep.count > 0); | PEGASUS_DEBUG_ASSERT(_rep.count > 0); |
| |
if (pthread_mutex_unlock(&_rep.mutex) != 0) |
#if defined(PEGASUS_DEBUG) |
throw Permission(ThreadType()); |
|
|
|
_rep.count--; | _rep.count--; |
|
#endif |
|
|
|
if (pthread_mutex_unlock(&_rep.mutex) != 0) |
|
throw Permission(Threads::self()); |
} | } |
| |
#endif /* PEGASUS_HAVE_PTHREADS */ | #endif /* PEGASUS_HAVE_PTHREADS */ |
|
|
Mutex::Mutex() | Mutex::Mutex() |
{ | { |
_rep.handle = CreateMutex(NULL, FALSE, NULL); | _rep.handle = CreateMutex(NULL, FALSE, NULL); |
Threads::clear(ThreadType()); |
#if defined(PEGASUS_DEBUG) |
_rep.count = 0; | _rep.count = 0; |
|
#endif |
} | } |
| |
Mutex::~Mutex() | Mutex::~Mutex() |
|
|
DWORD rc = WaitForSingleObject(_rep.handle, INFINITE); | DWORD rc = WaitForSingleObject(_rep.handle, INFINITE); |
| |
if (rc == WAIT_FAILED) | if (rc == WAIT_FAILED) |
throw WaitFailed(ThreadType()); |
throw WaitFailed(Threads::self()); |
| |
|
#if defined(PEGASUS_DEBUG) |
_rep.count++; | _rep.count++; |
|
#endif |
} | } |
| |
void Mutex::try_lock() | void Mutex::try_lock() |
|
|
DWORD rc = WaitForSingleObject(_rep.handle, 0); | DWORD rc = WaitForSingleObject(_rep.handle, 0); |
| |
if (rc == WAIT_TIMEOUT) | if (rc == WAIT_TIMEOUT) |
throw AlreadyLocked(ThreadType()); |
throw AlreadyLocked(Threads::self()); |
| |
if (rc == WAIT_FAILED) | if (rc == WAIT_FAILED) |
throw WaitFailed(ThreadType()); |
throw WaitFailed(Threads::self()); |
| |
|
#if defined(PEGASUS_DEBUG) |
_rep.count++; | _rep.count++; |
|
#endif |
} | } |
| |
void Mutex::timed_lock(Uint32 milliseconds) | void Mutex::timed_lock(Uint32 milliseconds) |
|
|
DWORD rc = WaitForSingleObject(_rep.handle, milliseconds); | DWORD rc = WaitForSingleObject(_rep.handle, milliseconds); |
| |
if (rc == WAIT_TIMEOUT) | if (rc == WAIT_TIMEOUT) |
throw TimeOut(ThreadType()); |
throw TimeOut(Threads::self()); |
| |
if (rc == WAIT_FAILED) | if (rc == WAIT_FAILED) |
throw WaitFailed(ThreadType()); |
throw WaitFailed(Threads::self()); |
| |
|
#if defined(PEGASUS_DEBUG) |
_rep.count++; | _rep.count++; |
|
#endif |
} | } |
| |
void Mutex::unlock() | void Mutex::unlock() |
{ | { |
PEGASUS_DEBUG_ASSERT(_magic); | PEGASUS_DEBUG_ASSERT(_magic); |
|
PEGASUS_DEBUG_ASSERT(_rep.count > 0); |
| |
Threads::clear(ThreadType()); |
#if defined(PEGASUS_DEBUG) |
_rep.count--; | _rep.count--; |
|
#endif |
ReleaseMutex(_rep.handle); | ReleaseMutex(_rep.handle); |
} | } |
| |