From: Otto Moerbeek Date: Wed, 26 Jun 2024 09:37:23 +0000 (+0200) Subject: Reformat X-Git-Tag: rec-5.2.0-alpha1~213^2~1 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=6a27dba9a38d8efb4109ebe2cb826d96997e2cb9;p=thirdparty%2Fpdns.git Reformat --- diff --git a/.not-formatted b/.not-formatted index b77e6d5c7c..e7f391d476 100644 --- a/.not-formatted +++ b/.not-formatted @@ -120,7 +120,6 @@ ./pdns/kvresp.cc ./pdns/libssl.cc ./pdns/libssl.hh -./pdns/lock.hh ./pdns/lua-auth4.cc ./pdns/lua-auth4.hh ./pdns/lua-base4.cc @@ -170,7 +169,6 @@ ./pdns/secpoll.cc ./pdns/secpoll.hh ./pdns/serialtweaker.cc -./pdns/sholder.hh ./pdns/signingpipe.cc ./pdns/signingpipe.hh ./pdns/sillyrecords.cc diff --git a/pdns/lock.hh b/pdns/lock.hh index 611d8ada02..9a289660e6 100644 --- a/pdns/lock.hh +++ b/pdns/lock.hh @@ -99,11 +99,13 @@ private: class ReadLock { public: - ReadLock(ReadWriteLock& lock): ReadLock(lock.getLock()) + ReadLock(ReadWriteLock& lock) : + ReadLock(lock.getLock()) { } - ReadLock(ReadWriteLock* lock): ReadLock(lock->getLock()) + ReadLock(ReadWriteLock* lock) : + ReadLock(lock->getLock()) { } @@ -115,7 +117,8 @@ public: } private: - ReadLock(std::shared_mutex& lock) : d_lock(lock) + ReadLock(std::shared_mutex& lock) : + d_lock(lock) { } @@ -125,11 +128,13 @@ private: class WriteLock { public: - WriteLock(ReadWriteLock& lock): WriteLock(lock.getLock()) + WriteLock(ReadWriteLock& lock) : + WriteLock(lock.getLock()) { } - WriteLock(ReadWriteLock* lock): WriteLock(lock->getLock()) + WriteLock(ReadWriteLock* lock) : + WriteLock(lock->getLock()) { } @@ -141,7 +146,8 @@ public: } private: - WriteLock(std::shared_mutex& lock) : d_lock(lock) + WriteLock(std::shared_mutex& lock) : + d_lock(lock) { } @@ -151,11 +157,13 @@ private: class TryReadLock { public: - TryReadLock(ReadWriteLock& lock): TryReadLock(lock.getLock()) + TryReadLock(ReadWriteLock& lock) : + TryReadLock(lock.getLock()) { } - TryReadLock(ReadWriteLock* lock): TryReadLock(lock->getLock()) + TryReadLock(ReadWriteLock* lock) : + TryReadLock(lock->getLock()) { } @@ -168,7 +176,8 @@ public: } private: - TryReadLock(std::shared_mutex& lock) : d_lock(lock, std::try_to_lock) + TryReadLock(std::shared_mutex& lock) : + d_lock(lock, std::try_to_lock) { } @@ -178,11 +187,13 @@ private: class TryWriteLock { public: - TryWriteLock(ReadWriteLock& lock): TryWriteLock(lock.getLock()) + TryWriteLock(ReadWriteLock& lock) : + TryWriteLock(lock.getLock()) { } - TryWriteLock(ReadWriteLock* lock): TryWriteLock(lock->getLock()) + TryWriteLock(ReadWriteLock* lock) : + TryWriteLock(lock->getLock()) { } @@ -195,7 +206,8 @@ public: } private: - TryWriteLock(std::shared_mutex& lock) : d_lock(lock, std::try_to_lock) + TryWriteLock(std::shared_mutex& lock) : + d_lock(lock, std::try_to_lock) { } @@ -206,15 +218,18 @@ template class LockGuardedHolder { public: - explicit LockGuardedHolder(T& value, std::mutex& mutex): d_lock(mutex), d_value(value) + explicit LockGuardedHolder(T& value, std::mutex& mutex) : + d_lock(mutex), d_value(value) { } - T& operator*() const noexcept { + T& operator*() const noexcept + { return d_value; } - T* operator->() const noexcept { + T* operator->() const noexcept + { return &d_value; } @@ -227,29 +242,34 @@ template class LockGuardedTryHolder { public: - explicit LockGuardedTryHolder(T& value, std::mutex& mutex): d_lock(mutex, std::try_to_lock), d_value(value) + explicit LockGuardedTryHolder(T& value, std::mutex& mutex) : + d_lock(mutex, std::try_to_lock), d_value(value) { } - T& operator*() const { + T& operator*() const + { if (!owns_lock()) { throw std::runtime_error("Trying to access data protected by a mutex while the lock has not been acquired"); } return d_value; } - T* operator->() const { + T* operator->() const + { if (!owns_lock()) { throw std::runtime_error("Trying to access data protected by a mutex while the lock has not been acquired"); } return &d_value; } - operator bool() const noexcept { + operator bool() const noexcept + { return d_lock.owns_lock(); } - bool owns_lock() const noexcept { + bool owns_lock() const noexcept + { return d_lock.owns_lock(); } @@ -267,11 +287,13 @@ template class LockGuarded { public: - explicit LockGuarded(const T& value): d_value(value) + explicit LockGuarded(const T& value) : + d_value(value) { } - explicit LockGuarded(T&& value): d_value(std::move(value)) + explicit LockGuarded(T&& value) : + d_value(std::move(value)) { } @@ -301,15 +323,18 @@ template class SharedLockGuardedHolder { public: - explicit SharedLockGuardedHolder(T& value, std::shared_mutex& mutex): d_lock(mutex), d_value(value) + explicit SharedLockGuardedHolder(T& value, std::shared_mutex& mutex) : + d_lock(mutex), d_value(value) { } - T& operator*() const noexcept { + T& operator*() const noexcept + { return d_value; } - T* operator->() const noexcept { + T* operator->() const noexcept + { return &d_value; } @@ -322,29 +347,34 @@ template class SharedLockGuardedTryHolder { public: - explicit SharedLockGuardedTryHolder(T& value, std::shared_mutex& mutex): d_lock(mutex, std::try_to_lock), d_value(value) + explicit SharedLockGuardedTryHolder(T& value, std::shared_mutex& mutex) : + d_lock(mutex, std::try_to_lock), d_value(value) { } - T& operator*() const { + T& operator*() const + { if (!owns_lock()) { throw std::runtime_error("Trying to access data protected by a mutex while the lock has not been acquired"); } return d_value; } - T* operator->() const { + T* operator->() const + { if (!owns_lock()) { throw std::runtime_error("Trying to access data protected by a mutex while the lock has not been acquired"); } return &d_value; } - operator bool() const noexcept { + operator bool() const noexcept + { return d_lock.owns_lock(); } - bool owns_lock() const noexcept { + bool owns_lock() const noexcept + { return d_lock.owns_lock(); } @@ -357,15 +387,18 @@ template class SharedLockGuardedNonExclusiveHolder { public: - explicit SharedLockGuardedNonExclusiveHolder(const T& value, std::shared_mutex& mutex): d_lock(mutex), d_value(value) + explicit SharedLockGuardedNonExclusiveHolder(const T& value, std::shared_mutex& mutex) : + d_lock(mutex), d_value(value) { } - const T& operator*() const noexcept { + const T& operator*() const noexcept + { return d_value; } - const T* operator->() const noexcept { + const T* operator->() const noexcept + { return &d_value; } @@ -378,29 +411,34 @@ template class SharedLockGuardedNonExclusiveTryHolder { public: - explicit SharedLockGuardedNonExclusiveTryHolder(const T& value, std::shared_mutex& mutex): d_lock(mutex, std::try_to_lock), d_value(value) + explicit SharedLockGuardedNonExclusiveTryHolder(const T& value, std::shared_mutex& mutex) : + d_lock(mutex, std::try_to_lock), d_value(value) { } - const T& operator*() const { + const T& operator*() const + { if (!owns_lock()) { throw std::runtime_error("Trying to access data protected by a mutex while the lock has not been acquired"); } return d_value; } - const T* operator->() const { + const T* operator->() const + { if (!owns_lock()) { throw std::runtime_error("Trying to access data protected by a mutex while the lock has not been acquired"); } return &d_value; } - operator bool() const noexcept { + operator bool() const noexcept + { return d_lock.owns_lock(); } - bool owns_lock() const noexcept { + bool owns_lock() const noexcept + { return d_lock.owns_lock(); } @@ -413,11 +451,13 @@ template class SharedLockGuarded { public: - explicit SharedLockGuarded(const T& value): d_value(value) + explicit SharedLockGuarded(const T& value) : + d_value(value) { } - explicit SharedLockGuarded(T&& value): d_value(std::move(value)) + explicit SharedLockGuarded(T&& value) : + d_value(std::move(value)) { } diff --git a/pdns/sholder.hh b/pdns/sholder.hh index 2a4f758c08..74727c5673 100644 --- a/pdns/sholder.hh +++ b/pdns/sholder.hh @@ -25,69 +25,73 @@ #include "lock.hh" -/** This is sort of a light-weight RCU idea. +/** This is sort of a light-weight RCU idea. Suitable for when you frequently consult some "readonly" state, which infrequently - gets changed. One way of dealing with this is fully locking access to the state, but + gets changed. One way of dealing with this is fully locking access to the state, but this is rather wasteful. - Instead, in the code below, the frequent users of the state get a "readonly" copy of it, - which they can consult. On access, we atomically compare if the local copy is still current + Instead, in the code below, the frequent users of the state get a "readonly" copy of it, + which they can consult. On access, we atomically compare if the local copy is still current with the global one. If it isn't we do the lock thing, and create a new local copy. - Meanwhile, to upgrade the global state, methods are offered that do appropriate locking + Meanwhile, to upgrade the global state, methods are offered that do appropriate locking and upgrade the 'generation' counter, signaling to the local copies that they need to be refreshed on the next access. Two ways to change the global copy are available: getCopy(), which delivers a deep copy of the current state, followed by setState() - modify(), which accepts a (lambda)function that modifies the state + modify(), which accepts a (lambda)function that modifies the state - NOTE: The actual destruction of the 'old' state happens when the last local state + NOTE: The actual destruction of the 'old' state happens when the last local state relinquishes its access to the state. "read-only" - Sometimes, a 'state' can contain parts that can safely be modified by multiple users, for + Sometimes, a 'state' can contain parts that can safely be modified by multiple users, for example, atomic counters. In such cases, it may be useful to explicitly declare such counters as mutable. */ -template class GlobalStateHolder; +template +class GlobalStateHolder; -template +template class LocalStateHolder { public: - explicit LocalStateHolder(GlobalStateHolder* source) : d_source(source) + explicit LocalStateHolder(GlobalStateHolder* source) : + d_source(source) {} - const T* operator->() // fast const-only access, but see "read-only" above + const T* operator->() // fast const-only access, but see "read-only" above { - if(d_source->getGeneration() != d_generation) { - d_source->getState(&d_state, & d_generation); + if (d_source->getGeneration() != d_generation) { + d_source->getState(&d_state, &d_generation); } return d_state.get(); } - const T& operator*() // fast const-only access, but see "read-only" above + const T& operator*() // fast const-only access, but see "read-only" above { return *operator->(); } void reset() { - d_generation=0; + d_generation = 0; d_state.reset(); } + private: std::shared_ptr d_state; unsigned int d_generation{0}; const GlobalStateHolder* d_source; }; -template +template class GlobalStateHolder { public: - GlobalStateHolder() : d_state(std::make_shared()) + GlobalStateHolder() : + d_state(std::make_shared()) {} LocalStateHolder getLocal() { @@ -112,14 +116,15 @@ public: } } - T getCopy() const //!< Safely & slowly get a copy of the global state + T getCopy() const //!< Safely & slowly get a copy of the global state { return *(*(d_state.lock())); } - + //! Safely & slowly modify the global state - template - void modify(F act) { + template + void modify(F act) + { auto state = d_state.lock(); auto newState = *(*state); // and yes, these three steps are necessary, can't ever modify state in place, even when locked! act(newState); @@ -128,6 +133,7 @@ public: } typedef T value_type; + private: unsigned int getGeneration() const {