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