Commit a3244754 by Danila Klimenko

Merge branch 'embb470_uniquelock_swap' into development

parents ca9ffed3 68609388
...@@ -28,6 +28,7 @@ ...@@ -28,6 +28,7 @@
#define EMBB_BASE_INTERNAL_MUTEX_INL_H_ #define EMBB_BASE_INTERNAL_MUTEX_INL_H_
#include <cassert> #include <cassert>
#include <algorithm>
namespace embb { namespace embb {
namespace base { namespace base {
...@@ -95,8 +96,8 @@ void UniqueLock<Mutex>::Unlock() { ...@@ -95,8 +96,8 @@ void UniqueLock<Mutex>::Unlock() {
template<typename Mutex> template<typename Mutex>
void UniqueLock<Mutex>::Swap(UniqueLock<Mutex>& other) { void UniqueLock<Mutex>::Swap(UniqueLock<Mutex>& other) {
locked_ = other.locked_; std::swap(mutex_, other.mutex_);
mutex_ = other.Release(); std::swap(locked_, other.locked_);
} }
template<typename Mutex> template<typename Mutex>
......
...@@ -439,11 +439,11 @@ class UniqueLock { ...@@ -439,11 +439,11 @@ class UniqueLock {
void Unlock(); void Unlock();
/** /**
* Transfers ownership of a mutex to this lock. * Exchanges ownership of the wrapped mutex with another lock.
*/ */
void Swap( void Swap(
UniqueLock<Mutex>& other UniqueLock<Mutex>& other
/**< [IN/OUT] Lock from which ownership shall be transferred */ /**< [IN/OUT] The lock to exchange ownership with */
); );
/** /**
......
...@@ -191,13 +191,21 @@ void MutexTest::TestUniqueLock() { ...@@ -191,13 +191,21 @@ void MutexTest::TestUniqueLock() {
} }
{ // Test lock swapping { // Test lock swapping
UniqueLock<> lock1; UniqueLock<> lock1(mutex_);
UniqueLock<> lock2(mutex_);
PT_EXPECT_EQ(lock1.OwnsLock(), false);
PT_EXPECT_EQ(lock2.OwnsLock(), true);
lock1.Swap(lock2);
PT_EXPECT_EQ(lock1.OwnsLock(), true); PT_EXPECT_EQ(lock1.OwnsLock(), true);
{
UniqueLock<> lock2;
PT_EXPECT_EQ(lock2.OwnsLock(), false); 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);
} }
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment