]> git.ipfire.org Git - thirdparty/pdns.git/commitdiff
Replace pthread_rwlock with std::shared_mutex
authorRemi Gacogne <remi.gacogne@powerdns.com>
Wed, 24 Mar 2021 17:49:02 +0000 (18:49 +0100)
committerRemi Gacogne <remi.gacogne@powerdns.com>
Wed, 24 Mar 2021 17:49:02 +0000 (18:49 +0100)
pdns/cachecleaner.hh
pdns/libssl.cc
pdns/libssl.hh
pdns/lock.hh
pdns/test-lock_hh.cc

index a06a35e0865d5e59161262d8eecf93e1915929ca..4962ff63576be733e34eb1d119dd0f4741b9fd5d 100644 (file)
@@ -22,6 +22,9 @@
 #pragma once
 
 #include <mutex>
+#include <boost/multi_index_container.hpp>
+
+#include "dnsname.hh"
 #include "lock.hh"
 
 // this function can clean any cache that has a getTTD() method on its entries, a preRemoval() method and a 'sequence' index as its second index
@@ -96,7 +99,7 @@ template <typename S, typename T> void moveCacheItemToBack(T& collection, typena
   moveCacheItemToFrontOrBack<S>(collection, iter, false);
 }
 
-template <typename S, typename T> uint64_t pruneLockedCollectionsVector(vector<T>& maps)
+template <typename S, typename T> uint64_t pruneLockedCollectionsVector(std::vector<T>& maps)
 {
   uint64_t totErased = 0;
   time_t now = time(nullptr);
@@ -122,7 +125,7 @@ template <typename S, typename T> uint64_t pruneLockedCollectionsVector(vector<T
   return totErased;
 }
 
-template <typename S, typename C, typename T> uint64_t pruneMutexCollectionsVector(C& container, vector<T>& maps, uint64_t maxCached, uint64_t cacheSize)
+template <typename S, typename C, typename T> uint64_t pruneMutexCollectionsVector(C& container, std::vector<T>& maps, uint64_t maxCached, uint64_t cacheSize)
 {
   time_t now = time(nullptr);
   uint64_t totErased = 0;
@@ -197,7 +200,7 @@ template <typename S, typename C, typename T> uint64_t pruneMutexCollectionsVect
   return totErased;
 }
 
-template <typename T> uint64_t purgeLockedCollectionsVector(vector<T>& maps)
+template <typename T> uint64_t purgeLockedCollectionsVector(std::vector<T>& maps)
 {
   uint64_t delcount=0;
 
@@ -210,10 +213,10 @@ template <typename T> uint64_t purgeLockedCollectionsVector(vector<T>& maps)
   return delcount;
 }
 
-template <typename N, typename T> uint64_t purgeLockedCollectionsVector(vector<T>& maps, const std::string& match)
+template <typename N, typename T> uint64_t purgeLockedCollectionsVector(std::vector<T>& maps, const std::string& match)
 {
   uint64_t delcount=0;
-  string prefix(match);
+  std::string prefix(match);
   prefix.resize(prefix.size()-1);
   DNSName dprefix(prefix);
   for(auto& mc : maps) {
index 9b43c91050e2847566ad6f1382f291d0e52b18c4..372e40db0aa4e7fd4d9d36b5ff30d4de0147f501 100644 (file)
@@ -578,7 +578,7 @@ OpenSSLTLSTicketKey::OpenSSLTLSTicketKey()
 #endif /* HAVE_LIBSODIUM */
 }
 
-OpenSSLTLSTicketKey::OpenSSLTLSTicketKey(ifstream& file)
+OpenSSLTLSTicketKey::OpenSSLTLSTicketKey(std::ifstream& file)
 {
   file.read(reinterpret_cast<char*>(d_name), sizeof(d_name));
   file.read(reinterpret_cast<char*>(d_cipherKey), sizeof(d_cipherKey));
index c95ea99f0c2770fe079432594353eb8709dbd580..b6e6dc1e1e12e05029b1a07b4be879e280f917e8 100644 (file)
@@ -1,5 +1,6 @@
 #pragma once
 
+#include <atomic>
 #include <fstream>
 #include <map>
 #include <memory>
index 98f386dc3d252c4685845f1dce6db16d3d018d8d..09299d7979de51cd6bee4a7289315311022b5f6f 100644 (file)
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  */
 #pragma once
-#include <pthread.h>
-#include <errno.h>
-#include "misc.hh"
-#include "pdnsexception.hh"
+#include <shared_mutex>
 
 class ReadWriteLock
 {
 public:
   ReadWriteLock()
   {
-    if (pthread_rwlock_init(&d_lock, nullptr) != 0) {
-      throw std::runtime_error("Error creating a read-write lock: " + stringerror());
-    }
-  }
-
-  ~ReadWriteLock() {
-    pthread_rwlock_destroy(&d_lock);
   }
 
   ReadWriteLock(const ReadWriteLock& rhs) = delete;
+  ReadWriteLock(ReadWriteLock&& rhs) = delete;
   ReadWriteLock& operator=(const ReadWriteLock& rhs) = delete;
 
-  pthread_rwlock_t* getLock()
+  std::shared_mutex& getLock()
   {
-    return &d_lock;
+    return d_lock;
   }
 
 private:
-  pthread_rwlock_t d_lock;
+  std::shared_mutex d_lock;
 };
 
 class ReadLock
@@ -62,30 +53,19 @@ public:
   {
   }
 
-  ~ReadLock()
-  {
-    if(d_lock) // may have been moved
-      pthread_rwlock_unlock(d_lock);
-  }
-
+  ReadLock(const ReadLock& rhs) = delete;
+  ReadLock& operator=(const ReadLock& rhs) = delete;
   ReadLock(ReadLock&& rhs)
   {
-    d_lock = rhs.d_lock;
-    rhs.d_lock = nullptr;
+    d_lock = std::move(rhs.d_lock);
   }
-  ReadLock(const ReadLock& rhs) = delete;
-  ReadLock& operator=(const ReadLock& rhs) = delete;
 
 private:
-  ReadLock(pthread_rwlock_t *lock) : d_lock(lock)
+  ReadLock(std::shared_mutex& lock) : d_lock(lock)
   {
-    int err;
-    if((err = pthread_rwlock_rdlock(d_lock))) {
-      throw PDNSException("error acquiring rwlock readlock: "+stringerror(err));
-    }
   }
 
pthread_rwlock_t *d_lock;
 std::shared_lock<std::shared_mutex> d_lock;
 };
 
 class WriteLock
@@ -99,31 +79,19 @@ public:
   {
   }
 
+  WriteLock(const WriteLock& rhs) = delete;
+  WriteLock& operator=(const WriteLock& rhs) = delete;
   WriteLock(WriteLock&& rhs)
   {
-    d_lock = rhs.d_lock;
-    rhs.d_lock=0;
+    d_lock = std::move(rhs.d_lock);
   }
 
-  ~WriteLock()
-  {
-    if(d_lock) // might have been moved
-      pthread_rwlock_unlock(d_lock);
-  }
-
-  WriteLock(const WriteLock& rhs) = delete;
-  WriteLock& operator=(const WriteLock& rhs) = delete;
-
 private:
-  WriteLock(pthread_rwlock_t *lock) : d_lock(lock)
+  WriteLock(std::shared_mutex& lock) : d_lock(lock)
   {
-    int err;
-    if((err = pthread_rwlock_wrlock(d_lock))) {
-      throw PDNSException("error acquiring rwlock wrlock: "+stringerror(err));
-    }
   }
 
-  pthread_rwlock_t *d_lock;
+  std::unique_lock<std::shared_mutex> d_lock;
 };
 
 class TryReadLock
@@ -137,40 +105,20 @@ public:
   {
   }
 
-  TryReadLock(TryReadLock&& rhs)
-  {
-    d_lock = rhs.d_lock;
-    rhs.d_lock = nullptr;
-    d_havelock = rhs.d_havelock;
-    rhs.d_havelock = false;
-  }
-
-  ~TryReadLock()
-  {
-    if(d_havelock && d_lock)
-      pthread_rwlock_unlock(d_lock);
-  }
-
   TryReadLock(const TryReadLock& rhs) = delete;
   TryReadLock& operator=(const TryReadLock& rhs) = delete;
 
-  bool gotIt()
+  bool gotIt() const
   {
-    return d_havelock;
+    return d_lock.owns_lock();
   }
 
 private:
-  TryReadLock(pthread_rwlock_t *lock) : d_lock(lock)
+  TryReadLock(std::shared_mutex& lock) : d_lock(lock, std::try_to_lock)
   {
-    int err;
-    if((err = pthread_rwlock_tryrdlock(d_lock)) && err!=EBUSY) {
-      throw PDNSException("error acquiring rwlock tryrdlock: "+stringerror(err));
-    }
-    d_havelock=(err==0);
   }
 
-  pthread_rwlock_t *d_lock;
-  bool d_havelock;
+  std::shared_lock<std::shared_mutex> d_lock;
 };
 
 class TryWriteLock
@@ -184,40 +132,18 @@ public:
   {
   }
 
-  TryWriteLock(TryWriteLock&& rhs)
-  {
-    d_lock = rhs.d_lock;
-    rhs.d_lock = nullptr;
-    d_havelock = rhs.d_havelock;
-    rhs.d_havelock = false;
-  }
-
-  ~TryWriteLock()
-  {
-    if(d_havelock && d_lock) // we might be moved
-      pthread_rwlock_unlock(d_lock);
-  }
-
   TryWriteLock(const TryWriteLock& rhs) = delete;
   TryWriteLock& operator=(const TryWriteLock& rhs) = delete;
 
-  bool gotIt()
+  bool gotIt() const
   {
-    return d_havelock;
+    return d_lock.owns_lock();
   }
 
 private:
-  TryWriteLock(pthread_rwlock_t *lock) : d_lock(lock)
+  TryWriteLock(std::shared_mutex& lock) : d_lock(lock, std::try_to_lock)
   {
-    d_havelock=false;
-    int err;
-    if((err = pthread_rwlock_trywrlock(d_lock)) && err!=EBUSY) {
-      throw PDNSException("error acquiring rwlock tryrwlock: "+stringerror(err));
-    }
-    d_havelock=(err==0);
   }
 
-  pthread_rwlock_t *d_lock;
-  bool d_havelock;
+  std::unique_lock<std::shared_mutex> d_lock;
 };
-
index 46e844c60aa20905b8ae4258d8c9a3ce201865c1..112be6f016a64fc15ccb7091433bda3263d37e58 100644 (file)
@@ -4,9 +4,11 @@
 #include "config.h"
 #endif
 #include <boost/test/unit_test.hpp>
-#include "lock.hh"
 #include <thread>
 
+#include "lock.hh"
+#include "pdnsexception.hh"
+
 using namespace boost;
 
 BOOST_AUTO_TEST_SUITE(test_lock_hh)
@@ -16,8 +18,9 @@ static std::vector<ReadWriteLock> g_locks(1000);
 static void lthread()
 {
   std::vector<ReadLock> rlocks;
-  for(auto& pp : g_locks)
+  for(auto& pp : g_locks) {
     rlocks.emplace_back(pp);
+  }
 }
 
 BOOST_AUTO_TEST_CASE(test_pdns_lock)