caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Gabriel Scherer <gabriel.scherer@gmail.com>
To: "Richard W.M. Jones" <rich@annexia.org>
Cc: caml users <caml-list@inria.fr>
Subject: Re: [Caml-list] Are refs volatile?
Date: Wed, 4 Nov 2015 16:08:48 +0100	[thread overview]
Message-ID: <CAPFanBFuCxnKuZv_ioBPeP2x445-nJXek-f4fBw-W9Ge0GtKAQ@mail.gmail.com> (raw)
In-Reply-To: <20151104124321.GA25230@annexia.org>

[-- Attachment #1: Type: text/plain, Size: 1637 bytes --]

I find the question hard to understand, so I may have missed a subtlety in
the code you have shown.

The purpose of volatile memory is to share memory between the current
program and an unknown environment. In your exemple, the value of (quit), a
boolean reference, is *not* shared, it is the value of (set_quit) that is
passed to the environment. So the question *cannot* be about the accesses
to the value "quit", which are clearly marked.

One way to rephrase the question would be: "could the compiler assume than
(set_quit) will never be called and thus optimize (!quit) to (false)?". I
don't see how that could ever be correct, given that the function
(set_quit) is passed to an external function.

On Wed, Nov 4, 2015 at 1:43 PM, Richard W.M. Jones <rich@annexia.org> wrote:

> Some code I wrote recently does:
>
>   let quit = ref false in
>   let set_quit _ = quit := true in
>   Sys.set_signal Sys.sigint (Sys.Signal_handle set_quit);
>   Sys.set_signal Sys.sigquit (Sys.Signal_handle set_quit);
>
> and later on (where `tasks' is a list of long-running tasks):
>
>   List.iter (
>     fun task ->
>       if not !quit then task ();
>   ) tasks;
>
> This works fine.  My question is, could a change to the compiler in
> future cause the reference to !quit to be optimized away?  And if so,
> is there a way to mark it as "volatile" (in the C sense)?
>
> Rich.
>
> --
> Richard Jones
> Red Hat
>
> --
> Caml-list mailing list.  Subscription management and archives:
> https://sympa.inria.fr/sympa/arc/caml-list
> Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
> Bug reports: http://caml.inria.fr/bin/caml-bugs
>

[-- Attachment #2: Type: text/html, Size: 2428 bytes --]

  parent reply	other threads:[~2015-11-04 15:09 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-11-04 12:43 Richard W.M. Jones
2015-11-04 13:18 ` Gerd Stolpmann
2015-11-04 15:08 ` Gabriel Scherer [this message]
2015-11-04 15:23   ` Richard W.M. Jones
2015-11-04 15:56     ` Pierre Chambart

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

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

  git send-email \
    --in-reply-to=CAPFanBFuCxnKuZv_ioBPeP2x445-nJXek-f4fBw-W9Ge0GtKAQ@mail.gmail.com \
    --to=gabriel.scherer@gmail.com \
    --cc=caml-list@inria.fr \
    --cc=rich@annexia.org \
    /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 NNTP newsgroup(s).