summaryrefslogtreecommitdiff
path: root/cpp/src/IceUtil/Shared.cpp
diff options
context:
space:
mode:
authorBernard Normier <bernard@zeroc.com>2007-08-23 14:56:08 -0400
committerBernard Normier <bernard@zeroc.com>2007-08-23 14:56:08 -0400
commit0664ab6ebfe794441a5d2f15507f449231384c85 (patch)
tree7df11c9a0d891c62cb284b62e1416b2742788ee2 /cpp/src/IceUtil/Shared.cpp
parenthttp://bugzilla.zeroc.com/bugzilla/show_bug.cgi?id=2426 - logger date/time fo... (diff)
downloadice-0664ab6ebfe794441a5d2f15507f449231384c85.tar.bz2
ice-0664ab6ebfe794441a5d2f15507f449231384c85.tar.xz
ice-0664ab6ebfe794441a5d2f15507f449231384c85.zip
Fixed bug #2233
Diffstat (limited to 'cpp/src/IceUtil/Shared.cpp')
-rwxr-xr-xcpp/src/IceUtil/Shared.cpp169
1 files changed, 110 insertions, 59 deletions
diff --git a/cpp/src/IceUtil/Shared.cpp b/cpp/src/IceUtil/Shared.cpp
index a0fa6a59f69..48823e3f21e 100755
--- a/cpp/src/IceUtil/Shared.cpp
+++ b/cpp/src/IceUtil/Shared.cpp
@@ -6,62 +6,113 @@
// ICE_LICENSE file included in this distribution.
//
// **********************************************************************
-
-#include <IceUtil/Shared.h>
-
-using namespace IceUtil;
-
-IceUtil::SimpleShared::SimpleShared() :
- _ref(0),
- _noDelete(false)
-{
-}
-
-IceUtil::SimpleShared::SimpleShared(const SimpleShared&) :
- _ref(0),
- _noDelete(false)
-{
-}
-
-IceUtil::Shared::Shared() :
-#ifndef ICE_HAS_ATOMIC_FUNCTIONS
- _ref(0),
-#endif
- _noDelete(false)
-{
-#ifdef ICE_HAS_ATOMIC_FUNCTIONS
- ice_atomic_set(&_ref, 0);
-#endif
-}
-
-IceUtil::Shared::Shared(const Shared&) :
-#ifndef ICE_HAS_ATOMIC_FUNCTIONS
- _ref(0),
-#endif
- _noDelete(false)
-{
-#ifdef ICE_HAS_ATOMIC_FUNCTIONS
- ice_atomic_set(&_ref, 0);
-#endif
-}
-
-int
-IceUtil::Shared::__getRef() const
-{
-#if defined(_WIN32)
- return InterlockedExchangeAdd(const_cast<LONG*>(&_ref), 0);
-#elif defined(ICE_HAS_ATOMIC_FUNCTIONS)
- return ice_atomic_exchange_add(0, const_cast<ice_atomic_t*>(&_ref));
-#else
- _mutex.lock();
- int ref = _ref;
- _mutex.unlock();
- return ref;
-#endif
-}
-
-void
-IceUtil::Shared::__setNoDelete(bool b)
-{
- _noDelete = b;
-}
+
+#include <IceUtil/Shared.h>
+
+using namespace IceUtil;
+
+IceUtil::SimpleShared::SimpleShared() :
+ _ref(0),
+ _noDelete(false)
+{
+}
+
+IceUtil::SimpleShared::SimpleShared(const SimpleShared&) :
+ _ref(0),
+ _noDelete(false)
+{
+}
+
+IceUtil::Shared::Shared() :
+#ifndef ICE_HAS_ATOMIC_FUNCTIONS
+ _ref(0),
+#endif
+ _noDelete(false)
+{
+#ifdef ICE_HAS_ATOMIC_FUNCTIONS
+ ice_atomic_set(&_ref, 0);
+#endif
+}
+
+IceUtil::Shared::Shared(const Shared&) :
+#ifndef ICE_HAS_ATOMIC_FUNCTIONS
+ _ref(0),
+#endif
+ _noDelete(false)
+{
+#ifdef ICE_HAS_ATOMIC_FUNCTIONS
+ ice_atomic_set(&_ref, 0);
+#endif
+}
+
+void
+IceUtil::Shared::__incRef()
+{
+#if defined(_WIN32)
+ assert(InterlockedExchangeAdd(&_ref, 0) >= 0);
+ InterlockedIncrement(&_ref);
+#elif defined(ICE_HAS_ATOMIC_FUNCTIONS)
+ assert(ice_atomic_exchange_add(0, &_ref) >= 0);
+ ice_atomic_inc(&_ref);
+#else
+ _mutex.lock();
+ assert(_ref >= 0);
+ ++_ref;
+ _mutex.unlock();
+#endif
+}
+
+void
+IceUtil::Shared::__decRef()
+{
+#if defined(_WIN32)
+ assert(InterlockedExchangeAdd(&_ref, 0) > 0);
+ if(InterlockedDecrement(&_ref) == 0 && !_noDelete)
+ {
+ _noDelete = true;
+ delete this;
+ }
+#elif defined(ICE_HAS_ATOMIC_FUNCTIONS)
+ assert(ice_atomic_exchange_add(0, &_ref) > 0);
+ if(ice_atomic_dec_and_test(&_ref) && !_noDelete)
+ {
+ _noDelete = true;
+ delete this;
+ }
+#else
+ _mutex.lock();
+ bool doDelete = false;
+ assert(_ref > 0);
+ if(--_ref == 0)
+ {
+ doDelete = !_noDelete;
+ _noDelete = true;
+ }
+ _mutex.unlock();
+ if(doDelete)
+ {
+ delete this;
+ }
+#endif
+}
+
+int
+IceUtil::Shared::__getRef() const
+{
+#if defined(_WIN32)
+ return InterlockedExchangeAdd(const_cast<LONG*>(&_ref), 0);
+#elif defined(ICE_HAS_ATOMIC_FUNCTIONS)
+ return ice_atomic_exchange_add(0, const_cast<ice_atomic_t*>(&_ref));
+#else
+ _mutex.lock();
+ int ref = _ref;
+ _mutex.unlock();
+ return ref;
+#endif
+}
+
+void
+IceUtil::Shared::__setNoDelete(bool b)
+{
+ _noDelete = b;
+}