Skip to content

Commit 9d63d76

Browse files
committed
[libcxx] LWG4172 fix self-move-assignment in {unique|shared}_lock
1 parent a088b0e commit 9d63d76

File tree

5 files changed

+17
-23
lines changed

5 files changed

+17
-23
lines changed

libcxx/docs/Status/Cxx2cIssues.csv

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@
113113
"","","","","",""
114114
"`LWG3578 <https://wg21.link/3578>`__","Iterator SCARYness in the context of associative container merging","2025-02 (Hagenberg)","","",""
115115
"`LWG3956 <https://wg21.link/3956>`__","``chrono::parse`` uses ``from_stream`` as a customization point","2025-02 (Hagenberg)","","",""
116-
"`LWG4172 <https://wg21.link/4172>`__","``unique_lock`` self-move-assignment is broken","2025-02 (Hagenberg)","","",""
116+
"`LWG4172 <https://wg21.link/4172>`__","``unique_lock`` self-move-assignment is broken","2025-02 (Hagenberg)","|Complete|","21",""
117117
"`LWG4175 <https://wg21.link/4175>`__","``get_env()`` specified in terms of ``as_const()`` but this doesn't work with rvalue senders","2025-02 (Hagenberg)","","",""
118118
"`LWG4179 <https://wg21.link/4179>`__","Wrong range in ``[alg.search]``","2025-02 (Hagenberg)","","",""
119119
"`LWG4186 <https://wg21.link/4186>`__","``regex_traits::transform_primary`` mistakenly detects ``typeid`` of a function","2025-02 (Hagenberg)","","",""

libcxx/include/__mutex/unique_lock.h

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -74,13 +74,7 @@ class _LIBCPP_TEMPLATE_VIS unique_lock {
7474
}
7575

7676
_LIBCPP_HIDE_FROM_ABI unique_lock& operator=(unique_lock&& __u) _NOEXCEPT {
77-
if (__owns_)
78-
__m_->unlock();
79-
80-
__m_ = __u.__m_;
81-
__owns_ = __u.__owns_;
82-
__u.__m_ = nullptr;
83-
__u.__owns_ = false;
77+
unique_lock{std::move(__u)}.swap(*this);
8478
return *this;
8579
}
8680

libcxx/include/shared_mutex

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -356,14 +356,7 @@ public:
356356
}
357357

358358
_LIBCPP_HIDE_FROM_ABI shared_lock& operator=(shared_lock&& __u) _NOEXCEPT {
359-
if (__owns_)
360-
__m_->unlock_shared();
361-
__m_ = nullptr;
362-
__owns_ = false;
363-
__m_ = __u.__m_;
364-
__owns_ = __u.__owns_;
365-
__u.__m_ = nullptr;
366-
__u.__owns_ = false;
359+
shared_lock{std::move(__u)}.swap(*this);
367360
return *this;
368361
}
369362

libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.shared/thread.lock.shared.cons/move_assign.pass.cpp

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,8 @@
2121

2222
#include "test_macros.h"
2323

24-
25-
int main(int, char**)
26-
{
27-
{
24+
int main(int, char**) {
25+
{
2826
typedef std::shared_timed_mutex M;
2927
M m0;
3028
M m1;
@@ -35,8 +33,13 @@ int main(int, char**)
3533
assert(lk1.owns_lock() == true);
3634
assert(lk0.mutex() == nullptr);
3735
assert(lk0.owns_lock() == false);
38-
}
39-
{
36+
37+
// Test self move assignment.
38+
lk1 = std::move(lk1);
39+
assert(lk1.mutex() == std::addressof(m0));
40+
assert(lk1.owns_lock());
41+
}
42+
{
4043
typedef nasty_mutex M;
4144
M m0;
4245
M m1;
@@ -47,7 +50,7 @@ int main(int, char**)
4750
assert(lk1.owns_lock() == true);
4851
assert(lk0.mutex() == nullptr);
4952
assert(lk0.owns_lock() == false);
50-
}
53+
}
5154

5255
return 0;
5356
}

libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.unique/thread.lock.unique.cons/move_assign.pass.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,9 @@ int main(int, char**) {
3232
assert(lk0.mutex() == nullptr);
3333
assert(!lk0.owns_lock());
3434

35+
// Test Test self move assignment.
36+
lk1 = std::move(lk1);
37+
assert(lk1.mutex() == std::addressof(m0));
38+
assert(lk1.owns_lock());
3539
return 0;
3640
}

0 commit comments

Comments
 (0)