comp.lang.ada
 help / color / mirror / Atom feed
From: Niklas Holsti <niklas.holsti@tidorum.invalid>
Subject: Re: Portable memory barrier?
Date: Sat, 6 May 2017 23:42:53 +0300
Date: 2017-05-06T23:42:53+03:00	[thread overview]
Message-ID: <en6qqdF2h22U1@mid.individual.net> (raw)
In-Reply-To: <oel8me$i10$1@dont-email.me>

On 17-05-06 22:41 , Jeffrey R. Carter wrote:
> On 05/06/2017 04:17 PM, Jere wrote:
>>
>> Side note:  If I read it right, you seemed to indicate that volatile
>> has nothing
>> to do with sequence.  I'm no Ada expert, so I wanted to ask your
>> interpretation
>> of section 16/3 of C.6.  I'm used to C and C++ where if you have two
>> volatile
>> variables being updated like this:
>>
>> Vol_Var_1 = 0;
>> Vol_Var_2 = 23;
>>
>> That the C and C++ standard guarantees the "compiler" will not reorder
>> those two
>> statements.  Note this says nothing about atomicness (which is only
>> one reason
>> among others why Volatile is not sufficient for threading).  It also
>> doesn't
>> prevent the processor from reordering them.  When I read that section
>> in C.6 of
>> the RM, the verbage made me think Ada also prevented the compiler from
>> reordering the statements (again no relation to atomicness or CPU
>> reordering).
>>
>> Is this not the case?
>
> Volatile is intended to guarantee that all reads and writes deal with
> the memory location directly.

Well... for some value of "directly". As I understand it, reading or 
writing a volatile variable can still use the cache, so depending on the 
cache architecture and state, a read may or may not read the "real" 
memory, and ditto for a write.

> The idea is for things like memory-mapped
> H/W registers, where the H/W might change the value of the register or
> writing to the register causes the H/W to do something.

And such registers are usually defined in the HW as non-cacheable, so in 
the end volatile access to such registers works as intended, and as you say.

> This prevents
> optimizations that keep the value in a register for a while before
> writing it back to memory, for example, but not reordering the accesses
> to different variables IIUC.

Just as in C, volatile accesses in Ada are guaranteed to occur in the 
order and number written in the source code and executed in the 
"standard" order:

RM C.6 (16/3): All tasks of the program (on all processors) that read or 
update volatile variables see the same order of updates to the 
variables. A use of an atomic variable or other mechanism may be 
necessary to avoid erroneous execution and to ensure that access to 
nonatomic volatile variables is sequential (see 9.10).

RM C.6 (20): The external effect of a program (see 1.1.3) is defined to 
include each read and update of a volatile or atomic object. The 
implementation shall not generate any memory reads or updates of atomic 
or volatile objects other than those specified by the program.


-- 
Niklas Holsti
Tidorum Ltd
niklas holsti tidorum fi
       .      @       .


  reply	other threads:[~2017-05-06 20:42 UTC|newest]

Thread overview: 65+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-05-06  2:23 Portable memory barrier? Jere
2017-05-06  8:47 ` Jeffrey R. Carter
2017-05-06 14:17   ` Jere
2017-05-06 19:08     ` Dennis Lee Bieber
2017-05-06 19:26     ` Jeffrey R. Carter
2017-05-06 19:41     ` Jeffrey R. Carter
2017-05-06 20:42       ` Niklas Holsti [this message]
2017-05-09 19:49     ` Randy Brukardt
2017-05-09 22:07       ` Jere
2017-05-11  1:14         ` Randy Brukardt
2017-05-10 18:28       ` Shark8
2017-05-11  1:17         ` Randy Brukardt
2017-05-11 16:23           ` Jeffrey R. Carter
2017-05-07 20:18 ` Robert Eachus
2017-05-08  7:45   ` Dmitry A. Kazakov
2017-05-08 15:56     ` Robert Eachus
2017-05-08 16:22       ` Dmitry A. Kazakov
2017-05-08 18:39         ` Robert Eachus
2017-05-08 19:18         ` Robert Eachus
2017-05-08 21:09           ` Dmitry A. Kazakov
2017-05-08 23:24             ` Robert Eachus
2017-05-09  0:30               ` Jere
2017-05-09  4:02                 ` Robert Eachus
2017-05-09  4:32                 ` Robert Eachus
2017-05-09  4:44                   ` Robert Eachus
2017-05-09 22:26                   ` Jere
2017-05-09 20:01                 ` Randy Brukardt
2017-05-09 19:57               ` Randy Brukardt
2017-05-10  0:51                 ` Jere
2017-05-10  5:25                   ` J-P. Rosen
2017-05-10 22:56                     ` Jere
2017-05-11  7:36                       ` Dmitry A. Kazakov
2017-05-13 20:25                         ` Jere
2017-05-10  7:13                   ` Dmitry A. Kazakov
2017-05-10 16:45                     ` Robert Eachus
2017-05-10 17:28                       ` Simon Wright
2017-05-10 23:21                     ` Jere
2017-05-11  0:47                       ` Randy Brukardt
2017-05-13 20:11                         ` Jere
2017-05-15 22:33                           ` Randy Brukardt
2017-05-10 23:30                     ` Jere
2017-05-11  0:38                     ` Randy Brukardt
2017-05-10 16:38                   ` Jeffrey R. Carter
2017-05-10 23:40                     ` Jere
2017-05-10 16:19                 ` Robert Eachus
2017-05-11  1:02                   ` Randy Brukardt
2017-05-11  1:51                     ` Robert Eachus
2017-05-15 22:45                       ` Randy Brukardt
2017-05-08 20:29         ` Niklas Holsti
2017-05-08 21:09           ` Dmitry A. Kazakov
2017-05-09  4:34             ` Niklas Holsti
2017-05-09  6:16               ` Niklas Holsti
2017-05-09  8:34                 ` Dmitry A. Kazakov
2017-05-09 20:18                 ` Randy Brukardt
2017-05-09 20:10           ` Randy Brukardt
2017-05-09  0:05         ` Jere
2017-05-09  8:26           ` Dmitry A. Kazakov
2017-05-09 19:53         ` Randy Brukardt
2017-05-09 20:27           ` Dmitry A. Kazakov
2017-05-11  0:35             ` Randy Brukardt
2017-05-11  8:24               ` Dmitry A. Kazakov
2017-05-15 22:53                 ` Randy Brukardt
2017-05-18 17:44                   ` Dmitry A. Kazakov
2017-05-18 21:01                     ` Randy Brukardt
2017-05-19  7:54                       ` Dmitry A. Kazakov
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox