[libcxx-commits] [libcxx] [libc++] Make locks available with _LIBCPP_HAS_NO_THREADS (PR #98717)

Petr Hosek via libcxx-commits libcxx-commits at lists.llvm.org
Sat Jul 13 21:31:08 PDT 2024


https://github.com/petrhosek updated https://github.com/llvm/llvm-project/pull/98717

>From 1aec8cd07c1b0f6f45cf54e640d8265299ee86c4 Mon Sep 17 00:00:00 2001
From: Petr Hosek <phosek at google.com>
Date: Fri, 12 Jul 2024 22:50:34 -0700
Subject: [PATCH 1/5] [libcxx] Make locks available with _LIBCPP_HAS_NO_THREADS

This change makes std::lock_guard, std::unique_lock, std::scoped_lock,
and std::shared_lock available when _LIBCPP_HAS_NO_THREADS is set.
These classes are generic and don't require threading support, and are
regularly used even in environments where threading isn't available like
embedded.

fixes #89891
---
 libcxx/include/__mutex/lock_guard.h  |  4 ----
 libcxx/include/__mutex/unique_lock.h |  4 ----
 libcxx/include/mutex                 |  6 +++---
 libcxx/include/shared_mutex          | 15 +++++++--------
 4 files changed, 10 insertions(+), 19 deletions(-)

diff --git a/libcxx/include/__mutex/lock_guard.h b/libcxx/include/__mutex/lock_guard.h
index 8340b9bbd4453..ef56896be9f68 100644
--- a/libcxx/include/__mutex/lock_guard.h
+++ b/libcxx/include/__mutex/lock_guard.h
@@ -16,8 +16,6 @@
 #  pragma GCC system_header
 #endif
 
-#ifndef _LIBCPP_HAS_NO_THREADS
-
 _LIBCPP_BEGIN_NAMESPACE_STD
 
 template <class _Mutex>
@@ -47,6 +45,4 @@ _LIBCPP_CTAD_SUPPORTED_FOR_TYPE(lock_guard);
 
 _LIBCPP_END_NAMESPACE_STD
 
-#endif // _LIBCPP_HAS_NO_THREADS
-
 #endif // _LIBCPP___MUTEX_LOCK_GUARD_H
diff --git a/libcxx/include/__mutex/unique_lock.h b/libcxx/include/__mutex/unique_lock.h
index 4a616ba51ee1c..5df791de4c742 100644
--- a/libcxx/include/__mutex/unique_lock.h
+++ b/libcxx/include/__mutex/unique_lock.h
@@ -22,8 +22,6 @@
 #  pragma GCC system_header
 #endif
 
-#ifndef _LIBCPP_HAS_NO_THREADS
-
 _LIBCPP_BEGIN_NAMESPACE_STD
 
 template <class _Mutex>
@@ -172,6 +170,4 @@ inline _LIBCPP_HIDE_FROM_ABI void swap(unique_lock<_Mutex>& __x, unique_lock<_Mu
 
 _LIBCPP_END_NAMESPACE_STD
 
-#endif // _LIBCPP_HAS_NO_THREADS
-
 #endif // _LIBCPP___MUTEX_UNIQUE_LOCK_H
diff --git a/libcxx/include/mutex b/libcxx/include/mutex
index 02c52dd72f02b..d4d7539240094 100644
--- a/libcxx/include/mutex
+++ b/libcxx/include/mutex
@@ -419,8 +419,9 @@ inline _LIBCPP_HIDE_FROM_ABI void lock(_L0& __l0, _L1& __l1, _L2& __l2, _L3&...
 }
 
 #  endif // _LIBCPP_CXX03_LANG
+#endif   // !_LIBCPP_HAS_NO_THREADS
 
-#  if _LIBCPP_STD_VER >= 17
+#if _LIBCPP_STD_VER >= 17
 template <class... _Mutexes>
 class _LIBCPP_TEMPLATE_VIS scoped_lock;
 
@@ -491,8 +492,7 @@ private:
 };
 _LIBCPP_CTAD_SUPPORTED_FOR_TYPE(scoped_lock);
 
-#  endif // _LIBCPP_STD_VER >= 17
-#endif   // !_LIBCPP_HAS_NO_THREADS
+#endif // _LIBCPP_STD_VER >= 17
 
 _LIBCPP_END_NAMESPACE_STD
 
diff --git a/libcxx/include/shared_mutex b/libcxx/include/shared_mutex
index 397ac290d9b2e..5918af4a214c1 100644
--- a/libcxx/include/shared_mutex
+++ b/libcxx/include/shared_mutex
@@ -122,16 +122,11 @@ template <class Mutex>
 
 */
 
-#include <__config>
-
-#ifdef _LIBCPP_HAS_NO_THREADS
-#  error "<shared_mutex> is not supported since libc++ has been configured without support for threads."
-#endif
-
 #include <__chrono/duration.h>
 #include <__chrono/steady_clock.h>
 #include <__chrono/time_point.h>
 #include <__condition_variable/condition_variable.h>
+#include <__config>
 #include <__memory/addressof.h>
 #include <__mutex/mutex.h>
 #include <__mutex/tag_types.h>
@@ -152,6 +147,8 @@ _LIBCPP_PUSH_MACROS
 
 _LIBCPP_BEGIN_NAMESPACE_STD
 
+#  ifndef _LIBCPP_HAS_NO_THREADS
+
 struct _LIBCPP_EXPORTED_FROM_ABI __shared_mutex_base {
   mutex __mut_;
   condition_variable __gate1_;
@@ -181,7 +178,7 @@ struct _LIBCPP_EXPORTED_FROM_ABI __shared_mutex_base {
   //     native_handle_type native_handle(); // See 30.2.3
 };
 
-#  if _LIBCPP_STD_VER >= 17
+#    if _LIBCPP_STD_VER >= 17
 class _LIBCPP_EXPORTED_FROM_ABI _LIBCPP_THREAD_SAFETY_ANNOTATION(__capability__("shared_mutex")) shared_mutex {
   __shared_mutex_base __base_;
 
@@ -218,7 +215,7 @@ public:
   //     typedef __shared_mutex_base::native_handle_type native_handle_type;
   //     _LIBCPP_HIDE_FROM_ABI native_handle_type native_handle() { return __base::unlock_shared(); }
 };
-#  endif
+#    endif
 
 class _LIBCPP_EXPORTED_FROM_ABI
 _LIBCPP_THREAD_SAFETY_ANNOTATION(__capability__("shared_timed_mutex")) shared_timed_mutex {
@@ -308,6 +305,8 @@ bool shared_timed_mutex::try_lock_shared_until(const chrono::time_point<_Clock,
   return true;
 }
 
+#  endif   // !_LIBCPP_HAS_NO_THREADS
+
 template <class _Mutex>
 class shared_lock {
 public:

>From 2b4d4305582570620a27a6c7c30a4053da63143c Mon Sep 17 00:00:00 2001
From: Petr Hosek <phosek at google.com>
Date: Sat, 13 Jul 2024 16:28:26 -0700
Subject: [PATCH 2/5] Update test annotations

---
 .../thread.lock/thread.lock.guard/assign.compile.fail.cpp      | 2 ++
 .../thread.lock/thread.lock.guard/copy.compile.fail.cpp        | 2 ++
 .../thread.lock/thread.lock.scoped/adopt_lock.pass.cpp         | 3 +--
 .../thread.mutex/thread.lock/thread.lock.scoped/mutex.pass.cpp | 3 +--
 .../thread.lock.shared.locking/try_lock_for.pass.cpp           | 3 +--
 .../thread.lock.shared.locking/try_lock_until.pass.cpp         | 3 +--
 .../thread.lock.shared.locking/unlock.pass.cpp                 | 3 +--
 .../thread.lock.shared.mod/member_swap.pass.cpp                | 3 +--
 .../thread.lock.shared.mod/nonmember_swap.pass.cpp             | 3 +--
 .../thread.lock.shared/thread.lock.shared.mod/release.pass.cpp | 3 +--
 .../thread.lock.shared/thread.lock.shared.obs/op_bool.pass.cpp | 3 +--
 .../thread.lock.unique.locking/try_lock.pass.cpp               | 3 +--
 .../thread.lock.unique.locking/try_lock_for.pass.cpp           | 3 +--
 .../thread.lock.unique.locking/try_lock_until.pass.cpp         | 3 +--
 .../thread.lock.unique.mod/member_swap.pass.cpp                | 2 --
 .../thread.lock.unique.mod/nonmember_swap.pass.cpp             | 2 --
 .../thread.lock.unique/thread.lock.unique.mod/release.pass.cpp | 2 --
 17 files changed, 16 insertions(+), 30 deletions(-)

diff --git a/libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.guard/assign.compile.fail.cpp b/libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.guard/assign.compile.fail.cpp
index 2d0f438ed0391..330cae19d1fea 100644
--- a/libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.guard/assign.compile.fail.cpp
+++ b/libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.guard/assign.compile.fail.cpp
@@ -6,6 +6,8 @@
 //
 //===----------------------------------------------------------------------===//
 
+// UNSUPPORTED: no-threads
+
 // <mutex>
 
 // template <class Mutex> class lock_guard;
diff --git a/libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.guard/copy.compile.fail.cpp b/libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.guard/copy.compile.fail.cpp
index e99517e47e8c6..7e9dd43a6bb32 100644
--- a/libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.guard/copy.compile.fail.cpp
+++ b/libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.guard/copy.compile.fail.cpp
@@ -6,6 +6,8 @@
 //
 //===----------------------------------------------------------------------===//
 
+// UNSUPPORTED: no-threads
+
 // <mutex>
 
 // template <class Mutex> class lock_guard;
diff --git a/libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.scoped/adopt_lock.pass.cpp b/libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.scoped/adopt_lock.pass.cpp
index 83b29ff669dca..a48caf7fdbb51 100644
--- a/libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.scoped/adopt_lock.pass.cpp
+++ b/libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.scoped/adopt_lock.pass.cpp
@@ -5,8 +5,7 @@
 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
 //
 //===----------------------------------------------------------------------===//
-//
-// UNSUPPORTED: no-threads
+
 // UNSUPPORTED: c++03, c++11, c++14
 
 // <mutex>
diff --git a/libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.scoped/mutex.pass.cpp b/libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.scoped/mutex.pass.cpp
index f953fa4f8d6df..0812c33c22226 100644
--- a/libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.scoped/mutex.pass.cpp
+++ b/libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.scoped/mutex.pass.cpp
@@ -5,8 +5,7 @@
 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
 //
 //===----------------------------------------------------------------------===//
-//
-// UNSUPPORTED: no-threads
+
 // UNSUPPORTED: c++03, c++11, c++14
 
 // <mutex>
diff --git a/libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.shared/thread.lock.shared.locking/try_lock_for.pass.cpp b/libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.shared/thread.lock.shared.locking/try_lock_for.pass.cpp
index d28ae395ccb0d..3b3c34f0b9351 100644
--- a/libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.shared/thread.lock.shared.locking/try_lock_for.pass.cpp
+++ b/libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.shared/thread.lock.shared.locking/try_lock_for.pass.cpp
@@ -5,8 +5,7 @@
 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
 //
 //===----------------------------------------------------------------------===//
-//
-// UNSUPPORTED: no-threads
+
 // UNSUPPORTED: c++03, c++11
 
 // <shared_mutex>
diff --git a/libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.shared/thread.lock.shared.locking/try_lock_until.pass.cpp b/libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.shared/thread.lock.shared.locking/try_lock_until.pass.cpp
index 880bf1cbd4999..ab5f3c7b6a935 100644
--- a/libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.shared/thread.lock.shared.locking/try_lock_until.pass.cpp
+++ b/libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.shared/thread.lock.shared.locking/try_lock_until.pass.cpp
@@ -5,8 +5,7 @@
 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
 //
 //===----------------------------------------------------------------------===//
-//
-// UNSUPPORTED: no-threads
+
 // UNSUPPORTED: c++03, c++11
 
 // <shared_mutex>
diff --git a/libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.shared/thread.lock.shared.locking/unlock.pass.cpp b/libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.shared/thread.lock.shared.locking/unlock.pass.cpp
index 4f31a75f68dec..0fcab6d346699 100644
--- a/libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.shared/thread.lock.shared.locking/unlock.pass.cpp
+++ b/libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.shared/thread.lock.shared.locking/unlock.pass.cpp
@@ -5,8 +5,7 @@
 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
 //
 //===----------------------------------------------------------------------===//
-//
-// UNSUPPORTED: no-threads
+
 // UNSUPPORTED: c++03, c++11
 
 // <shared_mutex>
diff --git a/libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.shared/thread.lock.shared.mod/member_swap.pass.cpp b/libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.shared/thread.lock.shared.mod/member_swap.pass.cpp
index aeca2022ba9c9..af904434efd82 100644
--- a/libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.shared/thread.lock.shared.mod/member_swap.pass.cpp
+++ b/libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.shared/thread.lock.shared.mod/member_swap.pass.cpp
@@ -5,8 +5,7 @@
 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
 //
 //===----------------------------------------------------------------------===//
-//
-// UNSUPPORTED: no-threads
+
 // UNSUPPORTED: c++03, c++11
 
 // <shared_mutex>
diff --git a/libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.shared/thread.lock.shared.mod/nonmember_swap.pass.cpp b/libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.shared/thread.lock.shared.mod/nonmember_swap.pass.cpp
index 25e5cf624caa8..a2827b0292e76 100644
--- a/libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.shared/thread.lock.shared.mod/nonmember_swap.pass.cpp
+++ b/libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.shared/thread.lock.shared.mod/nonmember_swap.pass.cpp
@@ -5,8 +5,7 @@
 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
 //
 //===----------------------------------------------------------------------===//
-//
-// UNSUPPORTED: no-threads
+
 // UNSUPPORTED: c++03, c++11
 
 // <shared_mutex>
diff --git a/libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.shared/thread.lock.shared.mod/release.pass.cpp b/libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.shared/thread.lock.shared.mod/release.pass.cpp
index 9dfdd0b0ded2f..412b9ba023827 100644
--- a/libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.shared/thread.lock.shared.mod/release.pass.cpp
+++ b/libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.shared/thread.lock.shared.mod/release.pass.cpp
@@ -5,8 +5,7 @@
 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
 //
 //===----------------------------------------------------------------------===//
-//
-// UNSUPPORTED: no-threads
+
 // UNSUPPORTED: c++03, c++11
 
 // <shared_mutex>
diff --git a/libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.shared/thread.lock.shared.obs/op_bool.pass.cpp b/libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.shared/thread.lock.shared.obs/op_bool.pass.cpp
index 1186671ad04d2..e26e8122ab191 100644
--- a/libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.shared/thread.lock.shared.obs/op_bool.pass.cpp
+++ b/libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.shared/thread.lock.shared.obs/op_bool.pass.cpp
@@ -5,8 +5,7 @@
 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
 //
 //===----------------------------------------------------------------------===//
-//
-// UNSUPPORTED: no-threads
+
 // UNSUPPORTED: c++03, c++11
 
 // <shared_mutex>
diff --git a/libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.unique/thread.lock.unique.locking/try_lock.pass.cpp b/libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.unique/thread.lock.unique.locking/try_lock.pass.cpp
index 4cf5ec2ab5ccf..9c6a971bc9f33 100644
--- a/libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.unique/thread.lock.unique.locking/try_lock.pass.cpp
+++ b/libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.unique/thread.lock.unique.locking/try_lock.pass.cpp
@@ -5,8 +5,7 @@
 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
 //
 //===----------------------------------------------------------------------===//
-//
-// UNSUPPORTED: no-threads
+
 // UNSUPPORTED: c++03
 // ALLOW_RETRIES: 2
 
diff --git a/libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.unique/thread.lock.unique.locking/try_lock_for.pass.cpp b/libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.unique/thread.lock.unique.locking/try_lock_for.pass.cpp
index 8e7004e5eec85..b211ce3afce07 100644
--- a/libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.unique/thread.lock.unique.locking/try_lock_for.pass.cpp
+++ b/libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.unique/thread.lock.unique.locking/try_lock_for.pass.cpp
@@ -5,8 +5,7 @@
 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
 //
 //===----------------------------------------------------------------------===//
-//
-// UNSUPPORTED: no-threads
+
 // UNSUPPORTED: c++03
 
 // <mutex>
diff --git a/libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.unique/thread.lock.unique.locking/try_lock_until.pass.cpp b/libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.unique/thread.lock.unique.locking/try_lock_until.pass.cpp
index 077bc517399ab..67c0cb94e9f90 100644
--- a/libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.unique/thread.lock.unique.locking/try_lock_until.pass.cpp
+++ b/libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.unique/thread.lock.unique.locking/try_lock_until.pass.cpp
@@ -5,8 +5,7 @@
 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
 //
 //===----------------------------------------------------------------------===//
-//
-// UNSUPPORTED: no-threads
+
 // UNSUPPORTED: c++03
 
 // <mutex>
diff --git a/libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.unique/thread.lock.unique.mod/member_swap.pass.cpp b/libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.unique/thread.lock.unique.mod/member_swap.pass.cpp
index fc12d3baea202..79731dec09bd5 100644
--- a/libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.unique/thread.lock.unique.mod/member_swap.pass.cpp
+++ b/libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.unique/thread.lock.unique.mod/member_swap.pass.cpp
@@ -5,8 +5,6 @@
 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
 //
 //===----------------------------------------------------------------------===//
-//
-// UNSUPPORTED: no-threads
 
 // <mutex>
 
diff --git a/libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.unique/thread.lock.unique.mod/nonmember_swap.pass.cpp b/libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.unique/thread.lock.unique.mod/nonmember_swap.pass.cpp
index 03d268c4b9306..f4c1661556f94 100644
--- a/libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.unique/thread.lock.unique.mod/nonmember_swap.pass.cpp
+++ b/libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.unique/thread.lock.unique.mod/nonmember_swap.pass.cpp
@@ -5,8 +5,6 @@
 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
 //
 //===----------------------------------------------------------------------===//
-//
-// UNSUPPORTED: no-threads
 
 // <mutex>
 
diff --git a/libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.unique/thread.lock.unique.mod/release.pass.cpp b/libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.unique/thread.lock.unique.mod/release.pass.cpp
index 4f2d59c3d333d..fd2ed6dd5afb8 100644
--- a/libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.unique/thread.lock.unique.mod/release.pass.cpp
+++ b/libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.unique/thread.lock.unique.mod/release.pass.cpp
@@ -5,8 +5,6 @@
 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
 //
 //===----------------------------------------------------------------------===//
-//
-// UNSUPPORTED: no-threads
 
 // <mutex>
 

>From e06a8e6ebe3cc99a576b9bd27e990807fb15bb74 Mon Sep 17 00:00:00 2001
From: Petr Hosek <phosek at google.com>
Date: Sat, 13 Jul 2024 16:59:05 -0700
Subject: [PATCH 3/5] More tests

---
 .../thread.lock.guard/mutex.pass.cpp          | 28 ++++++++++---------
 .../thread.lock.shared.cons/mutex.pass.cpp    |  3 +-
 .../thread.lock.shared.locking/lock.pass.cpp  |  3 +-
 .../try_lock.pass.cpp                         |  3 +-
 4 files changed, 21 insertions(+), 16 deletions(-)

diff --git a/libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.guard/mutex.pass.cpp b/libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.guard/mutex.pass.cpp
index 6025b0c3b465b..a95c2ef0029de 100644
--- a/libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.guard/mutex.pass.cpp
+++ b/libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.guard/mutex.pass.cpp
@@ -18,27 +18,29 @@
 //     -> lock_guard<_Mutex>;  // C++17
 
 #include <mutex>
-#include <cstdlib>
 #include <cassert>
-
-#include "make_test_thread.h"
 #include "test_macros.h"
 
-std::mutex m;
+struct TestMutex {
+    bool locked = false;
+    TestMutex() = default;
+    ~TestMutex() { assert(!locked); }
 
-void do_try_lock() {
-  assert(m.try_lock() == false);
-}
+    void lock() { assert(!locked); locked = true; }
+    bool try_lock() { if (locked) return false; locked = true; return true; }
+    void unlock() { assert(locked); locked = false; }
+
+    TestMutex(TestMutex const&) = delete;
+    TestMutex& operator=(TestMutex const&) = delete;
+};
 
 int main(int, char**) {
+  TestMutex m;
   {
-    std::lock_guard<std::mutex> lg(m);
-    std::thread t = support::make_test_thread(do_try_lock);
-    t.join();
+    std::lock_guard<TestMutex> lg(m);
+    assert(m.locked);
   }
-
-  m.lock();
-  m.unlock();
+  assert(!m.locked);
 
 #if TEST_STD_VER >= 17
   std::lock_guard lg(m);
diff --git a/libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.shared/thread.lock.shared.cons/mutex.pass.cpp b/libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.shared/thread.lock.shared.cons/mutex.pass.cpp
index ece330134f2cd..863948763e2ef 100644
--- a/libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.shared/thread.lock.shared.cons/mutex.pass.cpp
+++ b/libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.shared/thread.lock.shared.cons/mutex.pass.cpp
@@ -6,7 +6,6 @@
 //
 //===----------------------------------------------------------------------===//
 
-// UNSUPPORTED: no-threads
 // UNSUPPORTED: c++03, c++11
 
 // <shared_mutex>
@@ -79,10 +78,12 @@ void test() {
 }
 
 int main(int, char**) {
+#ifndef TEST_HAS_NO_THREADS
 #if TEST_STD_VER >= 17
   test<std::shared_mutex>();
 #endif
   test<std::shared_timed_mutex>();
+#endif
   test<TrackedMutex>();
 
   // Use shared_lock with a dummy mutex class that tracks whether each
diff --git a/libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.shared/thread.lock.shared.locking/lock.pass.cpp b/libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.shared/thread.lock.shared.locking/lock.pass.cpp
index d36ca1d38f8f1..9097ac74d1272 100644
--- a/libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.shared/thread.lock.shared.locking/lock.pass.cpp
+++ b/libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.shared/thread.lock.shared.locking/lock.pass.cpp
@@ -6,7 +6,6 @@
 //
 //===----------------------------------------------------------------------===//
 
-// UNSUPPORTED: no-threads
 // UNSUPPORTED: c++03, c++11
 
 // <shared_mutex>
@@ -99,10 +98,12 @@ void test() {
 }
 
 int main(int, char**) {
+#ifndef TEST_HAS_NO_THREADS
 #if TEST_STD_VER >= 17
   test<std::shared_mutex>();
 #endif
   test<std::shared_timed_mutex>();
+#endif
   test<TrackedMutex>();
 
   // Use shared_lock with a dummy mutex class that tracks whether each
diff --git a/libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.shared/thread.lock.shared.locking/try_lock.pass.cpp b/libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.shared/thread.lock.shared.locking/try_lock.pass.cpp
index b6146680b6e35..ccab79d4db016 100644
--- a/libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.shared/thread.lock.shared.locking/try_lock.pass.cpp
+++ b/libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.shared/thread.lock.shared.locking/try_lock.pass.cpp
@@ -6,7 +6,6 @@
 //
 //===----------------------------------------------------------------------===//
 
-// UNSUPPORTED: no-threads
 // UNSUPPORTED: c++03, c++11
 
 // <shared_mutex>
@@ -104,10 +103,12 @@ void test() {
 }
 
 int main(int, char**) {
+#ifndef TEST_HAS_NO_THREADS
 #if TEST_STD_VER >= 17
   test<std::shared_mutex>();
 #endif
   test<std::shared_timed_mutex>();
+#endif
   test<TrackedMutex>();
 
   // Use shared_lock with a dummy mutex class that tracks whether each

>From 896cdc342ea226d06891cdf073294c424cee0fde Mon Sep 17 00:00:00 2001
From: Petr Hosek <phosek at google.com>
Date: Sat, 13 Jul 2024 17:38:15 -0700
Subject: [PATCH 4/5] Fix formatting

---
 libcxx/include/shared_mutex                   |  2 +-
 .../thread.lock.guard/mutex.pass.cpp          | 29 +++++++++++++------
 .../thread.lock.shared.cons/mutex.pass.cpp    |  4 +--
 .../thread.lock.shared.locking/lock.pass.cpp  |  4 +--
 .../try_lock.pass.cpp                         |  4 +--
 5 files changed, 27 insertions(+), 16 deletions(-)

diff --git a/libcxx/include/shared_mutex b/libcxx/include/shared_mutex
index 5918af4a214c1..63d24e0182fe5 100644
--- a/libcxx/include/shared_mutex
+++ b/libcxx/include/shared_mutex
@@ -305,7 +305,7 @@ bool shared_timed_mutex::try_lock_shared_until(const chrono::time_point<_Clock,
   return true;
 }
 
-#  endif   // !_LIBCPP_HAS_NO_THREADS
+#  endif // !_LIBCPP_HAS_NO_THREADS
 
 template <class _Mutex>
 class shared_lock {
diff --git a/libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.guard/mutex.pass.cpp b/libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.guard/mutex.pass.cpp
index a95c2ef0029de..ca902f16f81d7 100644
--- a/libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.guard/mutex.pass.cpp
+++ b/libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.guard/mutex.pass.cpp
@@ -22,16 +22,27 @@
 #include "test_macros.h"
 
 struct TestMutex {
-    bool locked = false;
-    TestMutex() = default;
-    ~TestMutex() { assert(!locked); }
+  bool locked = false;
+  TestMutex() = default;
+  ~TestMutex() { assert(!locked); }
 
-    void lock() { assert(!locked); locked = true; }
-    bool try_lock() { if (locked) return false; locked = true; return true; }
-    void unlock() { assert(locked); locked = false; }
+  void lock() {
+    assert(!locked);
+    locked = true;
+  }
+  bool try_lock() {
+    if (locked)
+      return false;
+    locked = true;
+    return true;
+  }
+  void unlock() {
+    assert(locked);
+    locked = false;
+  }
 
-    TestMutex(TestMutex const&) = delete;
-    TestMutex& operator=(TestMutex const&) = delete;
+  TestMutex(TestMutex const&)            = delete;
+  TestMutex& operator=(TestMutex const&) = delete;
 };
 
 int main(int, char**) {
@@ -44,7 +55,7 @@ int main(int, char**) {
 
 #if TEST_STD_VER >= 17
   std::lock_guard lg(m);
-  static_assert((std::is_same<decltype(lg), std::lock_guard<decltype(m)>>::value), "" );
+  static_assert((std::is_same<decltype(lg), std::lock_guard<decltype(m)>>::value), "");
 #endif
 
   return 0;
diff --git a/libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.shared/thread.lock.shared.cons/mutex.pass.cpp b/libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.shared/thread.lock.shared.cons/mutex.pass.cpp
index 863948763e2ef..c9d145addd63d 100644
--- a/libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.shared/thread.lock.shared.cons/mutex.pass.cpp
+++ b/libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.shared/thread.lock.shared.cons/mutex.pass.cpp
@@ -79,9 +79,9 @@ void test() {
 
 int main(int, char**) {
 #ifndef TEST_HAS_NO_THREADS
-#if TEST_STD_VER >= 17
+#  if TEST_STD_VER >= 17
   test<std::shared_mutex>();
-#endif
+#  endif
   test<std::shared_timed_mutex>();
 #endif
   test<TrackedMutex>();
diff --git a/libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.shared/thread.lock.shared.locking/lock.pass.cpp b/libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.shared/thread.lock.shared.locking/lock.pass.cpp
index 9097ac74d1272..3e0ef122ef990 100644
--- a/libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.shared/thread.lock.shared.locking/lock.pass.cpp
+++ b/libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.shared/thread.lock.shared.locking/lock.pass.cpp
@@ -99,9 +99,9 @@ void test() {
 
 int main(int, char**) {
 #ifndef TEST_HAS_NO_THREADS
-#if TEST_STD_VER >= 17
+#  if TEST_STD_VER >= 17
   test<std::shared_mutex>();
-#endif
+#  endif
   test<std::shared_timed_mutex>();
 #endif
   test<TrackedMutex>();
diff --git a/libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.shared/thread.lock.shared.locking/try_lock.pass.cpp b/libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.shared/thread.lock.shared.locking/try_lock.pass.cpp
index ccab79d4db016..d42b00414230c 100644
--- a/libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.shared/thread.lock.shared.locking/try_lock.pass.cpp
+++ b/libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.shared/thread.lock.shared.locking/try_lock.pass.cpp
@@ -104,9 +104,9 @@ void test() {
 
 int main(int, char**) {
 #ifndef TEST_HAS_NO_THREADS
-#if TEST_STD_VER >= 17
+#  if TEST_STD_VER >= 17
   test<std::shared_mutex>();
-#endif
+#  endif
   test<std::shared_timed_mutex>();
 #endif
   test<TrackedMutex>();

>From d717ad2d1f79c20e7fdd5101e6974b65f8ce5e84 Mon Sep 17 00:00:00 2001
From: Petr Hosek <phosek at google.com>
Date: Sat, 13 Jul 2024 21:30:37 -0700
Subject: [PATCH 5/5] Make defer_lock_t, try_to_lock_t and adopt_lock_t
 available also

---
 libcxx/include/__mutex/tag_types.h | 10 +++-------
 1 file changed, 3 insertions(+), 7 deletions(-)

diff --git a/libcxx/include/__mutex/tag_types.h b/libcxx/include/__mutex/tag_types.h
index 05ccb8b23a6f4..2b2dd58ee4e80 100644
--- a/libcxx/include/__mutex/tag_types.h
+++ b/libcxx/include/__mutex/tag_types.h
@@ -15,8 +15,6 @@
 #  pragma GCC system_header
 #endif
 
-#ifndef _LIBCPP_HAS_NO_THREADS
-
 _LIBCPP_BEGIN_NAMESPACE_STD
 
 struct _LIBCPP_EXPORTED_FROM_ABI defer_lock_t {
@@ -31,18 +29,16 @@ struct _LIBCPP_EXPORTED_FROM_ABI adopt_lock_t {
   explicit adopt_lock_t() = default;
 };
 
-#  if _LIBCPP_STD_VER >= 17
+#if _LIBCPP_STD_VER >= 17
 inline constexpr defer_lock_t defer_lock   = defer_lock_t();
 inline constexpr try_to_lock_t try_to_lock = try_to_lock_t();
 inline constexpr adopt_lock_t adopt_lock   = adopt_lock_t();
-#  elif !defined(_LIBCPP_CXX03_LANG)
+#elif !defined(_LIBCPP_CXX03_LANG)
 constexpr defer_lock_t defer_lock   = defer_lock_t();
 constexpr try_to_lock_t try_to_lock = try_to_lock_t();
 constexpr adopt_lock_t adopt_lock   = adopt_lock_t();
-#  endif
+#endif
 
 _LIBCPP_END_NAMESPACE_STD
 
-#endif // _LIBCPP_HAS_NO_THREADS
-
 #endif // _LIBCPP___MUTEX_TAG_TYPES_H



More information about the libcxx-commits mailing list