MB-19281: [BP] Add template class RelaxedAtomic<> 69/63169/4
authorDave Rigby <daver@couchbase.com>
Thu, 21 Apr 2016 08:38:54 +0000 (09:38 +0100)
committerChiyoung Seo <chiyoung@couchbase.com>
Sat, 23 Apr 2016 01:08:49 +0000 (01:08 +0000)
Backport of the RelaxedAtomic template class from
platform/watson. Changed from C++11 std::atomic<> to our own
AtomicValue<> as 3.x doesn't have C++11 support on all platforms, and
moved to ep-engine as AtomicValue is an ep-engine specific class.

Doesn't include unit tests as they depend on GTest which isn't present
in 3.0.x.

Merge of the following platform commits:

* http://review.couchbase.org/54973 - Add template class RelaxedAtomic<>
* http://review.couchbase.org/55870 - RelaxedAtomic: Allow construction from template type
* http://review.couchbase.org/55889 - RelaxedAtomic: Remove 'explicit' definition for copy constructor

Change-Id: I16a5e2ebe85201aae85592329a2212c8a5c3a464
Reviewed-on: http://review.couchbase.org/63169
Reviewed-by: Trond Norbye <trond.norbye@gmail.com>
Well-Formed: buildbot <build@couchbase.com>
Reviewed-by: Will Gardner <will.gardner@couchbase.com>
Tested-by: buildbot <build@couchbase.com>
src/relaxed_atomic.h [new file with mode: 0644]

diff --git a/src/relaxed_atomic.h b/src/relaxed_atomic.h
new file mode 100644 (file)
index 0000000..edf2089
--- /dev/null
@@ -0,0 +1,124 @@
+/* -*- Mode: C++; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */
+/*
+ *     Copyright 2015 Couchbase, Inc.
+ *
+ *   Licensed under the Apache License, Version 2.0 (the "License");
+ *   you may not use this file except in compliance with the License.
+ *   You may obtain a copy of the License at
+ *
+ *       http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *   Unless required by applicable law or agreed to in writing, software
+ *   distributed under the License is distributed on an "AS IS" BASIS,
+ *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *   See the License for the specific language governing permissions and
+ *   limitations under the License.
+ */
+#pragma once
+
+#include "atomic.h"
+
+namespace Couchbase {
+
+    /**
+     * The RelaxedAtomic class wraps std::atomic<> and operates with
+     * relaxed memory ordering.
+     */
+    template<typename T>
+    class RelaxedAtomic {
+    public:
+        RelaxedAtomic() {
+            value.store(0, memory_order_relaxed);
+        }
+
+        RelaxedAtomic(const T& initial) {
+            value.store(initial, memory_order_relaxed);
+        }
+
+        explicit RelaxedAtomic(const RelaxedAtomic& other) {
+            value.store(other.value.load(memory_order_relaxed),
+                        memory_order_relaxed);
+        }
+
+        operator T() const {
+            return value.load(memory_order_relaxed);
+        }
+
+        T load() const {
+            return value.load(memory_order_relaxed);
+        }
+
+        RelaxedAtomic& operator=(const RelaxedAtomic& rhs) {
+            value.store(rhs.load(), memory_order_relaxed);
+            return *this;
+        }
+
+        RelaxedAtomic& operator+=(const T rhs) {
+            value.fetch_add(rhs, memory_order_relaxed);
+            return *this;
+        }
+
+        RelaxedAtomic& operator+=(const RelaxedAtomic& rhs) {
+            value.fetch_add(rhs.value.load(memory_order_relaxed),
+                            memory_order_relaxed);
+            return *this;
+        }
+
+        RelaxedAtomic& operator-=(const T rhs) {
+            value.fetch_sub(rhs, memory_order_relaxed);
+            return *this;
+        }
+
+        RelaxedAtomic& operator-=(const RelaxedAtomic& rhs) {
+            value.fetch_sub(rhs.value.load(memory_order_relaxed),
+                            memory_order_relaxed);
+            return *this;
+        }
+
+        T operator++() {
+            return value.fetch_add(1, memory_order_relaxed) + 1;
+        }
+
+        T operator++(int) {
+            return value.fetch_add(1, memory_order_relaxed);
+        }
+
+        T operator--() {
+            return value.fetch_sub(1, memory_order_relaxed) - 1;
+        }
+
+        T operator--(int) {
+            return value.fetch_sub(1, memory_order_relaxed);
+        }
+
+        RelaxedAtomic& operator=(T val) {
+            value.store(val, memory_order_relaxed);
+            return *this;
+        }
+
+        void reset() {
+            value.store(0, memory_order_relaxed);
+        }
+
+        void setIfGreater(const T& val) {
+            do {
+                T currval = value.load(memory_order_relaxed);
+                if (val > currval) {
+                    if (value.compare_exchange_weak(currval, val,
+                                                    memory_order_relaxed)) {
+                        break;
+                    }
+                } else {
+                    break;
+                }
+            } while (true);
+        }
+
+        void setIfGreater(const RelaxedAtomic& val) {
+            setIfGreater(val.load());
+        }
+
+    private:
+        AtomicValue<T> value;
+    };
+}