From 7ee6a6a8f203b1849aaa9409e8faa31570495c19 Mon Sep 17 00:00:00 2001 From: Guilherme Lawless Date: Wed, 2 Sep 2020 21:44:14 +0100 Subject: [PATCH] Add support for defer_lock in nano::unique_lock (#2909) Needs NANO_TIMED_LOCKS CMake variable > 0 to test, otherwise it defaults to using std::unique_lock This is necessary to build since #2901 but not caught by CI due to not using this developer-oriented feature --- nano/core_test/locks.cpp | 11 +++++++++++ nano/lib/locks.cpp | 6 ++++++ nano/lib/locks.hpp | 1 + 3 files changed, 18 insertions(+) diff --git a/nano/core_test/locks.cpp b/nano/core_test/locks.cpp index bebb2dc25f..e767904186 100644 --- a/nano/core_test/locks.cpp +++ b/nano/core_test/locks.cpp @@ -130,4 +130,15 @@ TEST (locks, condition_variable) finished = true; t.join (); } + +TEST (locks, defer_lock) +{ + std::mutex mutex; + nano::unique_lock lock (mutex, std::defer_lock); + ASSERT_FALSE (lock.owns_lock ()); + ASSERT_TRUE (lock.try_lock ()); + ASSERT_TRUE (lock.owns_lock ()); + lock.unlock (); + ASSERT_FALSE (lock.owns_lock ()); +} #endif diff --git a/nano/lib/locks.cpp b/nano/lib/locks.cpp index e38a4e174a..3d259cae66 100644 --- a/nano/lib/locks.cpp +++ b/nano/lib/locks.cpp @@ -66,6 +66,12 @@ mut (std::addressof (mutex)) lock_impl (); } +template +unique_lock::unique_lock (Mutex & mutex, std::defer_lock_t) noexcept : +mut (std::addressof (mutex)) +{ +} + template void unique_lock::lock_impl () { diff --git a/nano/lib/locks.hpp b/nano/lib/locks.hpp index f0b34bdb6d..1a982354fc 100644 --- a/nano/lib/locks.hpp +++ b/nano/lib/locks.hpp @@ -46,6 +46,7 @@ class unique_lock final public: unique_lock () = default; explicit unique_lock (Mutex & mutex_a); + unique_lock (Mutex & mutex_a, std::defer_lock_t) noexcept; unique_lock (unique_lock && other) = delete; unique_lock & operator= (unique_lock && other) noexcept; ~unique_lock () noexcept;