caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] Are refs volatile?
@ 2015-11-04 12:43 Richard W.M. Jones
  2015-11-04 13:18 ` Gerd Stolpmann
  2015-11-04 15:08 ` Gabriel Scherer
  0 siblings, 2 replies; 5+ messages in thread
From: Richard W.M. Jones @ 2015-11-04 12:43 UTC (permalink / raw)
  To: caml-list

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

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [Caml-list] Are refs volatile?
  2015-11-04 12:43 [Caml-list] Are refs volatile? Richard W.M. Jones
@ 2015-11-04 13:18 ` Gerd Stolpmann
  2015-11-04 15:08 ` Gabriel Scherer
  1 sibling, 0 replies; 5+ messages in thread
From: Gerd Stolpmann @ 2015-11-04 13:18 UTC (permalink / raw)
  To: Richard W.M. Jones; +Cc: caml-list

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

Am Mittwoch, den 04.11.2015, 12:43 +0000 schrieb Richard W.M. Jones:
> 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)?

Interesting question. My guess is that it is much harder to optimize
side-effects in the presence of closures, and that it is fairly unlikely
that anybody attempts this. In this piece of code, you must be sure that
[task] doesn't set [quit], but this ref can be well hidden, and the
analysis needed to figure this out seems to be complicated.

Gerd
-- 
------------------------------------------------------------
Gerd Stolpmann, Darmstadt, Germany    gerd@gerd-stolpmann.de
My OCaml site:          http://www.camlcity.org
Contact details:        http://www.camlcity.org/contact.html
Company homepage:       http://www.gerd-stolpmann.de
------------------------------------------------------------


[-- Attachment #2: This is a digitally signed message part --]
[-- Type: application/pgp-signature, Size: 473 bytes --]

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [Caml-list] Are refs volatile?
  2015-11-04 12:43 [Caml-list] Are refs volatile? Richard W.M. Jones
  2015-11-04 13:18 ` Gerd Stolpmann
@ 2015-11-04 15:08 ` Gabriel Scherer
  2015-11-04 15:23   ` Richard W.M. Jones
  1 sibling, 1 reply; 5+ messages in thread
From: Gabriel Scherer @ 2015-11-04 15:08 UTC (permalink / raw)
  To: Richard W.M. Jones; +Cc: caml users

[-- 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 --]

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [Caml-list] Are refs volatile?
  2015-11-04 15:08 ` Gabriel Scherer
@ 2015-11-04 15:23   ` Richard W.M. Jones
  2015-11-04 15:56     ` Pierre Chambart
  0 siblings, 1 reply; 5+ messages in thread
From: Richard W.M. Jones @ 2015-11-04 15:23 UTC (permalink / raw)
  To: Gabriel Scherer; +Cc: caml users

On Wed, Nov 04, 2015 at 04:08:48PM +0100, Gabriel Scherer wrote:
> I find the question hard to understand, so I may have missed a subtlety in
> the code you have shown.

It's very possible I don't know what I'm talking about, but
this is my analysis:

> >   List.iter (
> >     fun task ->
> >       if not !quit then task ();
> >   ) tasks;

Suppose that the optimizer can see the contents of `task', and see
that it cannot update the quit variable.  In that case, could it
assume that quit is a constant, and hoist the test outside the loop?
ie. transforming the code to:

  if not !quit then (
    List.iter (
      fun task ->
        task ();
    ) tasks
  )

Rich.

-- 
Richard Jones
Red Hat

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [Caml-list] Are refs volatile?
  2015-11-04 15:23   ` Richard W.M. Jones
@ 2015-11-04 15:56     ` Pierre Chambart
  0 siblings, 0 replies; 5+ messages in thread
From: Pierre Chambart @ 2015-11-04 15:56 UTC (permalink / raw)
  To: Richard W.M. Jones, Gabriel Scherer; +Cc: caml users

On 04/11/2015 15:23, Richard W.M. Jones wrote:
> On Wed, Nov 04, 2015 at 04:08:48PM +0100, Gabriel Scherer wrote:
>> I find the question hard to understand, so I may have missed a subtlety in
>> the code you have shown.
> It's very possible I don't know what I'm talking about, but
> this is my analysis:
>
>>>   List.iter (
>>>     fun task ->
>>>       if not !quit then task ();
>>>   ) tasks;
> Suppose that the optimizer can see the contents of `task', and see
> that it cannot update the quit variable.  In that case, could it
> assume that quit is a constant, and hoist the test outside the loop?
> ie. transforming the code to:
>
>   if not !quit then (
>     List.iter (
>       fun task ->
>         task ();
>     ) tasks
>   )
>
> Rich.
>
No. There is no specified semantic of ocaml, but in any reasonable
semantics of it won't be allowed.
I won't write something that could do that, and I would consider it a
bug if it happened.
-- 
Pierre Chambart

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2015-11-04 15:57 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-11-04 12:43 [Caml-list] Are refs volatile? Richard W.M. Jones
2015-11-04 13:18 ` Gerd Stolpmann
2015-11-04 15:08 ` Gabriel Scherer
2015-11-04 15:23   ` Richard W.M. Jones
2015-11-04 15:56     ` Pierre Chambart

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).