[libcxx-commits] [PATCH] D98334: [libcxx] Fix hang in try_acquire with __atomic_semaphore_base
Arthur O'Dwyer via Phabricator via libcxx-commits
libcxx-commits at lists.llvm.org
Thu Nov 4 21:28:38 PDT 2021
Quuxplusone updated this revision to Diff 384957.
Quuxplusone added a comment.
@ldionne, now that we're using `__atomic_semaphore_base` unconditionally, I think this patch has become super important. I confirm that the new test really does hang forever on my Macbook, and that this change to `<semaphore>` fixes the hang.
I think we should land this ASAP in both `main` and 13.x (assuming you're still planning to cherry-pick D110110 <https://reviews.llvm.org/D110110>). Having D110110 <https://reviews.llvm.org/D110110> without this patch too, is a pretty bad state to be in.
Repository:
rG LLVM Github Monorepo
CHANGES SINCE LAST ACTION
https://reviews.llvm.org/D98334/new/
https://reviews.llvm.org/D98334
Files:
libcxx/include/semaphore
libcxx/test/std/thread/thread.semaphore/try_acquire.pass.cpp
Index: libcxx/test/std/thread/thread.semaphore/try_acquire.pass.cpp
===================================================================
--- libcxx/test/std/thread/thread.semaphore/try_acquire.pass.cpp
+++ libcxx/test/std/thread/thread.semaphore/try_acquire.pass.cpp
@@ -30,14 +30,17 @@
std::counting_semaphore<> s(1);
assert(s.try_acquire());
+ assert(!s.try_acquire());
s.release();
assert(s.try_acquire());
+ assert(!s.try_acquire());
s.release(2);
std::thread t = support::make_test_thread([&](){
assert(s.try_acquire());
});
t.join();
assert(s.try_acquire());
+ assert(!s.try_acquire());
return 0;
}
Index: libcxx/include/semaphore
===================================================================
--- libcxx/include/semaphore
+++ libcxx/include/semaphore
@@ -105,17 +105,22 @@
_LIBCPP_AVAILABILITY_SYNC _LIBCPP_INLINE_VISIBILITY
bool try_acquire_for(chrono::duration<Rep, Period> const& __rel_time)
{
- auto const __test_fn = [this]() -> bool {
- auto __old = __a.load(memory_order_acquire);
- while(1) {
- if (__old == 0)
- return false;
- if(__a.compare_exchange_strong(__old, __old - 1, memory_order_acquire, memory_order_relaxed))
- return true;
- }
- };
+ if (__rel_time == chrono::duration<Rep, Period>::zero())
+ return try_acquire();
+ auto const __test_fn = [this]() { return try_acquire(); };
return __libcpp_thread_poll_with_backoff(__test_fn, __libcpp_timed_backoff_policy(), __rel_time);
}
+ _LIBCPP_AVAILABILITY_SYNC _LIBCPP_INLINE_VISIBILITY
+ bool try_acquire()
+ {
+ auto __old = __a.load(memory_order_acquire);
+ while (true) {
+ if (__old == 0)
+ return false;
+ if (__a.compare_exchange_strong(__old, __old - 1, memory_order_acquire, memory_order_relaxed))
+ return true;
+ }
+ }
};
#define _LIBCPP_SEMAPHORE_MAX (numeric_limits<ptrdiff_t>::max())
@@ -156,14 +161,14 @@
_LIBCPP_AVAILABILITY_SYNC _LIBCPP_INLINE_VISIBILITY
bool try_acquire()
{
- return try_acquire_for(chrono::nanoseconds::zero());
+ return __semaphore.try_acquire();
}
template <class Clock, class Duration>
_LIBCPP_AVAILABILITY_SYNC _LIBCPP_INLINE_VISIBILITY
bool try_acquire_until(chrono::time_point<Clock, Duration> const& __abs_time)
{
auto const current = Clock::now();
- if(current >= __abs_time)
+ if (current >= __abs_time)
return try_acquire();
else
return try_acquire_for(__abs_time - current);
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D98334.384957.patch
Type: text/x-patch
Size: 2712 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/libcxx-commits/attachments/20211105/46bd5294/attachment.bin>
More information about the libcxx-commits
mailing list