]> git.ipfire.org Git - thirdparty/pdns.git/blob - pdns/stat_t.hh
96c7433514de3bca2e5748c9e954379ece5c0c76
[thirdparty/pdns.git] / pdns / stat_t.hh
1 /*
2 * This file is part of PowerDNS or dnsdist.
3 * Copyright -- PowerDNS.COM B.V. and its contributors
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of version 2 of the GNU General Public License as
7 * published by the Free Software Foundation.
8 *
9 * In addition, for the avoidance of any doubt, permission is granted to
10 * link this program with OpenSSL and to (re)distribute the binaries
11 * produced as the result of such linking.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 */
22 #pragma once
23
24 #include <atomic>
25
26 #ifndef DISABLE_FALSE_SHARING_PADDING
27 #define CPU_LEVEL1_DCACHE_LINESIZE 64 // Until we know better via configure/getconf
28
29 namespace pdns {
30 template <typename T>
31 class stat_t_trait {
32 public:
33 using base_t = T;
34 using atomic_t = std::atomic<base_t>;
35
36 stat_t_trait() : stat_t_trait(base_t(0)) {
37 }
38 stat_t_trait(const base_t value)
39 {
40 new(&counter) atomic_t(value);
41 }
42 stat_t_trait& operator=(const base_t& value) {
43 ref().store(value);
44 return *this;
45 }
46 ~stat_t_trait() {
47 ref().~atomic_t();
48 }
49 stat_t_trait(stat_t_trait&&) = delete;
50 stat_t_trait& operator=(const stat_t_trait&) = delete;
51 stat_t_trait& operator=(stat_t_trait&&) = delete;
52 stat_t_trait(const stat_t_trait&) = delete;
53 base_t operator++(int) {
54 return ref()++;
55 }
56 base_t operator++() {
57 return ++(ref());
58 }
59 base_t operator--(int) {
60 return ref()--;
61 }
62 base_t operator--() {
63 return --(ref());
64 }
65 base_t operator+=(base_t arg) {
66 return ref() += arg;
67 }
68 base_t operator-=(base_t arg) {
69 return ref() -= arg;
70 }
71 base_t load() const {
72 return ref().load();
73 }
74 void store(base_t value) {
75 ref().store(value);
76 }
77 operator base_t() const {
78 return ref().load();
79 }
80
81 private:
82 atomic_t& ref() {
83 return *reinterpret_cast<atomic_t *>(&counter); // NOLINT(cppcoreguidelines-pro-type-reinterpret-cast)
84 }
85 const atomic_t& ref() const {
86 return *reinterpret_cast<const atomic_t *>(&counter); // NOLINT(cppcoreguidelines-pro-type-reinterpret-cast)
87 }
88 typename std::aligned_storage_t<sizeof(atomic_t), CPU_LEVEL1_DCACHE_LINESIZE> counter;
89 };
90
91 using stat_t = stat_t_trait<uint64_t>;
92 using stat32_t = stat_t_trait<uint32_t>;
93 using stat16_t = stat_t_trait<uint16_t>;
94 }
95 #else
96 namespace pdns {
97 using stat_t = std::atomic<uint64_t>;
98 using stat32_t = std::atomic<uint32_t>;
99 using stat16_t = std::atomic<uint16_t>;
100 template <class T>
101 using stat_t_trait = std::atomic<T>;
102 }
103 #endif