[libcxx-commits] [libcxx] [libc++][ThreadSafety] Add thread safety annotations for variadic std::scoped_lock (PR #204462)

Nikolas Klauser via libcxx-commits libcxx-commits at lists.llvm.org
Thu Jun 18 01:47:23 PDT 2026


================
@@ -0,0 +1,78 @@
+//===----------------------------------------------------------------------===//
+//
+// 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, c++03, c++11, c++14
+
+// <mutex>
+
+// GCC doesn't have thread safety attributes
+// UNSUPPORTED: gcc
+
+// ADDITIONAL_COMPILE_FLAGS: -Wthread-safety
+
+#include <mutex>
+
+#include "test_macros.h"
+
+#if TEST_STD_VER >= 17 && TEST_CLANG_VER >= 2101
+
+std::mutex m1;
+std::mutex m2;
+int data1 __attribute__((guarded_by(m1)));
+int data2 __attribute__((guarded_by(m2)));
+int data3;
+
+static void no_mutex() {
+  std::scoped_lock<> lock;
+  data3++;
+}
+
+static void one_mutex() {
+  std::scoped_lock<std::mutex> lock(m1);
+  data1++;
+}
+
+static void two_mutexes() {
+  std::scoped_lock<std::mutex, std::mutex> lock(m1, m2);
+  data1++;
+  data2++;
+}
+
+static void adopt_none() {
+  std::scoped_lock<> lock(std::adopt_lock);
+  data3++;
+}
+
+static void adopt_one() {
+  m1.lock();
+  std::scoped_lock<std::mutex> lock(std::adopt_lock, m1);
+  data1++;
+}
+
+static void adopt_two() {
+  m1.lock();
+  m2.lock();
+  std::scoped_lock<std::mutex, std::mutex> lock(std::adopt_lock, m1, m2);
+  data1++;
+  data2++;
+}
+
+#endif
+
+int main(int, char**) {
+#if TEST_STD_VER >= 17 && TEST_CLANG_VER >= 2101
----------------
philnik777 wrote:

This is redundant, since the C++ version is already checked by lit.

https://github.com/llvm/llvm-project/pull/204462


More information about the libcxx-commits mailing list