Rocksolid Light

Welcome to novaBBS (click a section below)

mail  files  register  newsreader  groups  login

Message-ID:  

"Atomic batteries to power, turbines to speed." -- Robin, The Boy Wonder


devel / comp.std.c / Re: atomic_compare_exchange atomicity of write to *expected

SubjectAuthor
* atomic_compare_exchange atomicity of write to *expectedPhilipp Klaus Krause
+* Re: atomic_compare_exchange atomicity of write to *expectedTijl Coosemans
|`* Re: atomic_compare_exchange atomicity of write to *expectedFlorian Weimer
| `- Re: atomic_compare_exchange atomicity of write to *expectedTim Rentsch
`- Re: atomic_compare_exchange atomicity of write to *expectedTim Rentsch

1
atomic_compare_exchange atomicity of write to *expected

<sjhadm$4uu$1@solani.org>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=354&group=comp.std.c#354

  copy link   Newsgroups: comp.std.c
Path: rocksolid2!i2pn.org!weretis.net!feeder8.news.weretis.net!reader5.news.weretis.net!news.solani.org!.POSTED!not-for-mail
From: pkk...@spth.de (Philipp Klaus Krause)
Newsgroups: comp.std.c
Subject: atomic_compare_exchange atomicity of write to *expected
Date: Tue, 5 Oct 2021 12:46:13 +0200
Message-ID: <sjhadm$4uu$1@solani.org>
Mime-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 7bit
Injection-Date: Tue, 5 Oct 2021 10:46:14 -0000 (UTC)
Injection-Info: solani.org;
logging-data="5086"; mail-complaints-to="abuse@news.solani.org"
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101
Thunderbird/78.14.0
Cancel-Lock: sha1:ki1DxW4H/vv1rPdmC5QFiJ2meho=
X-Mozilla-News-Host: snews://news.solani.org:563
Content-Language: en-US
X-User-ID: eJwNyMkRwDAIBLCWTPaAdhwY+i8h0VOCw520TK02ioz6A89rgEPYs2GeVVKgs3uqzva96A/0vBBE
 by: Philipp Klaus Krause - Tue, 5 Oct 2021 10:46 UTC

For the atomic_compare-exchange family, e.g.

_Bool atomic_compare_exchange_strong(volatile A *object, C *expected, C
desired);

the description states "Atomically, compares the contents of the memory
pointed to by object for equality with that pointed to by expected, and
if true, replaces the contents of the memory pointed to by object with
desired, and if false, updates the contents of the memory pointed to by
expected with that pointed to by object."

This reads to me as if the write to *expected is also part of the atomic
operation. This doesn't map well to compare-and-swap hardware
instructions, where the content of *object would be written to a
register, and later stored into *expected by a separate instruction.

Of course, this difference only matters if it is observable. I think it
is observable if just after the compare-and swap, a second thread writes
*object, while the first thread has already done the comparison, but not
yet updated *expected.
Then e.g. a thread thread could read the new value from *object, but the
old one from *expected, which is not possible if
atomic_compare_exchange_strong is fully atomic (i.e. also wrt. *expected).

Is my understanding here correct (I'm not an expert on atomics)?

Re: atomic_compare_exchange atomicity of write to *expected

<20211008132058.19c078b9@coosemans.org>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=360&group=comp.std.c#360

  copy link   Newsgroups: comp.std.c
Path: rocksolid2!i2pn.org!aioe.org!0k2hYNhnUnCLQJOSXWHu5A.user.46.165.242.75.POSTED!not-for-mail
From: tij...@coosemans.org (Tijl Coosemans)
Newsgroups: comp.std.c
Subject: Re: atomic_compare_exchange atomicity of write to *expected
Date: Fri, 8 Oct 2021 13:20:58 +0200
Organization: Aioe.org NNTP Server
Message-ID: <20211008132058.19c078b9@coosemans.org>
References: <sjhadm$4uu$1@solani.org>
Mime-Version: 1.0
Content-Type: text/plain; charset=US-ASCII
Content-Transfer-Encoding: 7bit
Injection-Info: gioia.aioe.org; logging-data="56298"; posting-host="0k2hYNhnUnCLQJOSXWHu5A.user.gioia.aioe.org"; mail-complaints-to="abuse@aioe.org";
X-Notice: Filtered by postfilter v. 0.9.2
 by: Tijl Coosemans - Fri, 8 Oct 2021 11:20 UTC

On Tue, 5 Oct 2021 12:46:13 +0200 Philipp Klaus Krause <pkk@spth.de>
wrote:
> For the atomic_compare-exchange family, e.g.
>
> _Bool atomic_compare_exchange_strong(volatile A *object, C *expected, C
> desired);
>
> the description states "Atomically, compares the contents of the memory
> pointed to by object for equality with that pointed to by expected, and
> if true, replaces the contents of the memory pointed to by object with
> desired, and if false, updates the contents of the memory pointed to by
> expected with that pointed to by object."
>
> This reads to me as if the write to *expected is also part of the atomic
> operation. This doesn't map well to compare-and-swap hardware
> instructions, where the content of *object would be written to a
> register, and later stored into *expected by a separate instruction.
>
> Of course, this difference only matters if it is observable. I think it
> is observable if just after the compare-and swap, a second thread writes
> *object, while the first thread has already done the comparison, but not
> yet updated *expected.
> Then e.g. a thread thread could read the new value from *object, but the
> old one from *expected, which is not possible if
> atomic_compare_exchange_strong is fully atomic (i.e. also wrt. *expected).
>
> Is my understanding here correct (I'm not an expert on atomics)?

*expected has type C, so it's not atomic, but even if it was, the
compare-and-swap, reading of *object, and reading of *expected are three
different operations and anything can happen in between them.

Re: atomic_compare_exchange atomicity of write to *expected

<86o87ypl8y.fsf@linuxsc.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=364&group=comp.std.c#364

  copy link   Newsgroups: comp.std.c
Path: rocksolid2!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: tr.17...@z991.linuxsc.com (Tim Rentsch)
Newsgroups: comp.std.c
Subject: Re: atomic_compare_exchange atomicity of write to *expected
Date: Sat, 09 Oct 2021 03:04:29 -0700
Organization: A noiseless patient Spider
Lines: 70
Message-ID: <86o87ypl8y.fsf@linuxsc.com>
References: <sjhadm$4uu$1@solani.org>
Mime-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Injection-Info: reader02.eternal-september.org; posting-host="4b78c8c73e68d6ec569569f083685d5c";
logging-data="28562"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18rYg8ZtDYebddpySotEJTvwpXXeJ6ozI0="
User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.4 (gnu/linux)
Cancel-Lock: sha1:1g7oW181DaCTDsOAoJnePJulnZ0=
sha1:98FLeF0zgxLHBMJtThDuceb3edw=
 by: Tim Rentsch - Sat, 9 Oct 2021 10:04 UTC

Philipp Klaus Krause <pkk@spth.de> writes:

> For the atomic_compare-exchange family, e.g.
>
> _Bool atomic_compare_exchange_strong(volatile A *object,
> C *expected, C desired);
>
> the description states "Atomically, compares the contents of the
> memory pointed to by object for equality with that pointed to by
> expected, and if true, replaces the contents of the memory pointed
> to by object with desired, and if false, updates the contents of the
> memory pointed to by expected with that pointed to by object."
>
> This reads to me as if the write to *expected is also part of the
> atomic operation.

For what it's worth my reading agrees on this point: any update
of *expected is included in the indivisible atomic operation of
comparing and either replacing of *object or updating of *expected.

> This doesn't map well to compare-and-swap hardware instructions,
> where the content of *object would be written to a register, and
> later stored into *expected by a separate instruction.

What I think you mean is that it /might/ not map nicely onto an
unprotected compare-and-swap operation, depending on context.
More on that point given below.

> Of course, this difference only matters if it is observable. I
> think it is observable if just after the compare-and swap, a second
> thread writes *object, while the first thread has already done the
> comparison, but not yet updated *expected.

Yes, in the worst case the combination of compare-and-swap and
(possible) subsequent update of *expected needs to be wrapped in
a protective shell covered by an inter-thread mutex, to prevent
other threads from breaking the atomicity requirements. But
please note: in the worst case. See below.

> Then e.g. a thread thread could read the new value from
> *object, but the old one from *expected, which is not possible
> if atomic_compare_exchange_strong is fully atomic (i.e. also
> wrt. *expected).

Consider the following function (disclaimer: not compiled):

void
update_atomic( volatile A* it, C new_from_old( C ) ){
C old = { 0 }, new;
do {
new = new_from_old( old );
} while( ! atomic_compare_exchange_strong( it, &old, new ) );
}

An implementation may compile this function using an inline
compare-and-swap, with no surrounding protection of using an
inter-thread mutex. A surrounding mutex call is not needed,
because the variable 'old' cannot be affected by what other
threads are doing (at least not in a way that has defined
behavior), and implementations can know that based on the
code in the function definition.

> Is my understanding here correct (I'm not an expert on atomics)?

My comments above are made primarily based on what I believe the
C standard requires, and not so much on trying to discern what I
think your understanding may be. If what I've said helps your
understanding then that's great but my emphasis has been on
explicating the consequences of what is in the C standard.

Re: atomic_compare_exchange atomicity of write to *expected

<87zgrgp0rj.fsf@mid.deneb.enyo.de>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=370&group=comp.std.c#370

  copy link   Newsgroups: comp.std.c
Path: rocksolid2!i2pn.org!weretis.net!feeder8.news.weretis.net!news.szaf.org!news.enyo.de!.POSTED!not-for-mail
From: fw...@deneb.enyo.de (Florian Weimer)
Newsgroups: comp.std.c
Subject: Re: atomic_compare_exchange atomicity of write to *expected
Date: Mon, 11 Oct 2021 07:51:28 +0200
Message-ID: <87zgrgp0rj.fsf@mid.deneb.enyo.de>
References: <sjhadm$4uu$1@solani.org> <20211008132058.19c078b9@coosemans.org>
Mime-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 8bit
Injection-Info: news.enyo.de;
logging-data="31249"; mail-complaints-to="news@enyo.de"
Cancel-Lock: sha1:PXylZTJxirXYPU9BPNt30kobDHM=
 by: Florian Weimer - Mon, 11 Oct 2021 05:51 UTC

* Tijl Coosemans:

> On Tue, 5 Oct 2021 12:46:13 +0200 Philipp Klaus Krause <pkk@spth.de>
> wrote:
>> For the atomic_compare-exchange family, e.g.
>>
>> _Bool atomic_compare_exchange_strong(volatile A *object, C *expected, C
>> desired);
>>
>> the description states "Atomically, compares the contents of the memory
>> pointed to by object for equality with that pointed to by expected, and
>> if true, replaces the contents of the memory pointed to by object with
>> desired, and if false, updates the contents of the memory pointed to by
>> expected with that pointed to by object."
>>
>> This reads to me as if the write to *expected is also part of the atomic
>> operation. This doesn't map well to compare-and-swap hardware
>> instructions, where the content of *object would be written to a
>> register, and later stored into *expected by a separate instruction.
>>
>> Of course, this difference only matters if it is observable. I think it
>> is observable if just after the compare-and swap, a second thread writes
>> *object, while the first thread has already done the comparison, but not
>> yet updated *expected.
>> Then e.g. a thread thread could read the new value from *object, but the
>> old one from *expected, which is not possible if
>> atomic_compare_exchange_strong is fully atomic (i.e. also wrt. *expected).
>>
>> Is my understanding here correct (I'm not an expert on atomics)?
>
> *expected has type C, so it's not atomic, but even if it was, the
> compare-and-swap, reading of *object, and reading of *expected are three
> different operations and anything can happen in between them.

And there are very few CPUs which can actually implement an atomic
updated of *expected as well. Since the intent is that
atomic_compare_exchange maps to a typical CAS instruction, it's
apparent hat an atomic update of *expected can't be the intended
meaning.

I do think that the wording in the standard is ambiguous, though.

Re: atomic_compare_exchange atomicity of write to *expected

<86zgoy10qa.fsf@linuxsc.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=422&group=comp.std.c#422

  copy link   Newsgroups: comp.std.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: tr.17...@z991.linuxsc.com (Tim Rentsch)
Newsgroups: comp.std.c
Subject: Re: atomic_compare_exchange atomicity of write to *expected
Date: Sat, 18 Dec 2021 05:50:05 -0800
Organization: A noiseless patient Spider
Lines: 68
Message-ID: <86zgoy10qa.fsf@linuxsc.com>
References: <sjhadm$4uu$1@solani.org> <20211008132058.19c078b9@coosemans.org> <87zgrgp0rj.fsf@mid.deneb.enyo.de>
Mime-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Injection-Info: reader02.eternal-september.org; posting-host="6f266d79a765f44bdb30d571740e8aec";
logging-data="2091"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19/NDWEpUEtZPlJNFZjDhdHVMuleww2WO4="
User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.4 (gnu/linux)
Cancel-Lock: sha1:e4YkQMUHiI1sWSa4TR+/simWdmc=
sha1:1IowohvG5Q5x40h4w97HRcfI7Jo=
 by: Tim Rentsch - Sat, 18 Dec 2021 13:50 UTC

Florian Weimer <fw@deneb.enyo.de> writes:

> * Coosemans:
>
>> On Tue, 5 Oct 2021 12:46:13 +0200 Philipp Klaus Krause <pkk@spth.de>
>> wrote:
>>
>>> For the atomic_compare-exchange family, e.g.
>>>
>>> _Bool atomic_compare_exchange_strong(volatile A *object, C *expected, C
>>> desired);
>>>
>>> the description states "Atomically, compares the contents of the
>>> memory pointed to by object for equality with that pointed to by
>>> expected, and if true, replaces the contents of the memory pointed
>>> to by object with desired, and if false, updates the contents of
>>> the memory pointed to by expected with that pointed to by object."
>>>
>>> This reads to me as if the write to *expected is also part of the
>>> atomic operation. This doesn't map well to compare-and-swap
>>> hardware instructions, where the content of *object would be
>>> written to a register, and later stored into *expected by a
>>> separate instruction.
>>>
>>> Of course, this difference only matters if it is observable. I
>>> think it is observable if just after the compare-and swap, a
>>> second thread writes *object, while the first thread has already
>>> done the comparison, but not yet updated *expected.
>>>
>>> Then e.g. a thread thread could read the new value from *object,
>>> but the old one from *expected, which is not possible if
>>> atomic_compare_exchange_strong is fully atomic (i.e. also
>>> wrt. *expected).
>>>
>>> Is my understanding here correct (I'm not an expert on atomics)?
>>
>> *expected has type C, so it's not atomic, but even if it was, the
>> compare-and-swap, reading of *object, and reading of *expected are
>> three different operations and anything can happen in between them.
>
> And there are very few CPUs which can actually implement an
> atomic updated of *expected as well. Since the intent is that
> atomic_compare_exchange maps to a typical CAS instruction,

I don't see anything in the C standard that supports this
premise.

> it's apparent hat an atomic update of *expected can't be the
> intended meaning.

I finally had a chance to look at this question more closely,
after which I think there is a stronger case for the opposite
conclusion. The description of the atomic_compare_exchange
generic functions (given in section 7.17.7.4), along with the
introductory comments on atomic functions (given in 7.17.1, and
in particular 7.17.1 p5.4) and the definitions for the various
choices of memory_order (given in 7.17.3, and in particular
7.17.3 p6, for memory_order_seq_cst), taken together imply that
any update of *expected is covered by the atomic umbrella that
surrounds the atomic_compare_exchange_strong operation.

Note that this protection does not extend to arbitrary accesses
of *expected (at least I don't think it does), but it does
include all other accesses that are conducted under the auspices
of a memory_order_seq_cst operation.

Short summary: any update of *expected is part of the overall
atomic operation.

1
server_pubkey.txt

rocksolid light 0.9.8
clearnet tor