-
Notifications
You must be signed in to change notification settings - Fork 14.9k
[libc++] Add thread safety annotations for std::lock #154078
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
@llvm/pr-subscribers-libcxx Author: Nikolas Klauser (philnik777) ChangesFixes #151733 Full diff: https://github.com/llvm/llvm-project/pull/154078.diff 2 Files Affected:
diff --git a/libcxx/include/mutex b/libcxx/include/mutex
index 78d8c8a9bcc6e..3f546f039f4c7 100644
--- a/libcxx/include/mutex
+++ b/libcxx/include/mutex
@@ -351,7 +351,7 @@ _LIBCPP_HIDE_FROM_ABI int try_lock(_L0& __l0, _L1& __l1, _L2& __l2, _L3&... __l3
# endif // _LIBCPP_CXX03_LANG
template <class _L0, class _L1>
-_LIBCPP_HIDE_FROM_ABI void lock(_L0& __l0, _L1& __l1) {
+_LIBCPP_HIDE_FROM_ABI void lock(_L0& __l0, _L1& __l1) _LIBCPP_ACQUIRE_CAPABILITY(__l0, __l1) {
while (true) {
{
unique_lock<_L0> __u0(__l0);
@@ -411,7 +411,8 @@ void __lock_first(int __i, _L0& __l0, _L1& __l1, _L2& __l2, _L3&... __l3) {
}
template <class _L0, class _L1, class _L2, class... _L3>
-inline _LIBCPP_HIDE_FROM_ABI void lock(_L0& __l0, _L1& __l1, _L2& __l2, _L3&... __l3) {
+inline _LIBCPP_HIDE_FROM_ABI void lock(_L0& __l0, _L1& __l1, _L2& __l2, _L3&... __l3)
+ _LIBCPP_ACQUIRE_CAPABILITY(__l0, __l1, __l2, __l3...) {
std::__lock_first(0, __l0, __l1, __l2, __l3...);
}
diff --git a/libcxx/test/extensions/clang/thread/thread.mutex/lock.verify.cpp b/libcxx/test/extensions/clang/thread/thread.mutex/lock.verify.cpp
new file mode 100644
index 0000000000000..0b3f87068589a
--- /dev/null
+++ b/libcxx/test/extensions/clang/thread/thread.mutex/lock.verify.cpp
@@ -0,0 +1,43 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+// UNSUPPORTED: no-threads
+
+// <mutex>
+
+// GCC doesn't have thread safety attributes
+// UNSUPPORTED: gcc
+
+// ADDITIONAL_COMPILE_FLAGS: -Wthread-safety -Wno-comment
+
+// XFAIL: FROZEN-CXX03-HEADERS-FIXME
+
+#include <mutex>
+
+std::mutex m0;
+std::mutex m1;
+std::mutex m2;
+std::mutex m3;
+
+void f1() {
+ std::lock(m0, m1);
+} // expected-warning {{mutex 'm0' is still held at the end of function}} \
+ expected-warning {{mutex 'm1' is still held at the end of function}}
+
+void f2() {
+ std::lock(m0, m1, m2);
+} // expected-warning {{mutex 'm0' is still held at the end of function}} \
+ expected-warning {{mutex 'm1' is still held at the end of function}} \
+ expected-warning {{mutex 'm2' is still held at the end of function}}
+
+void f3() {
+ std::lock(m0, m1, m2, m3);
+} // expected-warning {{mutex 'm0' is still held at the end of function}} \
+ expected-warning {{mutex 'm1' is still held at the end of function}} \
+ expected-warning {{mutex 'm2' is still held at the end of function}} \
+ expected-warning {{mutex 'm3' is still held at the end of function}}
|
6f85619
to
7364368
Compare
7364368
to
5fa1bcc
Compare
/cherry-pick 0a2eb85 |
LLVM Buildbot has detected a new failure on builder Full details are available at: https://lab.llvm.org/buildbot/#/builders/159/builds/30029 Here is the relevant piece of the build log for the reference
|
Failed to cherry-pick: 0a2eb85 https://github.com/llvm/llvm-project/actions/runs/17425303760 Please manually backport the fix and push it to your github fork. Once this is done, please create a pull request |
Looks like that this PR missed handling for Apple Clang... |
It looks like this patch leads to
Do more |
…4078)" This reverts commit 0a2eb85. Reverting because this leads to a false positive with -Wthread-safety. See llvm#156760.
Fixes #151733