/src/pdns/pdns/dnsdistdist/sholder.hh
Line | Count | Source |
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> |
54 | | class GlobalStateHolder; |
55 | | |
56 | | template <typename T> |
57 | | class LocalStateHolder |
58 | | { |
59 | | public: |
60 | | explicit LocalStateHolder(GlobalStateHolder<T>* source) : |
61 | 0 | d_source(source) |
62 | 0 | {} |
63 | | |
64 | | const T* operator->() // fast const-only access, but see "read-only" above |
65 | 0 | { |
66 | 0 | if (d_source->getGeneration() != d_generation) { |
67 | 0 | d_source->getState(&d_state, &d_generation); |
68 | 0 | } |
69 | |
|
70 | 0 | return d_state.get(); |
71 | 0 | } |
72 | | const T& operator*() // fast const-only access, but see "read-only" above |
73 | | { |
74 | | return *operator->(); |
75 | | } |
76 | | |
77 | | // fast const-only access, see "read-only" above. |
78 | | // if allowedOutdated is true, the current local version is returned |
79 | | // without checking if there has been an update. |
80 | | const T& get(bool allowedOutdated = false) |
81 | 0 | { |
82 | 0 | if (!allowedOutdated || !d_state) { |
83 | 0 | return *operator->(); |
84 | 0 | } |
85 | 0 | return *d_state; |
86 | 0 | } |
87 | | |
88 | | void reset() |
89 | | { |
90 | | d_generation = 0; |
91 | | d_state.reset(); |
92 | | } |
93 | | |
94 | | private: |
95 | | std::shared_ptr<T> d_state; |
96 | | unsigned int d_generation{0}; |
97 | | const GlobalStateHolder<T>* d_source; |
98 | | }; |
99 | | |
100 | | template <typename T> |
101 | | class GlobalStateHolder |
102 | | { |
103 | | public: |
104 | | GlobalStateHolder() : |
105 | 0 | d_state(std::make_shared<T>()) |
106 | 0 | {} |
107 | | LocalStateHolder<T> getLocal() |
108 | 0 | { |
109 | 0 | return LocalStateHolder<T>(this); |
110 | 0 | } |
111 | | |
112 | | void setState(const T& state) //!< Safely & slowly change the global state |
113 | | { |
114 | | std::shared_ptr<T> newState = std::make_shared<T>(state); |
115 | | { |
116 | | *(d_state.lock()) = std::move(newState); |
117 | | d_generation++; |
118 | | } |
119 | | } |
120 | | |
121 | | void setState(T&& state) //!< Safely & slowly change the global state |
122 | | { |
123 | | std::shared_ptr<T> newState = std::make_shared<T>(std::move(state)); |
124 | | { |
125 | | *(d_state.lock()) = std::move(newState); |
126 | | d_generation++; |
127 | | } |
128 | | } |
129 | | |
130 | | T getCopy() const //!< Safely & slowly get a copy of the global state |
131 | | { |
132 | | return *(*(d_state.lock())); |
133 | | } |
134 | | |
135 | | //! Safely & slowly modify the global state |
136 | | template <typename F> |
137 | | void modify(F act) |
138 | 0 | { |
139 | 0 | auto state = d_state.lock(); |
140 | 0 | auto newState = *(*state); // and yes, these three steps are necessary, can't ever modify state in place, even when locked! |
141 | 0 | act(newState); |
142 | 0 | *state = std::make_shared<T>(std::move(newState)); |
143 | 0 | ++d_generation; |
144 | 0 | } |
145 | | |
146 | | using value_type = T; |
147 | | |
148 | | private: |
149 | | unsigned int getGeneration() const |
150 | 0 | { |
151 | 0 | return d_generation; |
152 | 0 | } |
153 | | void getState(std::shared_ptr<T>* state, unsigned int* generation) const |
154 | 0 | { |
155 | 0 | *state = *d_state.lock(); |
156 | 0 | *generation = d_generation; |
157 | 0 | } |
158 | | friend class LocalStateHolder<T>; |
159 | | |
160 | | mutable LockGuarded<std::shared_ptr<T>> d_state; |
161 | | std::atomic<unsigned int> d_generation{1}; |
162 | | }; |