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
This commit is contained in:
Guilherme Lawless 2020-09-02 21:44:14 +01:00 committed by GitHub
commit e091cd17a1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 18 additions and 0 deletions

View file

@ -178,4 +178,15 @@ TEST (locks, condition_variable_wait_until)
// Should be 1 report
ASSERT_EQ (num_matches (ss.str ()), 1);
}
TEST (locks, defer_lock)
{
std::mutex mutex;
nano::unique_lock<std::mutex> 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

View file

@ -72,6 +72,12 @@ mut (std::addressof (mutex))
lock_impl ();
}
template <typename Mutex, typename U>
unique_lock<Mutex, U>::unique_lock (Mutex & mutex, std::defer_lock_t) noexcept :
mut (std::addressof (mutex))
{
}
template <typename Mutex, typename U>
void unique_lock<Mutex, U>::lock_impl ()
{

View file

@ -56,6 +56,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;