From fa15830e02ed249186625e845e2ac19749d10193 Mon Sep 17 00:00:00 2001
From: andreykproductengine <andreykproductengine@lindenlab.com>
Date: Tue, 15 Jan 2019 18:31:17 +0200
Subject: SL-10291 Replace apr_atomic with standard C++11 functionality

---
 indra/llcommon/CMakeLists.txt        |  2 ++
 indra/llcommon/llapp.h               |  3 +-
 indra/llcommon/llapr.h               | 28 ---------------
 indra/llcommon/llatomic.cpp          | 28 +++++++++++++++
 indra/llcommon/llatomic.h            | 69 ++++++++++++++++++++++++++++++++++++
 indra/llcommon/llinstancetracker.cpp |  7 ++--
 indra/llcommon/llinstancetracker.h   |  9 +++--
 indra/llcommon/llmutex.cpp           |  3 --
 indra/llcommon/llqueuedthread.h      |  6 ++--
 indra/llcommon/llrefcount.h          |  8 ++---
 indra/llcommon/llworkerthread.h      |  2 +-
 11 files changed, 118 insertions(+), 47 deletions(-)
 create mode 100644 indra/llcommon/llatomic.cpp
 create mode 100644 indra/llcommon/llatomic.h

(limited to 'indra/llcommon')

diff --git a/indra/llcommon/CMakeLists.txt b/indra/llcommon/CMakeLists.txt
index d9eb13d65a..ac19e6d025 100644
--- a/indra/llcommon/CMakeLists.txt
+++ b/indra/llcommon/CMakeLists.txt
@@ -35,6 +35,7 @@ set(llcommon_SOURCE_FILES
     llapp.cpp
     llapr.cpp
     llassettype.cpp
+    llatomic.cpp
     llbase32.cpp
     llbase64.cpp
     llbitpack.cpp
@@ -135,6 +136,7 @@ set(llcommon_HEADER_FILES
     llapp.h
     llapr.h
     llassettype.h
+    llatomic.h
     llbase32.h
     llbase64.h
     llbitpack.h
diff --git a/indra/llcommon/llapp.h b/indra/llcommon/llapp.h
index acd829d864..245c73e3a2 100644
--- a/indra/llcommon/llapp.h
+++ b/indra/llcommon/llapp.h
@@ -30,9 +30,8 @@
 #include <map>
 #include "llrun.h"
 #include "llsd.h"
+#include <atomic>
 // Forward declarations
-template <typename Type> class LLAtomic32;
-typedef LLAtomic32<U32> LLAtomicU32;
 class LLErrorThread;
 class LLLiveFile;
 #if LL_LINUX
diff --git a/indra/llcommon/llapr.h b/indra/llcommon/llapr.h
index 73d1d180ef..da50dda103 100644
--- a/indra/llcommon/llapr.h
+++ b/indra/llcommon/llapr.h
@@ -38,7 +38,6 @@
 #include "apr_thread_proc.h"
 #include "apr_getopt.h"
 #include "apr_signal.h"
-#include "apr_atomic.h"
 
 #include "llstring.h"
 
@@ -131,33 +130,6 @@ private:
 	std::unique_ptr<std::mutex> mMutexp;
 } ;
 
-template <typename Type> class LLAtomic32
-{
-public:
-	LLAtomic32<Type>() {};
-	LLAtomic32<Type>(Type x) {apr_atomic_set32(&mData, apr_uint32_t(x)); };
-	~LLAtomic32<Type>() {};
-
-	operator const Type() { apr_uint32_t data = apr_atomic_read32(&mData); return Type(data); }
-	
-	Type	CurrentValue() const { apr_uint32_t data = apr_atomic_read32(const_cast< volatile apr_uint32_t* >(&mData)); return Type(data); }
-
-	Type operator =(const Type& x) { apr_atomic_set32(&mData, apr_uint32_t(x)); return Type(mData); }
-	void operator -=(Type x) { apr_atomic_sub32(&mData, apr_uint32_t(x)); }
-	void operator +=(Type x) { apr_atomic_add32(&mData, apr_uint32_t(x)); }
-	Type operator ++(int) { return apr_atomic_inc32(&mData); } // Type++
-	Type operator --(int) { return apr_atomic_dec32(&mData); } // approximately --Type (0 if final is 0, non-zero otherwise)
-
-	Type operator ++() { return apr_atomic_inc32(&mData); } // Type++
-	Type operator --() { return apr_atomic_dec32(&mData); } // approximately --Type (0 if final is 0, non-zero otherwise)
-	
-private:
-	volatile apr_uint32_t mData;
-};
-
-typedef LLAtomic32<U32> LLAtomicU32;
-typedef LLAtomic32<S32> LLAtomicS32;
-
 // File IO convenience functions.
 // Returns NULL if the file fails to open, sets *sizep to file size if not NULL
 // abbreviated flags
diff --git a/indra/llcommon/llatomic.cpp b/indra/llcommon/llatomic.cpp
new file mode 100644
index 0000000000..93aba1f460
--- /dev/null
+++ b/indra/llcommon/llatomic.cpp
@@ -0,0 +1,28 @@
+/** 
+ * @file llatomic.cpp
+ *
+ * $LicenseInfo:firstyear=2018&license=viewerlgpl$
+ * Second Life Viewer Source Code
+ * Copyright (C) 2018, Linden Research, Inc.
+ * 
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation;
+ * version 2.1 of the License only.
+ * 
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ * 
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
+ * 
+ * Linden Research, Inc., 945 Battery Street, San Francisco, CA  94111  USA
+ * $/LicenseInfo$
+ */
+
+#include "llatomic.h"
+
+//============================================================================
diff --git a/indra/llcommon/llatomic.h b/indra/llcommon/llatomic.h
new file mode 100644
index 0000000000..8de773846c
--- /dev/null
+++ b/indra/llcommon/llatomic.h
@@ -0,0 +1,69 @@
+/** 
+ * @file llatomic.h
+ * @brief Base classes for atomic.
+ *
+ * $LicenseInfo:firstyear=2018&license=viewerlgpl$
+ * Second Life Viewer Source Code
+ * Copyright (C) 2018, Linden Research, Inc.
+ * 
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation;
+ * version 2.1 of the License only.
+ * 
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ * 
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
+ * 
+ * Linden Research, Inc., 945 Battery Street, San Francisco, CA  94111  USA
+ * $/LicenseInfo$
+ */
+
+#ifndef LL_LLATOMIC_H
+#define LL_LLATOMIC_H
+
+#include "stdtypes.h"
+
+#include <atomic>
+
+template <typename Type, typename AtomicType = std::atomic< Type > > class LLAtomicBase
+{
+public:
+    LLAtomicBase() {};
+    LLAtomicBase(Type x) { mData.store(x); }
+    ~LLAtomicBase() {};
+
+    operator const Type() { return mData; }
+
+    Type	CurrentValue() const { return mData; }
+
+    Type operator =(const Type& x) { mData.store(x); return mData; }
+    void operator -=(Type x) { mData -= x; }
+    void operator +=(Type x) { mData += x; }
+    Type operator ++(int) { return mData++; }
+    Type operator --(int) { return mData--; }
+
+    Type operator ++() { return ++mData; }
+    Type operator --() { return --mData; }
+
+private:
+    AtomicType mData;
+};
+
+// Typedefs for specialized versions. Using std::atomic_(u)int32_t to get the optimzed implementation.
+#ifdef LL_WINDOWS
+typedef LLAtomicBase<U32, std::atomic_uint32_t> LLAtomicU32;
+typedef LLAtomicBase<S32, std::atomic_int32_t> LLAtomicS32;
+#else
+typedef LLAtomicBase<U32, std::atomic_uint> LLAtomicU32;
+typedef LLAtomicBase<S32, std::atomic_int> LLAtomicS32;
+#endif
+
+typedef LLAtomicBase<bool, std::atomic_bool> LLAtomicBool;
+
+#endif // LL_LLATOMIC_H
diff --git a/indra/llcommon/llinstancetracker.cpp b/indra/llcommon/llinstancetracker.cpp
index 11fc53f8c8..3f990f4869 100644
--- a/indra/llcommon/llinstancetracker.cpp
+++ b/indra/llcommon/llinstancetracker.cpp
@@ -36,17 +36,16 @@
 
 void LLInstanceTrackerBase::StaticBase::incrementDepth()
 {
-	apr_atomic_inc32(&sIterationNestDepth);
+	++sIterationNestDepth;
 }
 
 void LLInstanceTrackerBase::StaticBase::decrementDepth()
 {
 	llassert(sIterationNestDepth);
-	apr_atomic_dec32(&sIterationNestDepth);
+	--sIterationNestDepth;
 }
 
 U32 LLInstanceTrackerBase::StaticBase::getDepth()
 {
-	apr_uint32_t data = apr_atomic_read32(&sIterationNestDepth);
-	return data;
+	return sIterationNestDepth;
 }
diff --git a/indra/llcommon/llinstancetracker.h b/indra/llcommon/llinstancetracker.h
index 910c8dbd99..363d0bcbd5 100644
--- a/indra/llcommon/llinstancetracker.h
+++ b/indra/llcommon/llinstancetracker.h
@@ -28,6 +28,7 @@
 #ifndef LL_LLINSTANCETRACKER_H
 #define LL_LLINSTANCETRACKER_H
 
+#include <atomic>
 #include <map>
 #include <typeinfo>
 
@@ -81,8 +82,12 @@ protected:
 		void decrementDepth();
 		U32 getDepth();
 	private:
-		U32 sIterationNestDepth;
-    };
+#ifdef LL_WINDOWS
+		std::atomic_uint32_t sIterationNestDepth;
+#else
+		std::atomic_uint sIterationNestDepth;
+#endif
+	};
 };
 
 LL_COMMON_API void assert_main_thread();
diff --git a/indra/llcommon/llmutex.cpp b/indra/llcommon/llmutex.cpp
index cf84e50953..75f43a4704 100644
--- a/indra/llcommon/llmutex.cpp
+++ b/indra/llcommon/llmutex.cpp
@@ -24,9 +24,6 @@
  */
 
 #include "linden_common.h"
-#include "llapr.h"
-
-#include "apr_portable.h"
 
 #include "llmutex.h"
 #include "llthread.h"
diff --git a/indra/llcommon/llqueuedthread.h b/indra/llcommon/llqueuedthread.h
index d3704b0fe2..5d3f873646 100644
--- a/indra/llcommon/llqueuedthread.h
+++ b/indra/llcommon/llqueuedthread.h
@@ -32,7 +32,7 @@
 #include <map>
 #include <set>
 
-#include "llapr.h"
+#include "llatomic.h"
 
 #include "llthread.h"
 #include "llsimplehash.h"
@@ -128,7 +128,7 @@ public:
 		};
 		
 	protected:
-		LLAtomic32<status_t> mStatus;
+		LLAtomicBase<status_t> mStatus;
 		U32 mPriority;
 		U32 mFlags;
 	};
@@ -198,7 +198,7 @@ public:
 protected:
 	BOOL mThreaded;  // if false, run on main thread and do updates during update()
 	BOOL mStarted;  // required when mThreaded is false to call startThread() from update()
-	LLAtomic32<BOOL> mIdleThread; // request queue is empty (or we are quitting) and the thread is idle
+	LLAtomicBool mIdleThread; // request queue is empty (or we are quitting) and the thread is idle
 	
 	typedef std::set<QueuedRequest*, queued_request_less> request_queue_t;
 	request_queue_t mRequestQueue;
diff --git a/indra/llcommon/llrefcount.h b/indra/llcommon/llrefcount.h
index cdc60fa54f..fb0411d27b 100644
--- a/indra/llcommon/llrefcount.h
+++ b/indra/llcommon/llrefcount.h
@@ -29,7 +29,7 @@
 #include <boost/noncopyable.hpp>
 #include <boost/intrusive_ptr.hpp>
 #include "llmutex.h"
-#include "llapr.h"
+#include "llatomic.h"
 
 //----------------------------------------------------------------------------
 // RefCount objects should generally only be accessed by way of LLPointer<>'s
@@ -107,8 +107,8 @@ public:
 	void unref()
 	{
 		llassert(mRef >= 1);
-		if ((--mRef) == 0)		// See note in llapr.h on atomic decrement operator return value.  
-		{	
+		if ((--mRef) == 0)
+		{
 			// 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
@@ -124,7 +124,7 @@ public:
 	}
 
 private: 
-	LLAtomic32< S32	> mRef; 
+	LLAtomicS32 mRef; 
 };
 
 /**
diff --git a/indra/llcommon/llworkerthread.h b/indra/llcommon/llworkerthread.h
index 09776816a8..b1a6f61360 100644
--- a/indra/llcommon/llworkerthread.h
+++ b/indra/llcommon/llworkerthread.h
@@ -33,7 +33,7 @@
 #include <string>
 
 #include "llqueuedthread.h"
-#include "llapr.h"
+#include "llatomic.h"
 
 #define USE_FRAME_CALLBACK_MANAGER 0
 
-- 
cgit v1.2.3