summaryrefslogtreecommitdiff
path: root/cpp/src/IceUtil/RecMutex.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'cpp/src/IceUtil/RecMutex.cpp')
-rw-r--r--cpp/src/IceUtil/RecMutex.cpp71
1 files changed, 27 insertions, 44 deletions
diff --git a/cpp/src/IceUtil/RecMutex.cpp b/cpp/src/IceUtil/RecMutex.cpp
index a6e75363afe..44f81a0ca59 100644
--- a/cpp/src/IceUtil/RecMutex.cpp
+++ b/cpp/src/IceUtil/RecMutex.cpp
@@ -31,16 +31,14 @@ IceUtil::RecMutex::~RecMutex()
DeleteCriticalSection(&_mutex);
}
-bool
+void
IceUtil::RecMutex::lock() const
{
EnterCriticalSection(&_mutex);
if(++_count > 1)
{
LeaveCriticalSection(&_mutex);
- return false;
}
- return true;
}
bool
@@ -48,25 +46,22 @@ IceUtil::RecMutex::trylock() const
{
if(!TryEnterCriticalSection(&_mutex))
{
- throw ThreadLockedException(__FILE__, __LINE__);
+ return false;
}
if(++_count > 1)
{
LeaveCriticalSection(&_mutex);
- return false;
}
return true;
}
-bool
+void
IceUtil::RecMutex::unlock() const
{
if(--_count == 0)
{
LeaveCriticalSection(&_mutex);
- return true;
}
- return false;
}
void
@@ -90,46 +85,36 @@ IceUtil::RecMutex::RecMutex() :
{
int rc;
-#if _POSIX_VERSION >= 199506L
-
+#if defined(__linux) && !defined(__USE_UNIX98)
+ const pthread_mutexattr_t attr = { PTHREAD_MUTEX_RECURSIVE_NP };
+#else
pthread_mutexattr_t attr;
-
rc = pthread_mutexattr_init(&attr);
if(rc != 0)
{
- throw ThreadSyscallException(__FILE__, __LINE__);
+ throw ThreadSyscallException(__FILE__, __LINE__, rc);
}
-
rc = pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
if(rc != 0)
{
- throw ThreadSyscallException(__FILE__, __LINE__);
+ throw ThreadSyscallException(__FILE__, __LINE__, rc);
}
-
-#elif defined(__linux__)
-
- const pthread_mutexattr_t attr = { PTHREAD_MUTEX_RECURSIVE_NP };
-
-#else
-
- const pthread_mutexattr_t attr = { PTHREAD_MUTEX_RECURSIVE };
-
#endif
rc = pthread_mutex_init(&_mutex, &attr);
if(rc != 0)
{
- throw ThreadSyscallException(__FILE__, __LINE__);
+ throw ThreadSyscallException(__FILE__, __LINE__, rc);
}
-#if _POSIX_VERSION >= 199506L
-
+#if defined(__linux) && !defined(__USE_UNIX98)
+// Nothing to do
+#else
rc = pthread_mutexattr_destroy(&attr);
if(rc != 0)
{
- throw ThreadSyscallException(__FILE__, __LINE__);
+ throw ThreadSyscallException(__FILE__, __LINE__, rc);
}
-
#endif
}
@@ -141,45 +126,45 @@ IceUtil::RecMutex::~RecMutex()
assert(rc == 0);
}
-bool
+void
IceUtil::RecMutex::lock() const
{
int rc = pthread_mutex_lock(&_mutex);
if(rc != 0)
{
- throw ThreadSyscallException(__FILE__, __LINE__);
+ throw ThreadSyscallException(__FILE__, __LINE__, rc);
}
if(++_count > 1)
{
rc = pthread_mutex_unlock(&_mutex);
assert(rc == 0);
- return false;
}
- return true;
}
bool
IceUtil::RecMutex::trylock() const
{
int rc = pthread_mutex_trylock(&_mutex);
- if(rc != 0)
+ bool result = (rc == 0);
+ if(!result)
{
- if(rc == EBUSY)
+ if(rc != EBUSY)
{
- throw ThreadLockedException(__FILE__, __LINE__);
+ throw ThreadSyscallException(__FILE__, __LINE__, rc);
}
- throw ThreadSyscallException(__FILE__, __LINE__);
- }
- if(++_count > 1)
+ }
+ else if(++_count > 1)
{
rc = pthread_mutex_unlock(&_mutex);
- assert(rc == 0);
- return false;
+ if(rc != 0)
+ {
+ throw ThreadSyscallException(__FILE__, __LINE__, rc);
+ }
}
- return true;
+ return result;
}
-bool
+void
IceUtil::RecMutex::unlock() const
{
if(--_count == 0)
@@ -187,9 +172,7 @@ IceUtil::RecMutex::unlock() const
int rc = 0; // Prevent warnings when NDEBUG is defined.
rc = pthread_mutex_unlock(&_mutex);
assert(rc == 0);
- return true;
}
- return false;
}
void