/src/pdns/pdns/dnsdistdist/stat_t.hh
Line | Count | Source (jump to first uncovered line) |
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 | 18.9k | { |
40 | 18.9k | new(&counter) atomic_t(value); |
41 | 18.9k | } |
42 | 0 | stat_t_trait& operator=(const base_t& value) { |
43 | 0 | ref().store(value); |
44 | 0 | return *this; |
45 | 0 | } |
46 | 18.9k | ~stat_t_trait() { |
47 | 18.9k | ref().~atomic_t(); |
48 | 18.9k | } |
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 | 0 | base_t operator++() { |
57 | 0 | return ++(ref()); |
58 | 0 | } |
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 | 0 | base_t load() const { |
72 | 0 | return ref().load(); |
73 | 0 | } |
74 | 0 | void store(base_t value) { |
75 | 0 | ref().store(value); |
76 | 0 | } Unexecuted instantiation: pdns::stat_t_trait<double>::store(double) Unexecuted instantiation: pdns::stat_t_trait<unsigned long>::store(unsigned long) |
77 | 0 | operator base_t() const { |
78 | 0 | return ref().load(); |
79 | 0 | } |
80 | | |
81 | | private: |
82 | 18.9k | atomic_t& ref() { |
83 | 18.9k | return *reinterpret_cast<atomic_t *>(&counter); // NOLINT(cppcoreguidelines-pro-type-reinterpret-cast) |
84 | 18.9k | } pdns::stat_t_trait<unsigned long>::ref() Line | Count | Source | 82 | 18.9k | atomic_t& ref() { | 83 | 18.9k | return *reinterpret_cast<atomic_t *>(&counter); // NOLINT(cppcoreguidelines-pro-type-reinterpret-cast) | 84 | 18.9k | } |
Unexecuted instantiation: pdns::stat_t_trait<double>::ref() |
85 | 0 | const atomic_t& ref() const { |
86 | 0 | return *reinterpret_cast<const atomic_t *>(&counter); // NOLINT(cppcoreguidelines-pro-type-reinterpret-cast) |
87 | 0 | } Unexecuted instantiation: pdns::stat_t_trait<double>::ref() const Unexecuted instantiation: pdns::stat_t_trait<unsigned long>::ref() const |
88 | | typename std::aligned_storage_t<sizeof(atomic_t), CPU_LEVEL1_DCACHE_LINESIZE> counter; |
89 | | }; |
90 | | } |
91 | | #else |
92 | | namespace pdns { |
93 | | template <class T> |
94 | | using stat_t_trait = std::atomic<T>; |
95 | | } |
96 | | #endif |
97 | | |
98 | | namespace pdns { |
99 | | using stat_t = stat_t_trait<uint64_t>; |
100 | | using stat32_t = stat_t_trait<uint32_t>; |
101 | | using stat16_t = stat_t_trait<uint16_t>; |
102 | | using stat_double_t = stat_t_trait<double>; |
103 | | } |