2 * This file is part of PowerDNS or dnsdist.
3 * Copyright -- PowerDNS.COM B.V. and its contributors
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.
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.
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.
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.
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.
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.
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.
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
45 NOTE: The actual destruction of the 'old' state happens when the last local state
46 relinquishes its access to the state.
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
53 template<typename T> class GlobalStateHolder;
56 class LocalStateHolder
59 explicit LocalStateHolder(GlobalStateHolder<T>* source) : d_source(source)
62 const T* operator->() // fast const-only access, but see "read-only" above
64 if(d_source->getGeneration() != d_generation) {
65 d_source->getState(&d_state, & d_generation);
70 const T& operator*() // fast const-only access, but see "read-only" above
81 std::shared_ptr<T> d_state;
82 unsigned int d_generation{0};
83 const GlobalStateHolder<T>* d_source;
87 class GlobalStateHolder
90 GlobalStateHolder() : d_state(std::make_shared<T>())
92 LocalStateHolder<T> getLocal()
94 return LocalStateHolder<T>(this);
97 void setState(const T& state) //!< Safely & slowly change the global state
99 std::shared_ptr<T> newState = std::make_shared<T>(state);
101 *(d_state.lock()) = std::move(newState);
106 void setState(T&& state) //!< Safely & slowly change the global state
108 std::shared_ptr<T> newState = std::make_shared<T>(std::move(state));
110 *(d_state.lock()) = std::move(newState);
115 T getCopy() const //!< Safely & slowly get a copy of the global state
117 return *(*(d_state.lock()));
120 //! Safely & slowly modify the global state
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!
126 *state = std::make_shared<T>(std::move(newState));
130 typedef T value_type;
132 unsigned int getGeneration() const
136 void getState(std::shared_ptr<T>* state, unsigned int* generation) const
138 *state = *d_state.lock();
139 *generation = d_generation;
141 friend class LocalStateHolder<T>;
143 mutable LockGuarded<std::shared_ptr<T>> d_state;
144 std::atomic<unsigned int> d_generation{1};