From: Remi Gacogne Date: Thu, 25 Mar 2021 09:56:48 +0000 (+0100) Subject: speedtest: Add tests for the ReadWriteLock class X-Git-Tag: dnsdist-1.6.0-rc1~42^2~1 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=4f81c9a7a22fc36d4ed96da1dea382221ab7d5e6;p=thirdparty%2Fpdns.git speedtest: Add tests for the ReadWriteLock class --- diff --git a/pdns/speedtest.cc b/pdns/speedtest.cc index bcbf36cce6..078acd058c 100644 --- a/pdns/speedtest.cc +++ b/pdns/speedtest.cc @@ -10,6 +10,7 @@ #include #include "uuid-utils.hh" #include "dnssecinfra.hh" +#include "lock.hh" #ifndef RECURSOR #include "statbag.hh" @@ -862,6 +863,100 @@ struct NSEC3HashTest DNSName d_name = DNSName("www.example.com"); }; +struct ReadWriteLockSharedTest +{ + explicit ReadWriteLockSharedTest(ReadWriteLock& lock): d_lock(lock) + { + } + + string getName() const { return "RW lock shared"; } + + void operator()() const { + for (size_t idx = 0; idx < 1000; ) { + ReadLock wl(d_lock); + ++idx; + } + } + +private: + ReadWriteLock& d_lock; +}; + +struct ReadWriteLockExclusiveTest +{ + explicit ReadWriteLockExclusiveTest(ReadWriteLock& lock): d_lock(lock) + { + } + + string getName() const { return "RW lock exclusive"; } + + void operator()() const { + for (size_t idx = 0; idx < 1000; ) { + WriteLock wl(d_lock); + ++idx; + } + } + +private: + ReadWriteLock& d_lock; +}; + +struct ReadWriteLockExclusiveTryTest +{ + explicit ReadWriteLockExclusiveTryTest(ReadWriteLock& lock, bool contended): d_lock(lock), d_contended(contended) + { + } + + string getName() const { return "RW lock try exclusive - " + std::string(d_contended ? "contended" : "non-contended"); } + + void operator()() const { + for (size_t idx = 0; idx < 1000; ) { + TryWriteLock wl(d_lock); + if (!wl.gotIt() && !d_contended) { + cerr<<"Error getting the lock"<