summaryrefslogtreecommitdiff
path: root/cpp/src
diff options
context:
space:
mode:
authorBernard Normier <bernard@zeroc.com>2003-04-16 04:24:18 +0000
committerBernard Normier <bernard@zeroc.com>2003-04-16 04:24:18 +0000
commit3c99b3b3923f419eb3c2934fb017dadea6180fc6 (patch)
treec61d296def71d4c261637ef1297706883bf17d94 /cpp/src
parenta link cost of 0 means accept all messages regardless of cost (diff)
downloadice-3c99b3b3923f419eb3c2934fb017dadea6180fc6.tar.bz2
ice-3c99b3b3923f419eb3c2934fb017dadea6180fc6.tar.xz
ice-3c99b3b3923f419eb3c2934fb017dadea6180fc6.zip
Mutex and Lock changes: lock/unlock now return void, trylock returns a bool
that indicates whether the lock was acquired or not, plus new member functions on LockT/TryLockT
Diffstat (limited to 'cpp/src')
-rw-r--r--cpp/src/IceUtil/Cond.cpp18
-rw-r--r--cpp/src/IceUtil/RWRecMutex.cpp25
-rw-r--r--cpp/src/IceUtil/RecMutex.cpp71
-rw-r--r--cpp/src/IceUtil/Thread.cpp16
-rw-r--r--cpp/src/IceUtil/ThreadException.cpp15
-rw-r--r--cpp/src/IceUtil/UUID.cpp5
6 files changed, 71 insertions, 79 deletions
diff --git a/cpp/src/IceUtil/Cond.cpp b/cpp/src/IceUtil/Cond.cpp
index 29b76783186..a6b6a84da25 100644
--- a/cpp/src/IceUtil/Cond.cpp
+++ b/cpp/src/IceUtil/Cond.cpp
@@ -25,7 +25,7 @@ IceUtil::Semaphore::Semaphore(long initial)
_sem = CreateSemaphore(0, initial, 0x7fffffff, 0);
if(_sem == INVALID_HANDLE_VALUE)
{
- throw ThreadSyscallException(__FILE__, __LINE__);
+ throw ThreadSyscallException(__FILE__, __LINE__, GetLastError());
}
}
@@ -40,7 +40,7 @@ IceUtil::Semaphore::wait() const
int rc = WaitForSingleObject(_sem, INFINITE);
if(rc != WAIT_OBJECT_0)
{
- throw ThreadSyscallException(__FILE__, __LINE__);
+ throw ThreadSyscallException(__FILE__, __LINE__, GetLastError());
}
}
@@ -53,7 +53,7 @@ IceUtil::Semaphore::timedWait(const Time& timeout) const
int rc = WaitForSingleObject(_sem, msec);
if(rc != WAIT_TIMEOUT && rc != WAIT_OBJECT_0)
{
- throw ThreadSyscallException(__FILE__, __LINE__);
+ throw ThreadSyscallException(__FILE__, __LINE__, GetLastError());
}
return rc != WAIT_TIMEOUT;
}
@@ -64,7 +64,7 @@ IceUtil::Semaphore::post(int count) const
int rc = ReleaseSemaphore(_sem, count, 0);
if(rc == 0)
{
- throw ThreadSyscallException(__FILE__, __LINE__);
+ throw ThreadSyscallException(__FILE__, __LINE__, GetLastError());
}
}
@@ -207,19 +207,19 @@ IceUtil::Cond::Cond()
rc = pthread_condattr_init(&attr);
if(rc != 0)
{
- throw ThreadSyscallException(__FILE__, __LINE__);
+ throw ThreadSyscallException(__FILE__, __LINE__, rc);
}
rc = pthread_cond_init(&_cond, &attr);
if(rc != 0)
{
- throw ThreadSyscallException(__FILE__, __LINE__);
+ throw ThreadSyscallException(__FILE__, __LINE__, rc);
}
rc = pthread_condattr_destroy(&attr);
if(rc != 0)
{
- throw ThreadSyscallException(__FILE__, __LINE__);
+ throw ThreadSyscallException(__FILE__, __LINE__, rc);
}
}
@@ -236,7 +236,7 @@ IceUtil::Cond::signal()
int rc = pthread_cond_signal(&_cond);
if(rc != 0)
{
- throw ThreadSyscallException(__FILE__, __LINE__);
+ throw ThreadSyscallException(__FILE__, __LINE__, rc);
}
}
@@ -246,7 +246,7 @@ IceUtil::Cond::broadcast()
int rc = pthread_cond_broadcast(&_cond);
if(rc != 0)
{
- throw ThreadSyscallException(__FILE__, __LINE__);
+ throw ThreadSyscallException(__FILE__, __LINE__, rc);
}
}
diff --git a/cpp/src/IceUtil/RWRecMutex.cpp b/cpp/src/IceUtil/RWRecMutex.cpp
index d5ff0a6b2db..5a98ae9036d 100644
--- a/cpp/src/IceUtil/RWRecMutex.cpp
+++ b/cpp/src/IceUtil/RWRecMutex.cpp
@@ -16,7 +16,6 @@
#include <IceUtil/Exception.h>
#include <IceUtil/Time.h>
-#include <assert.h>
IceUtil::RWRecMutex::RWRecMutex() :
_count(0),
@@ -45,7 +44,7 @@ IceUtil::RWRecMutex::readlock() const
_count++;
}
-void
+bool
IceUtil::RWRecMutex::tryReadlock() const
{
Mutex::Lock lock(_mutex);
@@ -56,12 +55,13 @@ IceUtil::RWRecMutex::tryReadlock() const
//
if(_count < 0 || _waitingWriters != 0)
{
- throw ThreadLockedException(__FILE__, __LINE__);
+ return false;
}
_count++;
+ return true;
}
-void
+bool
IceUtil::RWRecMutex::timedTryReadlock(const Time& timeout) const
{
Mutex::Lock lock(_mutex);
@@ -80,11 +80,12 @@ IceUtil::RWRecMutex::timedTryReadlock(const Time& timeout) const
}
else
{
- throw ThreadLockedException(__FILE__, __LINE__);
+ return false;
}
}
_count++;
+ return true;
}
void
@@ -128,7 +129,7 @@ IceUtil::RWRecMutex::writelock() const
_writerId = ThreadControl().id();
}
-void
+bool
IceUtil::RWRecMutex::tryWritelock() const
{
Mutex::Lock lock(_mutex);
@@ -140,7 +141,7 @@ IceUtil::RWRecMutex::tryWritelock() const
if(_count < 0 && _writerId == ThreadControl().id())
{
--_count;
- return;
+ return true;
}
//
@@ -148,7 +149,7 @@ IceUtil::RWRecMutex::tryWritelock() const
//
if(_count != 0)
{
- throw ThreadLockedException(__FILE__, __LINE__);
+ return false;
}
//
@@ -156,9 +157,10 @@ IceUtil::RWRecMutex::tryWritelock() const
//
_count = -1;
_writerId = ThreadControl().id();
+ return true;
}
-void
+bool
IceUtil::RWRecMutex::timedTryWritelock(const Time& timeout) const
{
Mutex::Lock lock(_mutex);
@@ -169,7 +171,7 @@ IceUtil::RWRecMutex::timedTryWritelock(const Time& timeout) const
if(_count < 0 && _writerId == ThreadControl().id())
{
--_count;
- return;
+ return true;
}
//
@@ -196,7 +198,7 @@ IceUtil::RWRecMutex::timedTryWritelock(const Time& timeout) const
}
else
{
- throw ThreadLockedException(__FILE__, __LINE__);
+ return false;
}
}
@@ -205,6 +207,7 @@ IceUtil::RWRecMutex::timedTryWritelock(const Time& timeout) const
//
_count = -1;
_writerId = ThreadControl().id();
+ return true;
}
void
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
diff --git a/cpp/src/IceUtil/Thread.cpp b/cpp/src/IceUtil/Thread.cpp
index d0f6407e12e..bb99ee54841 100644
--- a/cpp/src/IceUtil/Thread.cpp
+++ b/cpp/src/IceUtil/Thread.cpp
@@ -30,7 +30,7 @@ IceUtil::ThreadControl::ThreadControl()
int rc = DuplicateHandle(proc, current, proc, &_handle->handle, SYNCHRONIZE, TRUE, 0);
if(rc == 0)
{
- throw ThreadSyscallException(__FILE__, __LINE__);
+ throw ThreadSyscallException(__FILE__, __LINE__, GetLastError());
}
}
@@ -114,7 +114,7 @@ IceUtil::ThreadControl::join()
int rc = WaitForSingleObject(handle->handle, INFINITE);
if(rc != WAIT_OBJECT_0)
{
- throw ThreadSyscallException(__FILE__, __LINE__);
+ throw ThreadSyscallException(__FILE__, __LINE__, GetLastError());
}
}
@@ -236,7 +236,7 @@ IceUtil::Thread::start()
if(_handle->handle == 0)
{
__decRef();
- throw ThreadSyscallException(__FILE__, __LINE__);
+ throw ThreadSyscallException(__FILE__, __LINE__, GetLastError());
}
_started = true;
@@ -384,7 +384,7 @@ IceUtil::ThreadControl::join()
int rc = pthread_join(id, &ignore);
if(rc != 0)
{
- throw ThreadSyscallException(__FILE__, __LINE__);
+ throw ThreadSyscallException(__FILE__, __LINE__, rc);
}
}
@@ -399,7 +399,7 @@ IceUtil::ThreadControl::detach()
int rc = pthread_detach(id);
if(rc != 0)
{
- throw ThreadSyscallException(__FILE__, __LINE__);
+ throw ThreadSyscallException(__FILE__, __LINE__, rc);
}
}
@@ -475,6 +475,10 @@ startHook(void* arg)
cerr << "IceUtil::Thread::run(): uncaught exception: ";
cerr << e << endl;
}
+ catch(...)
+ {
+ cerr << "IceUtil::Thread::run(): uncaught exception" << endl;
+ }
return 0;
}
}
@@ -503,7 +507,7 @@ IceUtil::Thread::start()
if(rc != 0)
{
__decRef();
- throw ThreadSyscallException(__FILE__, __LINE__);
+ throw ThreadSyscallException(__FILE__, __LINE__, rc);
}
_started = true;
diff --git a/cpp/src/IceUtil/ThreadException.cpp b/cpp/src/IceUtil/ThreadException.cpp
index 86fd1e20852..857fc962987 100644
--- a/cpp/src/IceUtil/ThreadException.cpp
+++ b/cpp/src/IceUtil/ThreadException.cpp
@@ -16,13 +16,9 @@
using namespace std;
-IceUtil::ThreadSyscallException::ThreadSyscallException(const char* file, int line) :
+IceUtil::ThreadSyscallException::ThreadSyscallException(const char* file, int line, int err ):
Exception(file, line),
-#ifdef _WIN32
- _error(GetLastError())
-#else
- _error(errno)
-#endif
+ _error(err)
{
}
@@ -80,6 +76,13 @@ IceUtil::ThreadSyscallException::ice_throw() const
throw *this;
}
+int
+IceUtil::ThreadSyscallException::error() const
+{
+ return _error;
+}
+
+
IceUtil::ThreadLockedException::ThreadLockedException(const char* file, int line) :
Exception(file, line)
{
diff --git a/cpp/src/IceUtil/UUID.cpp b/cpp/src/IceUtil/UUID.cpp
index 2ba8a2c1709..9fe4ea8eb64 100644
--- a/cpp/src/IceUtil/UUID.cpp
+++ b/cpp/src/IceUtil/UUID.cpp
@@ -20,8 +20,6 @@
// (/dev/random) to generate "version 4" UUIDs, as described in
// http://www.ietf.org/internet-drafts/draft-mealling-uuid-urn-00.txt
-#include <assert.h>
-
#ifdef _WIN32
# include <rpc.h>
#else
@@ -59,7 +57,8 @@ inline void bytesToHex(unsigned char* bytes, int len, char*& hexBuffer)
IceUtil::UUIDGenerationException::UUIDGenerationException(const char* file, int line) :
Exception(file, line)
-{}
+{
+}
string
IceUtil::UUIDGenerationException::ice_name() const