/src/pdns/pdns/dnsdistdist/sholder.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 | | #include <memory> |
24 | | #include <atomic> |
25 | | |
26 | | #include "lock.hh" |
27 | | |
28 | | /** This is sort of a light-weight RCU idea. |
29 | | Suitable for when you frequently consult some "readonly" state, which infrequently |
30 | | gets changed. One way of dealing with this is fully locking access to the state, but |
31 | | this is rather wasteful. |
32 | | |
33 | | Instead, in the code below, the frequent users of the state get a "readonly" copy of it, |
34 | | which they can consult. On access, we atomically compare if the local copy is still current |
35 | | with the global one. If it isn't we do the lock thing, and create a new local copy. |
36 | | |
37 | | Meanwhile, to upgrade the global state, methods are offered that do appropriate locking |
38 | | and upgrade the 'generation' counter, signaling to the local copies that they need to be |
39 | | refreshed on the next access. |
40 | | |
41 | | Two ways to change the global copy are available: |
42 | | getCopy(), which delivers a deep copy of the current state, followed by setState() |
43 | | modify(), which accepts a (lambda)function that modifies the state |
44 | | |
45 | | NOTE: The actual destruction of the 'old' state happens when the last local state |
46 | | relinquishes its access to the state. |
47 | | |
48 | | "read-only" |
49 | | Sometimes, a 'state' can contain parts that can safely be modified by multiple users, for |
50 | | example, atomic counters. In such cases, it may be useful to explicitly declare such counters |
51 | | as mutable. */ |
52 | | |
53 | | template<typename T> class GlobalStateHolder; |
54 | | |
55 | | template<typename T> |
56 | | class LocalStateHolder |
57 | | { |
58 | | public: |
59 | | explicit LocalStateHolder(GlobalStateHolder<T>* source) : d_source(source) |
60 | | {} |
61 | | |
62 | | const T* operator->() // fast const-only access, but see "read-only" above |
63 | | { |
64 | | if(d_source->getGeneration() != d_generation) { |
65 | | d_source->getState(&d_state, & d_generation); |
66 | | } |
67 | | |
68 | | return d_state.get(); |
69 | | } |
70 | | const T& operator*() // fast const-only access, but see "read-only" above |
71 | | { |
72 | | return *operator->(); |
73 | | } |
74 | | |
75 | | void reset() |
76 | | { |
77 | | d_generation=0; |
78 | | d_state.reset(); |
79 | | } |
80 | | private: |
81 | | std::shared_ptr<T> d_state; |
82 | | unsigned int d_generation{0}; |
83 | | const GlobalStateHolder<T>* d_source; |
84 | | }; |
85 | | |
86 | | template<typename T> |
87 | | class GlobalStateHolder |
88 | | { |
89 | | public: |
90 | | GlobalStateHolder() : d_state(std::make_shared<T>()) |
91 | | {} |
92 | | LocalStateHolder<T> getLocal() |
93 | 0 | { |
94 | 0 | return LocalStateHolder<T>(this); |
95 | 0 | } Unexecuted instantiation: GlobalStateHolder<NetmaskGroup>::getLocal() Unexecuted instantiation: GlobalStateHolder<ServerPolicy>::getLocal() Unexecuted instantiation: GlobalStateHolder<std::__1::vector<DNSDistRuleAction, std::__1::allocator<DNSDistRuleAction> > >::getLocal() Unexecuted instantiation: GlobalStateHolder<std::__1::vector<DNSDistResponseRuleAction, std::__1::allocator<DNSDistResponseRuleAction> > >::getLocal() Unexecuted instantiation: GlobalStateHolder<std::__1::vector<std::__1::shared_ptr<DownstreamState>, std::__1::allocator<std::__1::shared_ptr<DownstreamState> > > >::getLocal() Unexecuted instantiation: GlobalStateHolder<NetmaskTree<DynBlock, AddressAndPortRange> >::getLocal() Unexecuted instantiation: GlobalStateHolder<SuffixMatchTree<DynBlock> >::getLocal() Unexecuted instantiation: GlobalStateHolder<std::__1::map<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, std::__1::shared_ptr<ServerPool>, std::__1::less<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > >, std::__1::allocator<std::__1::pair<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const, std::__1::shared_ptr<ServerPool> > > > >::getLocal() |
96 | | |
97 | | void setState(const T& state) //!< Safely & slowly change the global state |
98 | | { |
99 | | std::shared_ptr<T> newState = std::make_shared<T>(state); |
100 | | { |
101 | | *(d_state.lock()) = std::move(newState); |
102 | | d_generation++; |
103 | | } |
104 | | } |
105 | | |
106 | | void setState(T&& state) //!< Safely & slowly change the global state |
107 | | { |
108 | | std::shared_ptr<T> newState = std::make_shared<T>(std::move(state)); |
109 | | { |
110 | | *(d_state.lock()) = std::move(newState); |
111 | | d_generation++; |
112 | | } |
113 | | } |
114 | | |
115 | | T getCopy() const //!< Safely & slowly get a copy of the global state |
116 | | { |
117 | | return *(*(d_state.lock())); |
118 | | } |
119 | | |
120 | | //! Safely & slowly modify the global state |
121 | | template<typename F> |
122 | | void modify(F act) { |
123 | | auto state = d_state.lock(); |
124 | | auto newState = *(*state); // and yes, these three steps are necessary, can't ever modify state in place, even when locked! |
125 | | act(newState); |
126 | | *state = std::make_shared<T>(std::move(newState)); |
127 | | ++d_generation; |
128 | | } |
129 | | |
130 | | typedef T value_type; |
131 | | private: |
132 | | unsigned int getGeneration() const |
133 | | { |
134 | | return d_generation; |
135 | | } |
136 | | void getState(std::shared_ptr<T>* state, unsigned int* generation) const |
137 | | { |
138 | | *state = *d_state.lock(); |
139 | | *generation = d_generation; |
140 | | } |
141 | | friend class LocalStateHolder<T>; |
142 | | |
143 | | mutable LockGuarded<std::shared_ptr<T>> d_state; |
144 | | std::atomic<unsigned int> d_generation{1}; |
145 | | }; |