[libcxx-commits] [libcxx] [libc++] Refactor the std::unique_lock tests (PR #102151)
via libcxx-commits
libcxx-commits at lists.llvm.org
Thu Aug 15 08:13:44 PDT 2024
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-libcxx
Author: Nikolas Klauser (philnik777)
<details>
<summary>Changes</summary>
This makes some of the tests not flaky anymore, updates some tests to also work in C++03 and modernizes them in general.
---
Patch is 46.17 KiB, truncated to 20.00 KiB below, full version: https://github.com/llvm/llvm-project/pull/102151.diff
26 Files Affected:
- (renamed) libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.unique/implicit_ctad.compile.pass.cpp (+3-11)
- (modified) libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.unique/thread.lock.unique.cons/copy_assign.compile.pass.cpp (+2-2)
- (modified) libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.unique/thread.lock.unique.cons/copy_ctor.compile.pass.cpp (+2-2)
- (modified) libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.unique/thread.lock.unique.cons/default.pass.cpp (+2-3)
- (modified) libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.unique/thread.lock.unique.cons/move_assign.pass.cpp (+13-27)
- (modified) libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.unique/thread.lock.unique.cons/move_ctor.pass.cpp (+10-25)
- (modified) libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.unique/thread.lock.unique.cons/mutex.pass.cpp (+9-35)
- (modified) libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.unique/thread.lock.unique.cons/mutex_adopt_lock.pass.cpp (+9-21)
- (modified) libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.unique/thread.lock.unique.cons/mutex_defer_lock.pass.cpp (+7-19)
- (modified) libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.unique/thread.lock.unique.cons/mutex_duration.pass.cpp (+23-56)
- (modified) libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.unique/thread.lock.unique.cons/mutex_time_point.pass.cpp (+24-56)
- (modified) libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.unique/thread.lock.unique.cons/mutex_try_to_lock.pass.cpp (+21-51)
- (modified) libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.unique/thread.lock.unique.locking/lock.pass.cpp (+27-54)
- (modified) libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.unique/thread.lock.unique.locking/try_lock.pass.cpp (+24-17)
- (modified) libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.unique/thread.lock.unique.locking/try_lock_for.pass.cpp (+27-16)
- (modified) libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.unique/thread.lock.unique.locking/try_lock_until.pass.cpp (+23-16)
- (modified) libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.unique/thread.lock.unique.locking/unlock.pass.cpp (+17-9)
- (modified) libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.unique/thread.lock.unique.mod/member_swap.pass.cpp (+12-11)
- (modified) libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.unique/thread.lock.unique.mod/nonmember_swap.pass.cpp (+12-11)
- (modified) libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.unique/thread.lock.unique.mod/release.pass.cpp (+15-17)
- (modified) libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.unique/thread.lock.unique.obs/mutex.pass.cpp (+9-10)
- (modified) libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.unique/thread.lock.unique.obs/op_bool.pass.cpp (+11-11)
- (modified) libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.unique/thread.lock.unique.obs/owns_lock.pass.cpp (+8-9)
- (modified) libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.unique/types.compile.pass.cpp (+2-3)
- (removed) libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.unique/types.h (-88)
- (added) libcxx/test/support/checking_mutex.h (+80)
``````````diff
diff --git a/libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.unique/implicit_ctad.pass.cpp b/libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.unique/implicit_ctad.compile.pass.cpp
similarity index 71%
rename from libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.unique/implicit_ctad.pass.cpp
rename to libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.unique/implicit_ctad.compile.pass.cpp
index 8c7ca4279eead0..cc94c5704327fe 100644
--- a/libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.unique/implicit_ctad.pass.cpp
+++ b/libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.unique/implicit_ctad.compile.pass.cpp
@@ -16,15 +16,7 @@
#include <mutex>
-#include "test_macros.h"
-#include "types.h"
+#include "checking_mutex.h"
-int main(int, char**) {
- MyMutex mutex;
- {
- std::unique_lock lock(mutex);
- ASSERT_SAME_TYPE(decltype(lock), std::unique_lock<MyMutex>);
- }
-
- return 0;
-}
+checking_mutex mux;
+static_assert(std::is_same_v<std::unique_lock<checking_mutex>, decltype(std::unique_lock{mux})>);
diff --git a/libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.unique/thread.lock.unique.cons/copy_assign.compile.pass.cpp b/libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.unique/thread.lock.unique.cons/copy_assign.compile.pass.cpp
index 9ab8369637cdc5..c0cb7d4ddd27a6 100644
--- a/libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.unique/thread.lock.unique.cons/copy_assign.compile.pass.cpp
+++ b/libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.unique/thread.lock.unique.cons/copy_assign.compile.pass.cpp
@@ -14,6 +14,6 @@
#include <mutex>
-#include "../types.h"
+#include "checking_mutex.h"
-static_assert(!std::is_copy_assignable<std::lock_guard<MyMutex> >::value, "");
+static_assert(!std::is_copy_assignable<std::lock_guard<checking_mutex> >::value, "");
diff --git a/libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.unique/thread.lock.unique.cons/copy_ctor.compile.pass.cpp b/libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.unique/thread.lock.unique.cons/copy_ctor.compile.pass.cpp
index e846061f5fbd08..2846b24125e784 100644
--- a/libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.unique/thread.lock.unique.cons/copy_ctor.compile.pass.cpp
+++ b/libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.unique/thread.lock.unique.cons/copy_ctor.compile.pass.cpp
@@ -14,6 +14,6 @@
#include <mutex>
-#include "../types.h"
+#include "checking_mutex.h"
-static_assert(!std::is_copy_constructible<std::lock_guard<MyMutex> >::value, "");
+static_assert(!std::is_copy_constructible<std::lock_guard<checking_mutex> >::value, "");
diff --git a/libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.unique/thread.lock.unique.cons/default.pass.cpp b/libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.unique/thread.lock.unique.cons/default.pass.cpp
index 6fc4f7f23ced3a..f203acf5f07ddc 100644
--- a/libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.unique/thread.lock.unique.cons/default.pass.cpp
+++ b/libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.unique/thread.lock.unique.cons/default.pass.cpp
@@ -15,11 +15,10 @@
#include <cassert>
#include <mutex>
-#include "test_macros.h"
-#include "../types.h"
+#include "checking_mutex.h"
int main(int, char**) {
- std::unique_lock<MyMutex> ul;
+ std::unique_lock<checking_mutex> ul;
assert(!ul.owns_lock());
assert(ul.mutex() == nullptr);
diff --git a/libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.unique/thread.lock.unique.cons/move_assign.pass.cpp b/libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.unique/thread.lock.unique.cons/move_assign.pass.cpp
index 9563fdebd3e060..a243282b647002 100644
--- a/libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.unique/thread.lock.unique.cons/move_assign.pass.cpp
+++ b/libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.unique/thread.lock.unique.cons/move_assign.pass.cpp
@@ -13,37 +13,23 @@
// unique_lock& operator=(unique_lock&& u);
#include <cassert>
+#include <memory>
#include <mutex>
-#include "nasty_containers.h"
-#include "../types.h"
-#include "test_macros.h"
+#include "checking_mutex.h"
int main(int, char**) {
- {
- typedef MyMutex M;
- M m0;
- M m1;
- std::unique_lock<M> lk0(m0);
- std::unique_lock<M> lk1(m1);
- lk1 = std::move(lk0);
- assert(lk1.mutex() == std::addressof(m0));
- assert(lk1.owns_lock() == true);
- assert(lk0.mutex() == nullptr);
- assert(lk0.owns_lock() == false);
- }
- {
- typedef nasty_mutex M;
- M m0;
- M m1;
- std::unique_lock<M> lk0(m0);
- std::unique_lock<M> lk1(m1);
- lk1 = std::move(lk0);
- assert(lk1.mutex() == std::addressof(m0));
- assert(lk1.owns_lock() == true);
- assert(lk0.mutex() == nullptr);
- assert(lk0.owns_lock() == false);
- }
+ checking_mutex m0;
+ checking_mutex m1;
+ std::unique_lock<checking_mutex> lk0(m0);
+ std::unique_lock<checking_mutex> lk1(m1);
+
+ lk1 = std::move(lk0);
+
+ assert(lk1.mutex() == std::addressof(m0));
+ assert(lk1.owns_lock());
+ assert(lk0.mutex() == nullptr);
+ assert(!lk0.owns_lock());
return 0;
}
diff --git a/libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.unique/thread.lock.unique.cons/move_ctor.pass.cpp b/libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.unique/thread.lock.unique.cons/move_ctor.pass.cpp
index 08f6fc8410e25e..1fcb82dc3e55e3 100644
--- a/libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.unique/thread.lock.unique.cons/move_ctor.pass.cpp
+++ b/libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.unique/thread.lock.unique.cons/move_ctor.pass.cpp
@@ -5,8 +5,6 @@
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
-//
-// UNSUPPORTED: c++03
// <mutex>
@@ -15,33 +13,20 @@
// unique_lock(unique_lock&& u);
#include <cassert>
+#include <memory>
#include <mutex>
-#include "nasty_containers.h"
-#include "../types.h"
-#include "test_macros.h"
+#include "checking_mutex.h"
int main(int, char**) {
- {
- typedef MyMutex M;
- M m;
- std::unique_lock<M> lk0(m);
- std::unique_lock<M> lk = std::move(lk0);
- assert(lk.mutex() == std::addressof(m));
- assert(lk.owns_lock() == true);
- assert(lk0.mutex() == nullptr);
- assert(lk0.owns_lock() == false);
- }
- {
- typedef nasty_mutex M;
- M m;
- std::unique_lock<M> lk0(m);
- std::unique_lock<M> lk = std::move(lk0);
- assert(lk.mutex() == std::addressof(m));
- assert(lk.owns_lock() == true);
- assert(lk0.mutex() == nullptr);
- assert(lk0.owns_lock() == false);
- }
+ checking_mutex m;
+ std::unique_lock<checking_mutex> lk0(m);
+ std::unique_lock<checking_mutex> lk = std::move(lk0);
+
+ assert(lk.mutex() == std::addressof(m));
+ assert(lk.owns_lock());
+ assert(lk0.mutex() == nullptr);
+ assert(!lk0.owns_lock());
return 0;
}
diff --git a/libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.unique/thread.lock.unique.cons/mutex.pass.cpp b/libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.unique/thread.lock.unique.cons/mutex.pass.cpp
index 2be25748e903b0..31f15deec0cfaf 100644
--- a/libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.unique/thread.lock.unique.cons/mutex.pass.cpp
+++ b/libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.unique/thread.lock.unique.cons/mutex.pass.cpp
@@ -5,9 +5,6 @@
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
-//
-// UNSUPPORTED: no-threads
-// ALLOW_RETRIES: 2
// <mutex>
@@ -19,45 +16,22 @@
// -> unique_lock<_Mutex>; // C++17
#include <cassert>
-#include <chrono>
-#include <cstdlib>
#include <mutex>
-#include <thread>
-#include "make_test_thread.h"
+#include "checking_mutex.h"
#include "test_macros.h"
-std::mutex m;
-
-typedef std::chrono::system_clock Clock;
-typedef Clock::time_point time_point;
-typedef Clock::duration duration;
-typedef std::chrono::milliseconds ms;
-typedef std::chrono::nanoseconds ns;
-
-void f()
-{
- time_point t0 = Clock::now();
- time_point t1;
- {
- std::unique_lock<std::mutex> ul(m);
- t1 = Clock::now();
- }
- ns d = t1 - t0 - ms(250);
- assert(d < ms(50)); // within 50ms
-}
+int main(int, char**) {
+ checking_mutex mux;
-int main(int, char**)
-{
- m.lock();
- std::thread t = support::make_test_thread(f);
- std::this_thread::sleep_for(ms(250));
- m.unlock();
- t.join();
+ {
+ std::unique_lock<checking_mutex> lock(mux);
+ assert(mux.current_state == checking_mutex::locked_via_lock);
+ }
+ assert(mux.current_state == checking_mutex::unlocked);
#if TEST_STD_VER >= 17
- std::unique_lock ul(m);
- static_assert((std::is_same<decltype(ul), std::unique_lock<decltype(m)>>::value), "" );
+ static_assert(std::is_same_v<std::unique_lock<checking_mutex>, decltype(std::unique_lock{mux})>, "");
#endif
return 0;
diff --git a/libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.unique/thread.lock.unique.cons/mutex_adopt_lock.pass.cpp b/libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.unique/thread.lock.unique.cons/mutex_adopt_lock.pass.cpp
index 28cc43853180e6..14db741fa4adc3 100644
--- a/libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.unique/thread.lock.unique.cons/mutex_adopt_lock.pass.cpp
+++ b/libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.unique/thread.lock.unique.cons/mutex_adopt_lock.pass.cpp
@@ -5,8 +5,6 @@
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
-//
-// UNSUPPORTED: c++03
// <mutex>
@@ -15,29 +13,19 @@
// unique_lock(mutex_type& m, adopt_lock_t);
#include <cassert>
+#include <memory>
#include <mutex>
-#include "nasty_containers.h"
-#include "../types.h"
-#include "test_macros.h"
+#include "checking_mutex.h"
int main(int, char**) {
- {
- typedef MyMutex M;
- M m;
- m.lock();
- std::unique_lock<M> lk(m, std::adopt_lock);
- assert(lk.mutex() == std::addressof(m));
- assert(lk.owns_lock() == true);
- }
- {
- typedef nasty_mutex M;
- M m;
- m.lock();
- std::unique_lock<M> lk(m, std::adopt_lock);
- assert(lk.mutex() == std::addressof(m));
- assert(lk.owns_lock() == true);
- }
+ checking_mutex m;
+ m.lock();
+ m.last_try = checking_mutex::none;
+ std::unique_lock<checking_mutex> lk(m, std::adopt_lock_t());
+ assert(m.last_try == checking_mutex::none);
+ assert(lk.mutex() == std::addressof(m));
+ assert(lk.owns_lock());
return 0;
}
diff --git a/libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.unique/thread.lock.unique.cons/mutex_defer_lock.pass.cpp b/libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.unique/thread.lock.unique.cons/mutex_defer_lock.pass.cpp
index 96a9afbc9438c4..e6a3ffc884866c 100644
--- a/libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.unique/thread.lock.unique.cons/mutex_defer_lock.pass.cpp
+++ b/libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.unique/thread.lock.unique.cons/mutex_defer_lock.pass.cpp
@@ -5,8 +5,6 @@
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
-//
-// UNSUPPORTED: c++03
// <mutex>
@@ -15,27 +13,17 @@
// unique_lock(mutex_type& m, defer_lock_t);
#include <cassert>
+#include <memory>
#include <mutex>
-#include "nasty_containers.h"
-#include "../types.h"
-#include "test_macros.h"
+#include "checking_mutex.h"
int main(int, char**) {
- {
- typedef MyMutex M;
- M m;
- std::unique_lock<M> lk(m, std::defer_lock);
- assert(lk.mutex() == std::addressof(m));
- assert(lk.owns_lock() == false);
- }
- {
- typedef nasty_mutex M;
- M m;
- std::unique_lock<M> lk(m, std::defer_lock);
- assert(lk.mutex() == std::addressof(m));
- assert(lk.owns_lock() == false);
- }
+ checking_mutex m;
+ std::unique_lock<checking_mutex> lk(m, std::defer_lock_t());
+ assert(m.last_try == checking_mutex::none);
+ assert(lk.mutex() == std::addressof(m));
+ assert(lk.owns_lock() == false);
return 0;
}
diff --git a/libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.unique/thread.lock.unique.cons/mutex_duration.pass.cpp b/libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.unique/thread.lock.unique.cons/mutex_duration.pass.cpp
index 4bfabab919f177..624b99623d6be7 100644
--- a/libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.unique/thread.lock.unique.cons/mutex_duration.pass.cpp
+++ b/libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.unique/thread.lock.unique.cons/mutex_duration.pass.cpp
@@ -5,69 +5,36 @@
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
-//
-// UNSUPPORTED: no-threads
-// ALLOW_RETRIES: 2
// <mutex>
-// class timed_mutex;
-
// template <class Rep, class Period>
-// unique_lock(mutex_type& m, const chrono::duration<Rep, Period>& rel_time);
+// unique_lock::unique_lock(mutex_type& m, const chrono::duration<Rep, Period>& rel_time);
-#include <mutex>
-#include <thread>
-#include <cstdlib>
#include <cassert>
+#include <chrono>
+#include <mutex>
-#include "make_test_thread.h"
-#include "test_macros.h"
-
-std::timed_mutex m;
-
-typedef std::chrono::steady_clock Clock;
-typedef Clock::time_point time_point;
-typedef Clock::duration duration;
-typedef std::chrono::milliseconds ms;
-typedef std::chrono::nanoseconds ns;
-
-void f1()
-{
- time_point t0 = Clock::now();
- std::unique_lock<std::timed_mutex> lk(m, ms(300));
- assert(lk.owns_lock() == true);
- time_point t1 = Clock::now();
- ns d = t1 - t0 - ms(250);
- assert(d < ms(50)); // within 50ms
-}
-
-void f2()
-{
- time_point t0 = Clock::now();
- std::unique_lock<std::timed_mutex> lk(m, ms(250));
- assert(lk.owns_lock() == false);
- time_point t1 = Clock::now();
- ns d = t1 - t0 - ms(250);
- assert(d < ms(50)); // within 50ms
-}
-
-int main(int, char**)
-{
- {
- m.lock();
- std::thread t = support::make_test_thread(f1);
- std::this_thread::sleep_for(ms(250));
- m.unlock();
- t.join();
- }
- {
- m.lock();
- std::thread t = support::make_test_thread(f2);
- std::this_thread::sleep_for(ms(300));
- m.unlock();
- t.join();
- }
+#include "checking_mutex.h"
+
+int main(int, char**) {
+ checking_mutex mux;
+ { // check successful lock
+ mux.reject = false;
+ std::unique_lock<checking_mutex> lock(mux, std::chrono::seconds());
+ assert(mux.current_state == checking_mutex::locked_via_try_lock_for);
+ assert(lock.owns_lock());
+ }
+ assert(mux.current_state == checking_mutex::unlocked);
+
+ { // check unsuccessful lock
+ mux.reject = true;
+ std::unique_lock<checking_mutex> lock(mux, std::chrono::seconds());
+ assert(mux.current_state == checking_mutex::unlocked);
+ assert(mux.last_try == checking_mutex::locked_via_try_lock_for);
+ assert(!lock.owns_lock());
+ }
+ assert(mux.current_state == checking_mutex::unlocked);
return 0;
}
diff --git a/libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.unique/thread.lock.unique.cons/mutex_time_point.pass.cpp b/libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.unique/thread.lock.unique.cons/mutex_time_point.pass.cpp
index b85bbace3233c1..93d322050476f6 100644
--- a/libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.unique/thread.lock.unique.cons/mutex_time_point.pass.cpp
+++ b/libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.unique/thread.lock.unique.cons/mutex_time_point.pass.cpp
@@ -5,69 +5,37 @@
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
-//
-// UNSUPPORTED: no-threads
-// ALLOW_RETRIES: 2
// <mutex>
-// class timed_mutex;
-
// template <class Clock, class Duration>
-// unique_lock(mutex_type& m, const chrono::time_point<Clock, Duration>& abs_time);
+// unique_lock::unique_lock(mutex_type& m, const chrono::time_point<Clock, Duration>& abs_time);
-#include <mutex>
-#include <thread>
-#include <cstdlib>
#include <cassert>
+#include <chrono>
+#include <mutex>
-#include "make_test_thread.h"
-#include "test_macros.h"
-
-std::timed_mutex m;
-
-typedef std::chrono::steady_clock Clock;
-typedef Clock::time_point time_point;
-typedef Clock::duration duration;
-typedef std::chrono::milliseconds ms;
-typedef std::chrono::nanoseconds ns;
-
-void f1()
-{
- time_point t0 = Clock::now();
- std::unique_lock<std::timed_mutex> lk(m, Clock::now() + ms(300));
- assert(lk.owns_lock() == true);
- time_point t1 = Clock::now();
- ns d = t1 - t0 - ms(250);
- assert(d < ns(50000000)); // within 50ms
-}
-
-void f2()
-{
- time_point t0 = Clock::now();
- std::unique_lock<std::timed_mutex> lk(m, Clock::now() + ms(250));
- assert(lk.owns_lock() == false);
- time_point t1 = Clock::now();
- ns d = t1 - t0 - ms(250);
- assert(d < ms(50)); // within 50ms
-}
-
-int main(int, char**)
-{
- {
- m.lock();
- std::thread t = support::make_test_thread(f1);
- std::this_thread::sleep_for(ms(250));
- m.unlock();
- t.join();
- }
- {
- m.lock();
- std::thread t = support::make_test_thread(f2);
- std::this_thread::sleep_for(ms(300));
- m.unlock();
- t.join();
- }
+#include "checking_mutex.h"
+
+int main(int, char**) {
+ checking_mutex mux;
+
+ { // check successful lock
+ mux.reject = false;
+ std::unique_lock<checking_mutex> lock(mux, std::chrono::time_point<std::chrono::system_clock>());
+ assert(mux.current_state == checking_mutex::locked_via_try_lock_until);
+ assert(lock.owns_lock());
+ }
+ assert(mux.current_state == checking_mutex::unlocked);
+
+ { // check unsuccessful lock
+ mux.reject = true;
+ std::unique_lock<checking_mutex> lock(mux, std::chrono::time_point<std::chrono::system_clock>());
+ assert(mux.current_state == checking_mutex::unlocked);
+ assert(mux.last_try == checking_mutex::locked_via_try_lock_until);
+ assert(!lock.owns_lock());
+ }
+ assert(mux.current_state == checking_mutex::unlocked);
return 0;
}
diff --git a/libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.unique/thread.lock.unique.cons/mutex_try_to_lock.pass.cpp b/libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.unique/thread.lock.unique.cons/mutex_try_to_lock.pass.cpp
index 992d383dfa780d..e7af0fc34e7509 100644
--- a/libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.unique/thread.lock.unique.cons/mutex_try_to_lock.pass.cpp
+++ b/libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.unique/thread.lock.unique.cons/mutex_try_to_lock.pass.cpp
@@ -6,10 +6,6 @@
//
//===----------------------------------------------------------------------===//
-// UNSUPPORTED: no-threads
-// UNSUPPORTED: c++03
-// ALLOW_RETRIES: 2
-
// <mutex>
// template <class Mutex> class unique_lock;
@@ -17,55 +13,29 @@
// unique_lock(mutex_type& m, try_to_lock_t);
#include <cassert>
-#include <chrono>
-#include <cstdlib>
#include <mutex>
-#include <thread>
-
-#include "make_test_thread.h"
-#include "test_macros.h"
-
-std::mutex m;
-
-typedef std::chrono::system_clock Clock;
-typedef Clock::time_point time_point;
-typedef Clock::duration duration;
-typedef std::chrono::milliseconds ms;
-typedef std::chrono::nanoseconds ns;
-
-void f()
-{
- time_point t0 = Clock::now();
- {
- std::unique_lock<std::mutex> lk(m, std::try_to_lock);
- assert(lk.owns_lock() == false);
- }
- {
- std::unique_lock<std::mutex> lk(m, std::try_to_lock);
- assert(lk.owns_lock() == false);
- }
- {
- std::unique_lock<std::mutex> lk(m, std::try_to_lock);
- assert(lk.owns_lock() == false);
- }
- while (true)
- {
- std::unique_lock<std::mutex> lk(m, std::try_to_lock);
- if (lk.owns_lock())
- break;
- }
- time_point t1 = Clock::now();
- ns d = t1 - t0 - ms(250);
- assert(d < ms(200)); // within 200ms
-}
-...
[truncated]
``````````
</details>
https://github.com/llvm/llvm-project/pull/102151
More information about the libcxx-commits
mailing list