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.
26 /** This is sort of a light-weight RCU idea.
27 Suitable for when you frequently consult some "readonly" state, which infrequently
28 gets changed. One way of dealing with this is fully locking access to the state, but
29 this is rather wasteful.
31 Instead, in the code below, the frequent users of the state get a "readonly" copy of it,
32 which they can consult. On access, we atomically compare if the local copy is still current
33 with the global one. If it isn't we do the lock thing, and create a new local copy.
35 Meanwhile, to upgrade the global state, methods are offered that do appropriate locking
36 and upgrade the 'generation' counter, signaling to the local copies that they need to be
37 refreshed on the next access.
39 Two ways to change the global copy are available:
40 getCopy(), which delivers a deep copy of the current state, followed by setState()
41 modify(), which accepts a (lambda)function that modifies the state
43 NOTE: The actual destruction of the 'old' state happens when the last local state
44 relinquishes its access to the state.
47 Sometimes, a 'state' can contain parts that can safely be modified by multiple users, for
48 example, atomic counters. In such cases, it may be useful to explicitly declare such counters
51 template<typename T> class GlobalStateHolder;
54 class LocalStateHolder
57 explicit LocalStateHolder(GlobalStateHolder<T>* source) : d_source(source)
60 const T* operator->() // fast const-only access, but see "read-only" above
62 if(d_source->getGeneration() != d_generation) {
63 d_source->getState(&d_state, & d_generation);
68 const T& operator*() // fast const-only access, but see "read-only" above
79 std::shared_ptr<T> d_state;
80 unsigned int d_generation{0};
81 const GlobalStateHolder<T>* d_source;
85 class GlobalStateHolder
88 GlobalStateHolder() : d_state(std::make_shared<T>())
90 LocalStateHolder<T> getLocal()
92 return LocalStateHolder<T>(this);
95 void setState(T state) //!< Safely & slowly change the global state
97 std::shared_ptr<T> newState = std::make_shared<T>(state);
99 std::lock_guard<std::mutex> l(d_lock);
105 T getCopy() const //!< Safely & slowly get a copy of the global state
107 std::lock_guard<std::mutex> l(d_lock);
111 //! Safely & slowly modify the global state
114 std::lock_guard<std::mutex> l(d_lock);
115 auto state=*d_state; // and yes, these three steps are necessary, can't ever modify state in place, even when locked!
117 d_state = std::make_shared<T>(state);
122 typedef T value_type;
124 unsigned int getGeneration() const
128 void getState(std::shared_ptr<T>* state, unsigned int* generation) const
130 std::lock_guard<std::mutex> l(d_lock);
132 *generation = d_generation;
134 friend class LocalStateHolder<T>;
135 mutable std::mutex d_lock;
136 std::shared_ptr<T> d_state;
137 std::atomic<unsigned int> d_generation{1};