From ae1aa461ea3f96c092e2a50ae40f290b03b25356 Mon Sep 17 00:00:00 2001 From: "Graham Madarasz (Graham)" Date: Thu, 28 Feb 2013 16:37:09 -0800 Subject: Attempt at a faster ThreadSafeRefCount class --- indra/llcommon/llthread.h | 40 ++++++++++++++++++---------------------- 1 file changed, 18 insertions(+), 22 deletions(-) (limited to 'indra/llcommon/llthread.h') diff --git a/indra/llcommon/llthread.h b/indra/llcommon/llthread.h index 5c8bbca2ca..c2f4184a8a 100644 --- a/indra/llcommon/llthread.h +++ b/indra/llcommon/llthread.h @@ -241,47 +241,43 @@ public: LLThreadSafeRefCount(const LLThreadSafeRefCount&); LLThreadSafeRefCount& operator=(const LLThreadSafeRefCount& ref) { - if (sMutex) - { - sMutex->lock(); - } mRef = 0; - if (sMutex) - { - sMutex->unlock(); - } return *this; } - - void ref() { - if (sMutex) sMutex->lock(); mRef++; - if (sMutex) sMutex->unlock(); } S32 unref() { - llassert(mRef >= 1); - if (sMutex) sMutex->lock(); - S32 res = --mRef; - if (sMutex) sMutex->unlock(); - if (0 == res) + llassert(mRef >= 1); + bool time_to_die = (mRef == 1); + if (time_to_die) { - delete this; + if (sMutex) sMutex->lock(); + // Looks redundant, but is very much not + // We need to check again once we've acquired the lock + // so that two threads who get into the if in parallel + // don't both attempt to the delete. + // + if (mRef == 1) + delete this; + mRef--; + if (sMutex) sMutex->unlock(); return 0; } - return res; - } + return --mRef; + } S32 getNumRefs() const { - return mRef; + const S32 currentVal = mRef.CurrentValue(); + return currentVal; } private: - S32 mRef; + LLAtomic32< S32 > mRef; }; //============================================================================ -- cgit v1.2.3 From dfda8826eb4654845430520dac48c011e058e1c0 Mon Sep 17 00:00:00 2001 From: "Graham Madarasz (Graham)" Date: Fri, 1 Mar 2013 11:21:35 -0800 Subject: Make WL updates use pre-hashed strings for uniform sets --- indra/llcommon/llthread.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'indra/llcommon/llthread.h') diff --git a/indra/llcommon/llthread.h b/indra/llcommon/llthread.h index c2f4184a8a..0d22bc863d 100644 --- a/indra/llcommon/llthread.h +++ b/indra/llcommon/llthread.h @@ -262,9 +262,9 @@ public: // so that two threads who get into the if in parallel // don't both attempt to the delete. // - if (mRef == 1) - delete this; mRef--; + if (mRef == 0) + delete this; if (sMutex) sMutex->unlock(); return 0; } -- cgit v1.2.3 From 6742d90d39fbb5c5bc3b675f37841fd6c8ec09c9 Mon Sep 17 00:00:00 2001 From: simon Date: Wed, 17 Apr 2013 11:17:46 -0700 Subject: Some minor cleanups while hunting crashes. Reviewed by Kelly --- indra/llcommon/llthread.h | 2 ++ 1 file changed, 2 insertions(+) (limited to 'indra/llcommon/llthread.h') diff --git a/indra/llcommon/llthread.h b/indra/llcommon/llthread.h index 8c95b1c0e5..11594f276e 100644 --- a/indra/llcommon/llthread.h +++ b/indra/llcommon/llthread.h @@ -311,4 +311,6 @@ public: //============================================================================ +extern LL_COMMON_API void assert_main_thread(); + #endif // LL_LLTHREAD_H -- cgit v1.2.3 From e2eca610459362eda50f6a5edfb9ed5269fd5e51 Mon Sep 17 00:00:00 2001 From: simon Date: Tue, 23 Apr 2013 16:31:20 -0700 Subject: Revert LLThreadSafeRefCount optimization; caps fetching was failing. Reviewed by Kelly --- indra/llcommon/llthread.h | 68 ++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 67 insertions(+), 1 deletion(-) (limited to 'indra/llcommon/llthread.h') diff --git a/indra/llcommon/llthread.h b/indra/llcommon/llthread.h index 11594f276e..a6eb14db9d 100644 --- a/indra/llcommon/llthread.h +++ b/indra/llcommon/llthread.h @@ -225,6 +225,71 @@ void LLThread::unlockData() // see llmemory.h for LLPointer<> definition +#if (1) // Old code - see comment below +class LL_COMMON_API LLThreadSafeRefCount +{ +public: + static void initThreadSafeRefCount(); // creates sMutex + static void cleanupThreadSafeRefCount(); // destroys sMutex + +private: + static LLMutex* sMutex; + +protected: + virtual ~LLThreadSafeRefCount(); // use unref() + +public: + LLThreadSafeRefCount(); + LLThreadSafeRefCount(const LLThreadSafeRefCount&); + LLThreadSafeRefCount& operator=(const LLThreadSafeRefCount& ref) + { + if (sMutex) + { + sMutex->lock(); + } + mRef = 0; + if (sMutex) + { + sMutex->unlock(); + } + return *this; + } + + + + void ref() + { + if (sMutex) sMutex->lock(); + mRef++; + if (sMutex) sMutex->unlock(); + } + + S32 unref() + { + llassert(mRef >= 1); + if (sMutex) sMutex->lock(); + S32 res = --mRef; + if (sMutex) sMutex->unlock(); + if (0 == res) + { + delete this; + return 0; + } + return res; + } + S32 getNumRefs() const + { + return mRef; + } + +private: + S32 mRef; +}; + +#else + // New code - This was from https://bitbucket.org/lindenlab/viewer-cat/commits/b03bb43e4ead57f904cb3c1e9745dc8460de6efc + // and attempts + class LL_COMMON_API LLThreadSafeRefCount { public: @@ -263,7 +328,7 @@ public: // so that two threads who get into the if in parallel // don't both attempt to the delete. // - mRef--; + mRef--; // Simon: why not if (mRef == 1) delete this; ? There still seems to be a window where mRef could be modified if (mRef == 0) delete this; if (sMutex) sMutex->unlock(); @@ -280,6 +345,7 @@ public: private: LLAtomic32< S32 > mRef; }; +#endif // new code /** * intrusive pointer support for LLThreadSafeRefCount -- cgit v1.2.3 From 39638b0de3734fea2d112161a3289b67f7594ba3 Mon Sep 17 00:00:00 2001 From: simon Date: Fri, 26 Apr 2013 15:54:03 -0700 Subject: Convert LLThreadSafeRefCount back to atomic ref counting. Reviewed by Kelly --- indra/llcommon/llthread.h | 90 +++++------------------------------------------ 1 file changed, 9 insertions(+), 81 deletions(-) (limited to 'indra/llcommon/llthread.h') diff --git a/indra/llcommon/llthread.h b/indra/llcommon/llthread.h index a6eb14db9d..f51d985b5f 100644 --- a/indra/llcommon/llthread.h +++ b/indra/llcommon/llthread.h @@ -225,7 +225,6 @@ void LLThread::unlockData() // see llmemory.h for LLPointer<> definition -#if (1) // Old code - see comment below class LL_COMMON_API LLThreadSafeRefCount { public: @@ -243,99 +242,28 @@ public: LLThreadSafeRefCount(const LLThreadSafeRefCount&); LLThreadSafeRefCount& operator=(const LLThreadSafeRefCount& ref) { - if (sMutex) - { - sMutex->lock(); - } mRef = 0; - if (sMutex) - { - sMutex->unlock(); - } return *this; } - - void ref() { - if (sMutex) sMutex->lock(); mRef++; - if (sMutex) sMutex->unlock(); } - S32 unref() + void unref() { llassert(mRef >= 1); - if (sMutex) sMutex->lock(); - S32 res = --mRef; - if (sMutex) sMutex->unlock(); - if (0 == res) - { - delete this; - return 0; + if ((--mRef) == 0) // See note in llapr.h on atomic decrement operator return value. + { + // If we hit zero, the caller should be the only smart pointer owning the object and we can delete it. + // It is technically possible for a vanilla pointer to mess this up, or another thread to + // jump in, find this object, create another smart pointer and end up dangling, but if + // the code is that bad and not thread-safe, it's trouble already. + delete this; } - return res; - } - S32 getNumRefs() const - { - return mRef; } -private: - S32 mRef; -}; - -#else - // New code - This was from https://bitbucket.org/lindenlab/viewer-cat/commits/b03bb43e4ead57f904cb3c1e9745dc8460de6efc - // and attempts - -class LL_COMMON_API LLThreadSafeRefCount -{ -public: - static void initThreadSafeRefCount(); // creates sMutex - static void cleanupThreadSafeRefCount(); // destroys sMutex - -private: - static LLMutex* sMutex; - -protected: - virtual ~LLThreadSafeRefCount(); // use unref() - -public: - LLThreadSafeRefCount(); - LLThreadSafeRefCount(const LLThreadSafeRefCount&); - LLThreadSafeRefCount& operator=(const LLThreadSafeRefCount& ref) - { - mRef = 0; - return *this; - } - - void ref() - { - mRef++; - } - - S32 unref() - { - llassert(mRef >= 1); - bool time_to_die = (mRef == 1); - if (time_to_die) - { - if (sMutex) sMutex->lock(); - // Looks redundant, but is very much not - // We need to check again once we've acquired the lock - // so that two threads who get into the if in parallel - // don't both attempt to the delete. - // - mRef--; // Simon: why not if (mRef == 1) delete this; ? There still seems to be a window where mRef could be modified - if (mRef == 0) - delete this; - if (sMutex) sMutex->unlock(); - return 0; - } - return --mRef; - } S32 getNumRefs() const { const S32 currentVal = mRef.CurrentValue(); @@ -345,7 +273,7 @@ public: private: LLAtomic32< S32 > mRef; }; -#endif // new code + /** * intrusive pointer support for LLThreadSafeRefCount -- cgit v1.2.3