On 2015-03-24 14:30, Thiago Macieira wrote:
> On Tuesday 24 March 2015 09:56:59
je...@preshing.com wrote:
>> * Because of x86. These particular operations can be implemented on x86 as
>> a single instruction using x86's LOCK prefix. A compare_exchange_weak loop
>> for these operations would be less efficient on x86. (On Power and ARM,
>> they all get converted to compare_exchange_weak anyway.)
>> * To make code more concise. I can see this argument for fetch_add/sub,
>> since those operations seem to appear in a lot of lock-free algorithms. The
>> argument for and/or/xor is less convincing.
>>
>> I can't find the exact reasoning anywhere on
open-std.org. Have I
>> identified the actual reasons?
>>
>> Are there any other reasons I'm not aware of?
>>
>> Is there any other architecture besides x86/64 that benefits directly from
>> this library design?
>
> IA-64. Any other non-LL/SC architecture too, except that I don't know of any
> besides the two IAs.
Not that anyone uses it any more :-), but I want to say SPARC has some
low level stuff of this nature besides just CAS. I would imagine that
architectures that have CAS are likely to have other similar operations,
especially as increment and decrement are very common operations to do
to an atomic (e.g. anyone who has designed an architecture to
specifically have very fast atomic reference counting).
> In any case, reason #1 is enough reason to have those functions, since we're
> talking about very, very low-level classes here.
I'll echo that; if you're doing stuff where you need *atomics*, you
probably really, really want the fastest possible atomics, and that
means ADD/INC/DEC, not just CAS, especially as the former are guaranteed
to execute in constant time, while a CAS loop is theoretically not
guaranteed to *ever* complete.
(I'm actually a little surprised, come to think of it, that there are no
INC/DEC wrappers... but maybe no interesting architecture has those that
doesn't also have at least ADD[/SUB].)
--
Matthew