diff --git a/base_cpp/include/embb/base/internal/mutex-inl.h b/base_cpp/include/embb/base/internal/mutex-inl.h index 0d9b336..86f66ac 100644 --- a/base_cpp/include/embb/base/internal/mutex-inl.h +++ b/base_cpp/include/embb/base/internal/mutex-inl.h @@ -28,6 +28,7 @@ #define EMBB_BASE_INTERNAL_MUTEX_INL_H_ #include +#include namespace embb { namespace base { @@ -95,8 +96,8 @@ void UniqueLock::Unlock() { template void UniqueLock::Swap(UniqueLock& other) { - locked_ = other.locked_; - mutex_ = other.Release(); + std::swap(mutex_, other.mutex_); + std::swap(locked_, other.locked_); } template diff --git a/base_cpp/include/embb/base/mutex.h b/base_cpp/include/embb/base/mutex.h index 0b8c7e3..1d63027 100644 --- a/base_cpp/include/embb/base/mutex.h +++ b/base_cpp/include/embb/base/mutex.h @@ -439,11 +439,11 @@ class UniqueLock { void Unlock(); /** - * Transfers ownership of a mutex to this lock. + * Exchanges ownership of the wrapped mutex with another lock. */ void Swap( UniqueLock& other - /**< [IN/OUT] Lock from which ownership shall be transferred */ + /**< [IN/OUT] The lock to exchange ownership with */ ); /** diff --git a/base_cpp/test/mutex_test.cc b/base_cpp/test/mutex_test.cc index 17e5c9e..48cc0a9 100644 --- a/base_cpp/test/mutex_test.cc +++ b/base_cpp/test/mutex_test.cc @@ -191,13 +191,21 @@ void MutexTest::TestUniqueLock() { } { // Test lock swapping - UniqueLock<> lock1; - UniqueLock<> lock2(mutex_); - PT_EXPECT_EQ(lock1.OwnsLock(), false); - PT_EXPECT_EQ(lock2.OwnsLock(), true); - lock1.Swap(lock2); + UniqueLock<> lock1(mutex_); PT_EXPECT_EQ(lock1.OwnsLock(), true); - PT_EXPECT_EQ(lock2.OwnsLock(), false); + + { + UniqueLock<> lock2; + PT_EXPECT_EQ(lock2.OwnsLock(), false); + + lock1.Swap(lock2); + PT_EXPECT_EQ(lock1.OwnsLock(), false); + PT_EXPECT_EQ(lock2.OwnsLock(), true); + } + + // At this point, "lock2" was destroyed and "mutex_" must be unlocked. + UniqueLock<> lock3(mutex_, embb::base::try_lock); + PT_EXPECT_EQ(lock3.OwnsLock(), true); } }