summaryrefslogtreecommitdiff
path: root/include/my_counter.h
diff options
context:
space:
mode:
authorSergey Vojtovich <svoj@mariadb.org>2018-12-26 16:49:11 +0400
committerSergey Vojtovich <svoj@mariadb.org>2018-12-27 22:46:38 +0400
commitca83115b3e77ffa696527d82640d5f16810294a2 (patch)
tree72792e7e9c398adcb04f191f66db1cbf1ef6fce0 /include/my_counter.h
parent88b7b8199a6061ccdeb2e2a1ac789eb98e02ac92 (diff)
downloadmariadb-git-ca83115b3e77ffa696527d82640d5f16810294a2.tar.gz
MDEV-17441 - InnoDB transition to C++11 atomics
Added Atomic_counter class to replace big set of atomic operations uses in InnoDB, as well as in the server.
Diffstat (limited to 'include/my_counter.h')
-rw-r--r--include/my_counter.h44
1 files changed, 44 insertions, 0 deletions
diff --git a/include/my_counter.h b/include/my_counter.h
new file mode 100644
index 00000000000..00fbf8cef91
--- /dev/null
+++ b/include/my_counter.h
@@ -0,0 +1,44 @@
+#ifndef MY_COUNTER_H_INCLUDED
+#define MY_COUNTER_H_INCLUDED
+/*
+ Copyright (C) 2018 MariaDB Foundation
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; version 2 of the License.
+
+ This program 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 General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+*/
+
+#include <atomic>
+
+
+template <typename Type> class Atomic_counter
+{
+ std::atomic<Type> m_counter;
+
+ Type add(Type i) { return m_counter.fetch_add(i, std::memory_order_relaxed); }
+ Type sub(Type i) { return m_counter.fetch_sub(i, std::memory_order_relaxed); }
+
+public:
+ Type operator++(int) { return add(1); }
+ Type operator--(int) { return sub(1); }
+
+ Type operator++() { return add(1) + 1; }
+ Type operator--() { return sub(1) - 1; }
+
+ Type operator+=(const Type i) { return add(i) + i; }
+ Type operator-=(const Type i) { return sub(i) - i; }
+
+ operator Type() const { return m_counter.load(std::memory_order_relaxed); }
+ Type operator=(const Type val)
+ { m_counter.store(val, std::memory_order_relaxed); return val; }
+};
+#endif /* MY_COUNTER_H_INCLUDED */