]> git.ipfire.org Git - thirdparty/pdns.git/blame - pdns/sholder.hh
Make the constructors taking a pthread_rwlock_t * private.
[thirdparty/pdns.git] / pdns / sholder.hh
CommitLineData
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>
ca77577f 25#include <mutex>
ecbe9133 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.
30
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.
34
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.
38
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
42
43 NOTE: The actual destruction of the 'old' state happens when the last local state
44 relinquishes its access to the state.
45
46 "read-only"
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
49 as mutable. */
50
51template<typename T> class GlobalStateHolder;
52
53template<typename T>
54class LocalStateHolder
55{
56public:
57 explicit LocalStateHolder(GlobalStateHolder<T>* source) : d_source(source)
58 {}
59
e5a14b2b 60 const T* operator->() // fast const-only access, but see "read-only" above
ecbe9133 61 {
62 if(d_source->getGeneration() != d_generation) {
63 d_source->getState(&d_state, & d_generation);
64 }
65
66 return d_state.get();
67 }
e5a14b2b 68 const T& operator*() // fast const-only access, but see "read-only" above
ecbe9133 69 {
e5a14b2b 70 return *operator->();
ecbe9133 71 }
72
73 void reset()
74 {
75 d_generation=0;
76 d_state.reset();
77 }
78private:
79 std::shared_ptr<T> d_state;
ec4520d8 80 unsigned int d_generation{0};
ecbe9133 81 const GlobalStateHolder<T>* d_source;
82};
83
84template<typename T>
85class GlobalStateHolder
86{
87public:
ec4520d8 88 GlobalStateHolder() : d_state(std::make_shared<T>())
89 {}
ecbe9133 90 LocalStateHolder<T> getLocal()
91 {
92 return LocalStateHolder<T>(this);
93 }
e5a14b2b 94
33c2fd8a 95 void setState(const T& state) //!< Safely & slowly change the global state
ecbe9133 96 {
47678b82
RG
97 std::shared_ptr<T> newState = std::make_shared<T>(state);
98 {
99 std::lock_guard<std::mutex> l(d_lock);
33c2fd8a
RG
100 d_state = std::move(newState);
101 d_generation++;
102 }
103 }
104
105 void setState(T&& state) //!< Safely & slowly change the global state
106 {
107 std::shared_ptr<T> newState = std::make_shared<T>(std::move(state));
108 {
109 std::lock_guard<std::mutex> l(d_lock);
110 d_state = 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 {
117 std::lock_guard<std::mutex> l(d_lock);
e5a14b2b 118 return *d_state;
ecbe9133 119 }
120
e5a14b2b 121 //! Safely & slowly modify the global state
ecbe9133 122 template<typename F>
123 void modify(F act) {
33c2fd8a 124 std::lock_guard<std::mutex> l(d_lock);
ca77577f 125 auto state=*d_state; // and yes, these three steps are necessary, can't ever modify state in place, even when locked!
126 act(state);
33c2fd8a 127 d_state = std::make_shared<T>(std::move(state));
ecbe9133 128 ++d_generation;
129 }
130
131
132 typedef T value_type;
133private:
e5a14b2b 134 unsigned int getGeneration() const
135 {
136 return d_generation;
137 }
138 void getState(std::shared_ptr<T>* state, unsigned int* generation) const
139 {
140 std::lock_guard<std::mutex> l(d_lock);
141 *state=d_state;
142 *generation = d_generation;
143 }
144 friend class LocalStateHolder<T>;
ecbe9133 145 mutable std::mutex d_lock;
146 std::shared_ptr<T> d_state;
ec4520d8 147 std::atomic<unsigned int> d_generation{1};
ecbe9133 148};