unofficial mirror of guile-devel@gnu.org 
 help / color / mirror / Atom feed
From: "Philip McGrath" <philip@philipmcgrath.com>
To: "Andrew Tropin" <andrew@trop.in>, "Jean Abou Samra" <jean@abou-samra.fr>
Cc: guile-devel@gnu.org, "Andy Wingo" <wingo@igalia.com>
Subject: Re: [PATCH] Add atomic-box-update! function to (ice-9 atomic)
Date: Thu, 22 Jun 2023 01:21:27 -0400	[thread overview]
Message-ID: <4c24e86e-2dcb-4d41-89c4-67814b6ef4f1@app.fastmail.com> (raw)
In-Reply-To: <87ttv01711.fsf@trop.in>

On Wed, Jun 21, 2023, at 11:59 PM, Andrew Tropin wrote:
> On 2023-06-21 18:54, Jean Abou Samra wrote:
>
>>> Le 21 juin 2023 à 18:46, Andrew Tropin <andrew@trop.in> a écrit :
>>> 
>>> Make sense, but it's hard for me to say something valuable on this
>>> topic.  Usually, I don't use eq? and don't have enough knowledge of its
>>> internals.
>>
>>
>> *Currently*, it just checks whether the two C-level SCM values are the
>> same bitwise, so even implicit copies of fixnums will remain eq?. In
>> theory, Guile could make copies of bignums. I am not aware of it doing
>> so.
>>
>> However, all that is guaranteed is that (eq? a a) when a is a
>> non-immediate (pair, string, vector, hashtable, etc) or one of a few
>> constants like booleans, the empty list and *unspecified*. Notably, it
>> isn't guaranteed for numbers or characters.
>>
> [...]  I'm
> almost sure that we need eq? here as we need to make sure that the value
> previously stored and returned atomic-box-compare-and-swap! is the same
> object in memory, however this example from manual is indeed confusing:
>
> --8<---------------cut here---------------start------------->8---
>  (let ((n (+ 2 3)))
>        (eq? n n))                           ==>  _unspecified_
> --8<---------------cut here---------------end--------------->8---
>
> So maybe you are right and it's better to use eqv? here.
>

The problem with eqv? is that it doesn't correspond well to machine-level atomic compare-and-set instructions, so using it would defeat the purpose of lightweight atomic boxes.

R6RS specifies that "the behavior of eq? on number objects and characters is implementation-dependent, but it always returns either #t or #f, and returns #t only when eqv? would also return #t." [1] I think the right thing to do here is for Guile to provide more guarantees about its implementation of eq?.

Guaranteeing that fixnums are eq? when they are = would be particularly reasonable and extremely unlikely to cause constrain any future port of Guile: the whole point of fixnums is to support efficient machine operations like this. I also think most Schemers would be quite surprised if (lambda (x) (eq? x x)) ever returned #f. More broadly, *The Scheme Programming Language* says at the beginning of its documentation for eq? that, "In most Scheme systems, two objects are considered identical if they are represented internally by the same pointer value and distinct (not identical) if they are represented internally by different pointer values, although other criteria, such as time-stamping, are possible."

In any case, the current documentation for atomic-box-compare-and-swap! is clear that the comparison is eq?: it just means that, when the behavior of eq? is unreliable, so is the behavior of atomic-box-compare-and-swap!.

Tangentially, does atomic-box-compare-and-swap! handle spurious failures on architectures like ARM, or does it expose machine semantics to the programmer? Maybe that's covered by details of the C11 memory model, but I don't think "sequential consistency" alone is enough to answer the question.

-Philip

[1]: https://www.r6rs.org/final/html/r6rs/r6rs-Z-H-14.html#node_sec_11.5
[2]: https://scheme.com/tspl4/objects.html#./objects:s10



  reply	other threads:[~2023-06-22  5:21 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-06-19 12:20 [PATCH] Add atomic-box-update! function to (ice-9 atomic) Andrew Tropin
2023-06-21  9:06 ` Jean Abou Samra
2023-06-21  9:06   ` Jean Abou Samra
2023-06-21 16:46     ` Andrew Tropin
2023-06-21 16:54       ` Jean Abou Samra
2023-06-22  3:59         ` Andrew Tropin
2023-06-22  5:21           ` Philip McGrath [this message]
2023-06-22  9:02             ` Andrew Tropin
2023-06-22 21:42               ` Philip McGrath
2023-07-13  3:30                 ` Andrew Tropin
2023-08-22 10:51 ` Andrew Tropin
2023-08-22 10:59 ` [PATCH v2] " Andrew Tropin
2023-08-22 17:51   ` Maxime Devos
2023-08-24 16:19     ` Andrew Tropin
2023-08-24 15:38 ` [PATCH v3] " Andrew Tropin
2023-09-20  8:17 ` [PATCH v4] " Andrew Tropin

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/guile/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=4c24e86e-2dcb-4d41-89c4-67814b6ef4f1@app.fastmail.com \
    --to=philip@philipmcgrath.com \
    --cc=andrew@trop.in \
    --cc=guile-devel@gnu.org \
    --cc=jean@abou-samra.fr \
    --cc=wingo@igalia.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).