[libcxx-commits] [PATCH] D131483: [libcxx] [test] Make some threading tests more robust
Martin Storsjö via Phabricator via libcxx-commits
libcxx-commits at lists.llvm.org
Thu Aug 11 05:36:50 PDT 2022
mstorsjo updated this revision to Diff 451819.
mstorsjo added a comment.
Rebased the patch on top of D131484 <https://reviews.llvm.org/D131484>, added the same descriptive comment on the new uses of the define too.
Repository:
rG LLVM Github Monorepo
CHANGES SINCE LAST ACTION
https://reviews.llvm.org/D131483/new/
https://reviews.llvm.org/D131483
Files:
libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.shared/thread.lock.shared.cons/mutex_try_to_lock.pass.cpp
libcxx/test/std/thread/thread.mutex/thread.mutex.requirements/thread.shared_mutex.requirements/thread.shared_mutex.class/try_lock_shared.pass.cpp
libcxx/test/std/thread/thread.mutex/thread.mutex.requirements/thread.sharedtimedmutex.requirements/thread.sharedtimedmutex.class/try_lock_shared.pass.cpp
Index: libcxx/test/std/thread/thread.mutex/thread.mutex.requirements/thread.sharedtimedmutex.requirements/thread.sharedtimedmutex.class/try_lock_shared.pass.cpp
===================================================================
--- libcxx/test/std/thread/thread.mutex/thread.mutex.requirements/thread.sharedtimedmutex.requirements/thread.sharedtimedmutex.class/try_lock_shared.pass.cpp
+++ libcxx/test/std/thread/thread.mutex/thread.mutex.requirements/thread.sharedtimedmutex.requirements/thread.sharedtimedmutex.class/try_lock_shared.pass.cpp
@@ -54,7 +54,7 @@
assert(!m.try_lock_shared());
assert(!m.try_lock_shared());
while(!m.try_lock_shared())
- ;
+ std::this_thread::yield();
time_point t1 = Clock::now();
m.unlock_shared();
ns d = t1 - t0 - ms(250);
Index: libcxx/test/std/thread/thread.mutex/thread.mutex.requirements/thread.shared_mutex.requirements/thread.shared_mutex.class/try_lock_shared.pass.cpp
===================================================================
--- libcxx/test/std/thread/thread.mutex/thread.mutex.requirements/thread.shared_mutex.requirements/thread.shared_mutex.class/try_lock_shared.pass.cpp
+++ libcxx/test/std/thread/thread.mutex/thread.mutex.requirements/thread.shared_mutex.requirements/thread.shared_mutex.class/try_lock_shared.pass.cpp
@@ -37,6 +37,16 @@
typedef std::chrono::milliseconds ms;
typedef std::chrono::nanoseconds ns;
+
+// Thread sanitizer causes more overhead and will sometimes cause this test
+// to fail. To prevent this we give Thread sanitizer more time to complete the
+// test.
+#if !defined(TEST_IS_SLOW)
+ms Tolerance = ms(200);
+#else
+ms Tolerance = ms(200 * 5);
+#endif
+
void f()
{
time_point t0 = Clock::now();
@@ -44,11 +54,11 @@
assert(!m.try_lock_shared());
assert(!m.try_lock_shared());
while(!m.try_lock_shared())
- ;
+ std::this_thread::yield();
time_point t1 = Clock::now();
m.unlock_shared();
ns d = t1 - t0 - ms(250);
- assert(d < ms(200)); // within 200ms
+ assert(d < Tolerance); // within tolerance
}
Index: libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.shared/thread.lock.shared.cons/mutex_try_to_lock.pass.cpp
===================================================================
--- libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.shared/thread.lock.shared.cons/mutex_try_to_lock.pass.cpp
+++ libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.shared/thread.lock.shared.cons/mutex_try_to_lock.pass.cpp
@@ -36,6 +36,16 @@
typedef std::chrono::milliseconds ms;
typedef std::chrono::nanoseconds ns;
+
+// Thread sanitizer causes more overhead and will sometimes cause this test
+// to fail. To prevent this we give Thread sanitizer more time to complete the
+// test.
+#if !defined(TEST_IS_SLOW)
+ms Tolerance = ms(200);
+#else
+ms Tolerance = ms(200 * 5);
+#endif
+
void f()
{
time_point t0 = Clock::now();
@@ -56,10 +66,11 @@
std::shared_lock<std::shared_timed_mutex> lk(m, std::try_to_lock);
if (lk.owns_lock())
break;
+ std::this_thread::yield();
}
time_point t1 = Clock::now();
ns d = t1 - t0 - ms(250);
- assert(d < ms(200)); // within 200ms
+ assert(d < Tolerance); // within tolerance
}
int main(int, char**)
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D131483.451819.patch
Type: text/x-patch
Size: 3322 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/libcxx-commits/attachments/20220811/95886178/attachment.bin>
More information about the libcxx-commits
mailing list