[libcxx-commits] [libcxx] [libc++] Refactor the predicate taking variant of `__cxx_atomic_wait` (PR #80596)

Jan Kokemüller via libcxx-commits libcxx-commits at lists.llvm.org
Sun Feb 18 04:16:48 PST 2024


================
@@ -178,6 +178,7 @@ _LIBCPP_EXPORTED_FROM_ABI void __cxx_atomic_notify_one(__cxx_atomic_contention_t
 _LIBCPP_EXPORTED_FROM_ABI void __cxx_atomic_notify_all(__cxx_atomic_contention_t const volatile* __location) {
   __libcpp_contention_notify(&__libcpp_contention_state(__location)->__contention_state, __location, false);
 }
+// This function is never used, but still exported for ABI compatibility.
----------------
jiixyj wrote:

Yes, exactly. This overload of `__libcpp_atomic_monitor` was used to get the "monitor" value in case the `ptrdiff_t` of the semaphore could be waited on directly. And this was the reason for the original ABA bug, where this could happen:

1. `__libcpp_atomic_monitor` returns "1" (i.e. the semaphore has the value 1)
2. another thread swoops in, takes the "1" down to "0"
3. "our" poll function returns "false", because there is nothing left to `try_acquire`
4. another thread releases the semaphore (increases the value from "0" to "1")
5. `atomic_wait` now waits on the "1", resulting in a deadlock.


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


More information about the libcxx-commits mailing list