]>
Commit | Line | Data |
---|---|---|
12471842 PL |
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 | */ | |
e8984b87 | 22 | #pragma once |
ecbe9133 | 23 | #include <memory> |
24 | #include <atomic> | |
4a7f983e RG |
25 | |
26 | #include "lock.hh" | |
27 | ||
ecbe9133 | 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 | ||
e5a14b2b | 62 | const T* operator->() // fast const-only access, but see "read-only" above |
ecbe9133 | 63 | { |
64 | if(d_source->getGeneration() != d_generation) { | |
65 | d_source->getState(&d_state, & d_generation); | |
66 | } | |
67 | ||
68 | return d_state.get(); | |
69 | } | |
e5a14b2b | 70 | const T& operator*() // fast const-only access, but see "read-only" above |
ecbe9133 | 71 | { |
e5a14b2b | 72 | return *operator->(); |
ecbe9133 | 73 | } |
74 | ||
75 | void reset() | |
76 | { | |
77 | d_generation=0; | |
78 | d_state.reset(); | |
79 | } | |
80 | private: | |
81 | std::shared_ptr<T> d_state; | |
ec4520d8 | 82 | unsigned int d_generation{0}; |
ecbe9133 | 83 | const GlobalStateHolder<T>* d_source; |
84 | }; | |
85 | ||
86 | template<typename T> | |
87 | class GlobalStateHolder | |
88 | { | |
89 | public: | |
ec4520d8 | 90 | GlobalStateHolder() : d_state(std::make_shared<T>()) |
91 | {} | |
ecbe9133 | 92 | LocalStateHolder<T> getLocal() |
93 | { | |
94 | return LocalStateHolder<T>(this); | |
95 | } | |
e5a14b2b | 96 | |
33c2fd8a | 97 | void setState(const T& state) //!< Safely & slowly change the global state |
ecbe9133 | 98 | { |
47678b82 RG |
99 | std::shared_ptr<T> newState = std::make_shared<T>(state); |
100 | { | |
4a7f983e | 101 | *(d_state.lock()) = std::move(newState); |
33c2fd8a RG |
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 | { | |
4a7f983e | 110 | *(d_state.lock()) = std::move(newState); |
47678b82 RG |
111 | d_generation++; |
112 | } | |
ecbe9133 | 113 | } |
e5a14b2b | 114 | |
115 | T getCopy() const //!< Safely & slowly get a copy of the global state | |
ecbe9133 | 116 | { |
4a7f983e | 117 | return *(*(d_state.lock())); |
ecbe9133 | 118 | } |
119 | ||
e5a14b2b | 120 | //! Safely & slowly modify the global state |
ecbe9133 | 121 | template<typename F> |
122 | void modify(F act) { | |
4a7f983e RG |
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)); | |
ecbe9133 | 127 | ++d_generation; |
128 | } | |
129 | ||
ecbe9133 | 130 | typedef T value_type; |
131 | private: | |
e5a14b2b | 132 | unsigned int getGeneration() const |
133 | { | |
134 | return d_generation; | |
135 | } | |
136 | void getState(std::shared_ptr<T>* state, unsigned int* generation) const | |
137 | { | |
4a7f983e | 138 | *state = *d_state.lock(); |
e5a14b2b | 139 | *generation = d_generation; |
140 | } | |
141 | friend class LocalStateHolder<T>; | |
4a7f983e RG |
142 | |
143 | mutable LockGuarded<std::shared_ptr<T>> d_state; | |
ec4520d8 | 144 | std::atomic<unsigned int> d_generation{1}; |
ecbe9133 | 145 | }; |