C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Abstraction for load-linked/store-conditional

From: Thiago Macieira <thiago_at_[hidden]>
Date: Mon, 22 Jul 2024 09:03:55 -0700
On Sunday 21 July 2024 15:23:58 GMT-7 Joseph Schuchart via Std-Proposals
wrote:
> Here is my approach: Since LL/SC is a load and a (conditional) store that
> work in tandem with potentially arbitrary code in between (vs a single CAS
> instruction or well-defined operations if std::atomic is implemented using
> LL/SC) they should be encapsulated into one abstraction, like:

"Potentially arbitrary code" is going to be a problem, as others have
described.

Instead, I suggest restricting to the sorts of operations that are going to be
most commonly used. I've been calling this "atomic_mutate" and have a working
implementation for x86 (using CAS) at
 https://codereview.qt-project.org/c/qt/qtbase/+/489704
but haven't had time to write a paper on (don't expect me to in time for C+
+26).

The idea is that an atomic mutation is to atomically
 1) load the current value
 2) perform a check against its current value and an one extra value
 3) if true, mutate it with another operation and a second extra value and
 4) store it

This was inspired by the Linux futex(2) ATOMIC_OP, which can be used with sub-
commands like FUTEX_WAKE_OP. The first use we will see of it is inside
std::shared_ptr, the "increment by 1 if not zero" that happens when locking a
std::weak_ptr back to shared_ptr.

For LL/SC architectures, the implementation could allow any combination of
comparison and mutation operations, but it requires compiler help for the
reasons others have reported.

For CAS architectures, all combinations are also possible and can be
implemented in library code, but almost always require looping. On some
architectures, there may be dedicated instructions for some operations, like
x86's CMPccXADD (compare-conditional-fetch-add).

What still needs to be discussed:
- what does this return? success or old value or both?
- should there be a weak and strong versions? What is weak and strong for CAS
implementatoins?
- what are the roster of comparisons and mutation operations?
- what are the platform limitations outside of x86?

-- 
Thiago Macieira - thiago (AT) macieira.info - thiago (AT) kde.org
  Principal Engineer - Intel DCAI Platform & System Engineering

Received on 2024-07-22 16:03:59