diff options
author | Vincent Richard <[email protected]> | 2007-05-22 19:25:59 +0000 |
---|---|---|
committer | Vincent Richard <[email protected]> | 2007-05-22 19:25:59 +0000 |
commit | 8c6ba0e0587b4b28044385da90af408d72a0da01 (patch) | |
tree | 9a5ff41766dc96c9808bccc1777b305ed4d78022 /src/utility/smartPtr.cpp | |
parent | Fail if GNU TLS or GNU SASL dev packages are required but not installed. (diff) | |
download | vmime-8c6ba0e0587b4b28044385da90af408d72a0da01.tar.gz vmime-8c6ba0e0587b4b28044385da90af408d72a0da01.zip |
Fixed bug #1721186: thread-safe reference counting.
Diffstat (limited to 'src/utility/smartPtr.cpp')
-rw-r--r-- | src/utility/smartPtr.cpp | 131 |
1 files changed, 3 insertions, 128 deletions
diff --git a/src/utility/smartPtr.cpp b/src/utility/smartPtr.cpp index 16249df8..08a8f57c 100644 --- a/src/utility/smartPtr.cpp +++ b/src/utility/smartPtr.cpp @@ -29,135 +29,10 @@ namespace vmime { namespace utility { -// refManager - -refManager::refManager(object* obj) - : m_object(obj), m_strongCount(1), m_weakCount(1) -{ -} - - -refManager::~refManager() -{ -} - - -const bool refManager::addStrong() -{ - if (m_strongCount <= 0) - return false; - - m_strongCount.increment(); - m_weakCount.increment(); - - return true; -} - - -void refManager::releaseStrong() -{ - m_strongCount.decrement(); - - if (m_strongCount.compareExchange(0, -424242) == 0) // prevent from adding strong refs later - deleteObject(); - - releaseWeak(); -} - - -void refManager::addWeak() -{ - m_weakCount.increment(); -} - - -void refManager::releaseWeak() -{ - if (m_weakCount.decrement() == 0) - deleteManager(); -} - - -object* refManager::getObject() -{ - return m_object; -} - - -void refManager::deleteManager() -{ - delete this; -} - - -void refManager::deleteObject() -{ - try - { - m_object->setRefManager(0); - delete m_object; - } - catch (...) - { - // Exception in destructor - } - - m_object = 0; -} - - -const long refManager::getStrongRefCount() const -{ - return m_strongCount; -} - - -const long refManager::getWeakRefCount() const -{ - return m_weakCount; -} - - - -// refCounter - -refCounter::refCounter(const long initialValue) - : m_value(initialValue) -{ -} - - -refCounter::~refCounter() -{ -} - - -const long refCounter::increment() -{ - return ++m_value; -} - - -const long refCounter::decrement() -{ - return --m_value; -} - - -const long refCounter::compareExchange(const long compare, const long exchangeWith) -{ - const int prev = m_value; - - if (m_value == compare) - m_value = exchangeWith; - - return prev; -} - - -refCounter::operator long() const +void refManager::deleteObjectImpl(object* obj) { - return m_value; + obj->setRefManager(0); + delete obj; } |