Skip to content

Commit

Permalink
Add support for defer_lock in nano::unique_lock (#2909)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
guilhermelawless committed Sep 2, 2020
1 parent acabda9 commit 7ee6a6a
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 0 deletions.
11 changes: 11 additions & 0 deletions nano/core_test/locks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -130,4 +130,15 @@ TEST (locks, condition_variable)
finished = true;
t.join ();
}

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
6 changes: 6 additions & 0 deletions nano/lib/locks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,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 ()
{
Expand Down
1 change: 1 addition & 0 deletions nano/lib/locks.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 7ee6a6a

Please sign in to comment.