summaryrefslogtreecommitdiff
path: root/cpp/include/IceUtil
diff options
context:
space:
mode:
Diffstat (limited to 'cpp/include/IceUtil')
-rw-r--r--cpp/include/IceUtil/Cond.h8
-rw-r--r--cpp/include/IceUtil/Config.h108
-rw-r--r--cpp/include/IceUtil/Shared.h97
3 files changed, 117 insertions, 96 deletions
diff --git a/cpp/include/IceUtil/Cond.h b/cpp/include/IceUtil/Cond.h
index 37973c059c3..f5b1d62220e 100644
--- a/cpp/include/IceUtil/Cond.h
+++ b/cpp/include/IceUtil/Cond.h
@@ -100,6 +100,10 @@ public:
template <typename Lock> inline void
wait(const Lock& lock) const
{
+ if (!lock.acquired())
+ {
+ throw ThreadLockedException(__FILE__, __LINE__);
+ }
waitImpl(lock._mutex);
}
@@ -113,6 +117,10 @@ public:
template <typename Lock> inline bool
timedWait(const Lock& lock, const Time& timeout) const
{
+ if (!lock.acquired())
+ {
+ throw ThreadLockedException(__FILE__, __LINE__);
+ }
return timedWaitImpl(lock._mutex, timeout);
}
diff --git a/cpp/include/IceUtil/Config.h b/cpp/include/IceUtil/Config.h
index 0e2a07026da..b67de990c41 100644
--- a/cpp/include/IceUtil/Config.h
+++ b/cpp/include/IceUtil/Config.h
@@ -17,13 +17,49 @@
//
-// Endianness: define ICE_UTIL_BIGENDIAN on big endian platforms,
-// nothing on little endian platforms.
+// Endianness
+// Most CPUs support only one endianness, with the notable exceptions
+// of Itanium (IA64) and MIPS.
+//
+#if defined(__i386) || defined(_M_IX86)
+# define ICE_LITTLE_ENDIAN
+#elif defined(__sparc)
+# define ICE_BIG_ENDIAN
+#else
+# error "Unknown architecture"
+#endif
-#if defined(__sparc)
-#define ICE_UTIL_BIGENDIAN
+//
+// 32 or 64 bit mode?
+//
+#if (defined(__sun) && defined(__sparcv9))
+# define ICE_64
+#else
+# define ICE_32
#endif
+//
+// Compiler extensions to export and import symbols: see the documentation
+// for Visual C++ and HP aC++.
+//
+// TODO: more macros to support IBM Visual Age _Export syntax as well.
+//
+#ifdef _MSC_VER
+# define ICE_DECLSPEC_EXPORT __declspec(dllexport)
+# define ICE_DECLSPEC_IMPORT __declspec(dllimport)
+#else
+# define ICE_DECLSPEC_EXPORT /**/
+# define ICE_DECLSPEC_IMPORT /**/
+#endif
+
+//
+// Let's use these extensions with for IceUtil:
+//
+#ifdef ICE_UTIL_API_EXPORTS
+# define ICE_UTIL_API ICE_DECLSPEC_EXPORT
+#else
+# define ICE_UTIL_API ICE_DECLSPEC_IMPORT
+#endif
//
// For STLport. If we compile in debug mode, we want to use the debug
@@ -47,12 +83,6 @@
# error "Only multi-threaded DLL libraries can be used with Ice!"
# endif
-# ifdef ICE_UTIL_API_EXPORTS
-# define ICE_UTIL_API __declspec(dllexport)
-# else
-# define ICE_UTIL_API __declspec(dllimport)
-# endif
-
# include <windows.h>
// '...' : forcing value to bool 'true' or 'false' (performance warning)
@@ -70,14 +100,9 @@
// ...: decorated name length exceeded, name was truncated
# pragma warning( disable : 4503 )
-# define SIZEOF_WCHAR_T 2
-
-#elif (defined(__linux) || defined(__FreeBSD__)) && defined(__i386)
-
-# define ICE_UTIL_API /**/
-# define HAVE_READLINE
-# define SIZEOF_WCHAR_T 4
-
+#elif defined(__sun) && defined(__sparc)
+# include <inttypes.h>
+#else
//
// The ISO C99 standard specifies that in C++ implementations the
// macros for minimum/maximum integer values should only be defined if
@@ -85,20 +110,8 @@
//
# define __STDC_LIMIT_MACROS
# include <stdint.h>
-
-#elif defined(__sun) && defined(__sparc)
-
-# define ICE_UTIL_API /**/
-# define SIZEOF_WCHAR_T 4
-# include <inttypes.h>
-
-#else
-
-# error "unsupported operating system or platform"
-
#endif
-
//
// Some include files we need almost everywhere.
//
@@ -139,32 +152,37 @@ private:
//
// Some definitions for 64-bit integers.
//
-#if defined(_WIN32)
-
+#if defined(_MSC_VER)
typedef __int64 Int64;
const Int64 Int64Min = -9223372036854775808i64;
const Int64 Int64Max = 9223372036854775807i64;
-# define ICE_INT64(x) Int64(x##i64)
+#elif defined(__SUNPRO_CC)
+# if defined(ICE_64)
+typedef long Int64;
+const Int64 Int64Min = -0x7fffffffffffffffL-1L;
+const Int64 Int64Max = 0x7fffffffffffffffL;
+# else
+typedef long long Int64;
+const Int64 Int64Min = -0x7fffffffffffffffLL-1LL;
+const Int64 Int64Max = 0x7fffffffffffffffLL;
+# endif
#else
-
-# if defined(INT64_MIN) && defined(INT64_MAX)
-
+// Assumes ISO C99 types
+//
typedef int64_t Int64;
const Int64 Int64Min = INT64_MIN;
const Int64 Int64Max = INT64_MAX;
-# else
-
-typedef long long Int64;
-const Int64 Int64Min = -0x7fffffffffffffffLL-1LL;
-const Int64 Int64Max = 0x7fffffffffffffffLL;
-
-# endif
-
-#define ICE_INT64(x) Int64(x##LL)
+#endif
+#if defined(_MSC_VER)
+# define ICE_INT64(n) n##i64
+#elif defined(ICE_64)
+# define ICE_INT64(n) n##L
+#else
+# define ICE_INT64(n) n##LL
#endif
}
diff --git a/cpp/include/IceUtil/Shared.h b/cpp/include/IceUtil/Shared.h
index aae20dc017a..7666f999dbd 100644
--- a/cpp/include/IceUtil/Shared.h
+++ b/cpp/include/IceUtil/Shared.h
@@ -17,19 +17,10 @@
#include <IceUtil/Config.h>
-//
-// The inline assembler causes problems with shared libraries.
-//
-#if (defined(__ICC) && !defined(_WIN32)) || defined (__sun)
-# define ICE_USE_MUTEX_SHARED
-#endif
-
-#ifdef ICE_USE_MUTEX_SHARED
-# include <IceUtil/Mutex.h>
-#endif
-
-#if !defined(_WIN32) && !defined(ICE_USE_MUTEX_SHARED)
+#if (defined(__linux) || defined(__FreeBSD__)) && defined(__i386) && !defined(__ICC)
+# define ICE_HAS_ATOMIC_FUNCTIONS
+// __ICC: The inline assembler causes problems with shared libraries.
//
// Linux only. Unfortunately, asm/atomic.h builds non-SMP safe code
// with non-SMP kernels. This means that executables compiled with a
@@ -116,6 +107,11 @@ inline int ice_atomic_exchange_add(int i, ice_atomic_t* v)
return tmp + i;
}
+#elif defined(_WIN32)
+// Nothing to include
+#else
+// Use a simple mutex
+# include <IceUtil/Mutex.h>
#endif
//
@@ -203,13 +199,13 @@ public:
private:
-#ifdef ICE_USE_MUTEX_SHARED
- int _ref;
- Mutex _mutex;
-#elif defined(_WIN32)
+#if defined(_WIN32)
LONG _ref;
-#else
+#elif defined(ICE_HAS_ATOMIC_FUNCTIONS)
ice_atomic_t _ref;
+#else
+ int _ref;
+ Mutex _mutex;
#endif
bool _noDelete;
};
@@ -219,7 +215,7 @@ private:
// all of them should be inlined.
//
-#ifdef ICE_USE_MUTEX_SHARED
+#if defined(_WIN32)
inline
Shared::Shared() :
@@ -236,26 +232,17 @@ Shared::~Shared()
inline void
Shared::__incRef()
{
- _mutex.lock();
- assert(_ref >= 0);
- ++_ref;
- _mutex.unlock();
+ assert(InterlockedExchangeAdd(&_ref, 0) >= 0);
+ InterlockedIncrement(&_ref);
}
inline void
Shared::__decRef()
{
- _mutex.lock();
- bool doDelete = false;
- assert(_ref > 0);
- if(--_ref == 0)
+ assert(InterlockedExchangeAdd(&_ref, 0) > 0);
+ if(InterlockedDecrement(&_ref) == 0 && !_noDelete)
{
- doDelete = !_noDelete;
_noDelete = true;
- }
- _mutex.unlock();
- if(doDelete)
- {
delete this;
}
}
@@ -263,27 +250,22 @@ Shared::__decRef()
inline int
Shared::__getRef() const
{
- _mutex.lock();
- int ref = _ref;
- _mutex.unlock();
- return ref;
+ return InterlockedExchangeAdd(const_cast<LONG*>(&_ref), 0);
}
inline void
Shared::__setNoDelete(bool b)
{
- _mutex.lock();
_noDelete = b;
- _mutex.unlock();
}
-#elif defined(_WIN32)
+#elif defined(ICE_HAS_ATOMIC_FUNCTIONS)
inline
Shared::Shared() :
- _ref(0),
_noDelete(false)
{
+ ice_atomic_set(&_ref, 0);
}
inline
@@ -294,15 +276,15 @@ Shared::~Shared()
inline void
Shared::__incRef()
{
- assert(InterlockedExchangeAdd(&_ref, 0) >= 0);
- InterlockedIncrement(&_ref);
+ assert(ice_atomic_exchange_add(0, &_ref) >= 0);
+ ice_atomic_inc(&_ref);
}
inline void
Shared::__decRef()
{
- assert(InterlockedExchangeAdd(&_ref, 0) > 0);
- if(InterlockedDecrement(&_ref) == 0 && !_noDelete)
+ assert(ice_atomic_exchange_add(0, &_ref) > 0);
+ if(ice_atomic_dec_and_test(&_ref) && !_noDelete)
{
_noDelete = true;
delete this;
@@ -312,7 +294,7 @@ Shared::__decRef()
inline int
Shared::__getRef() const
{
- return InterlockedExchangeAdd(const_cast<LONG*>(&_ref), 0);
+ return ice_atomic_exchange_add(0, const_cast<ice_atomic_t*>(&_ref));
}
inline void
@@ -325,9 +307,9 @@ Shared::__setNoDelete(bool b)
inline
Shared::Shared() :
+ _ref(0),
_noDelete(false)
{
- ice_atomic_set(&_ref, 0);
}
inline
@@ -338,17 +320,26 @@ Shared::~Shared()
inline void
Shared::__incRef()
{
- assert(ice_atomic_exchange_add(0, &_ref) >= 0);
- ice_atomic_inc(&_ref);
+ _mutex.lock();
+ assert(_ref >= 0);
+ ++_ref;
+ _mutex.unlock();
}
inline void
Shared::__decRef()
{
- assert(ice_atomic_exchange_add(0, &_ref) > 0);
- if(ice_atomic_dec_and_test(&_ref) && !_noDelete)
+ _mutex.lock();
+ bool doDelete = false;
+ assert(_ref > 0);
+ if(--_ref == 0)
{
+ doDelete = !_noDelete;
_noDelete = true;
+ }
+ _mutex.unlock();
+ if(doDelete)
+ {
delete this;
}
}
@@ -356,17 +347,21 @@ Shared::__decRef()
inline int
Shared::__getRef() const
{
- return ice_atomic_exchange_add(0, const_cast<ice_atomic_t*>(&_ref));
+ _mutex.lock();
+ int ref = _ref;
+ _mutex.unlock();
+ return ref;
}
inline void
Shared::__setNoDelete(bool b)
{
+ _mutex.lock();
_noDelete = b;
+ _mutex.unlock();
}
#endif
}
-
#endif