Compiler ordering barriers in C++0x
  Home FAQ Contact Sign in
Your Ad Here
comp.lang.c++.moderated only
 
Advanced search
POPULAR GROUPS

more...

comp.lang.c++.moderated Profile…

 Up
Compiler ordering barriers in C++0x         


Author: Dmitriy Vyukov
Date: Apr 23, 2008 23:10

Will it be possible to issue only compiler ordering barrier (release,
acquire or full), but not hardware ordering barrier in C++0x?

Accesses to volatile variables are ordered only with respect to
accesses to other volatile variables. What I want it to order access
to variable with respect to accesses to all other volatile and non-
volatile variables (w/o any hardware barriers, only compiler
ordering).

Now it can be accomplished with "__asm__ __volatile__
("" : : :"memory")" on gcc, and with _ReadWriteBarrier() on msvc. I am
interested whether it will be possible to do this in C++0x language w/
o compiler dependency.

Such compiler barriers are useful in effective synchronization
algorithms like SMR+RCU:
http://sourceforge.net/project/showfiles.php?group_id=127837
(fastsmr package)
because they allows one to eliminate all hardware memory barriers from
fast-path.

Dmitriy V'jukov
Show full article (1.03Kb)
14 Comments
Re: Compiler ordering barriers in C++0x         


Author: nickf3
Date: Apr 28, 2008 00:00

On Apr 24, 3:04 am, Dmitriy Vyukov gmail.com> wrote:
> Will it be possible to issue only compiler ordering barrier (release,
> acquire or full), but not hardware ordering barrier in C++0x?
>
> Accesses to volatile variables are ordered only with respect to
> accesses to other volatile variables. What I want it to order access
> to variable with respect to accesses to all other volatile and non-
> volatile variables (w/o any hardware barriers, only compiler
> ordering).
>
> Now it can be accomplished with "__asm__ __volatile__
> ("" : : :"memory")" on gcc, and with _ReadWriteBarrier() on msvc. I am
> interested whether it will be possible to do this in C++0x language w/
> o compiler dependency.
>
> Such compiler barriers are useful in effective synchronization
> algorithms like SMR+RCU:http://sourceforge.net/project/showfiles.php?group_id=127837
> (fastsmr package)
> because they allows one to eliminate all hardware memory barriers from
> fast-path. ...
Show full article (1.27Kb)
no comments
Re: Compiler ordering barriers in C++0x         


Author: Anthony Williams
Date: May 2, 2008 02:50

Dmitriy Vyukov gmail.com> writes:
> Will it be possible to issue only compiler ordering barrier (release,
> acquire or full), but not hardware ordering barrier in C++0x?
>
> Accesses to volatile variables are ordered only with respect to
> accesses to other volatile variables. What I want it to order access
> to variable with respect to accesses to all other volatile and non-
> volatile variables (w/o any hardware barriers, only compiler
> ordering).

Within a single thread, all operations *are* ordered. The ordering relations
are happens-before and sequenced-before. A is sequenced-before before B when
A physically comes before B in the source code, or the sequencing rules say so
(e.g. lhs of a comma op is sequenced before rhs).

If A is sequenced-before B, then A also happens-before B. If you then
introduce a synchronization edge between thread T1 and thread T2
(e.g. store(mem_order_release) on T1, load(mem_order_acquire) on T2), then
anything that happens-before the store also happens-before the load.
Show full article (2.08Kb)
no comments
Re: Compiler ordering barriers in C++0x         


Author: Szabolcs Ferenczi
Date: May 2, 2008 10:50

On May 2, 12:43 pm, Anthony Williams yahoo.com> wrote:
> [...]
> All accesses to shared data MUST be synchronized with atomics: [...]

Can you elaborate this point please. How can you generally synchronise
N processes with help of atomics? Do you mean only two processes under
certain circumstances?

Best would be if you give an example for it.

Thanks for your help.

Best Regards,
Szabolcs

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
no comments
Re: Compiler ordering barriers in C++0x         


Author: Anthony Williams
Date: May 3, 2008 04:20

Szabolcs Ferenczi gmail.com> writes:
> On May 2, 12:43 pm, Anthony Williams yahoo.com> wrote:
>> [...]
>> All accesses to shared data MUST be synchronized with atomics: [...]
>
> Can you elaborate this point please. How can you generally synchronise
> N processes with help of atomics? Do you mean only two processes under
> certain circumstances?

If any thread modifies shared data that is not of type atomic_xxx, the
developer must ensure appropriate synchronization with any other thread that
accesses that shared data in order to avoid a data race (and the undefined
behaviour that comes with that). If you're using atomics, that means you must
have (at least) a release on some atomic variable by the modifying thread
after the modify, and an acquire on the same atomic variable by the other
thread before its access (read or write).

If you have N threads accessing the shared data, and one does a modify, all
the other N-1 threads must be properly synchronized with the modifying thread
as above in order to avoid data races.
Show full article (1.55Kb)
no comments
Re: Compiler ordering barriers in C++0x         


Author: Dmitriy V'jukov
Date: May 3, 2008 04:20

On 2 п╪п╟п╧, 14:43, Anthony Williams yahoo.com> wrote:
> Dmitriy Vyukov gmail.com> writes:
>> Will it be possible to issue only compiler ordering barrier (release,
>> acquire or full), but not hardware ordering barrier in C++0x?
>
>> Accesses to volatile variables are ordered only with respect to
>> accesses to other volatile variables. What I want it to order access
>> to variable with respect to accesses to all other volatile and non-
>> volatile variables (w/o any hardware barriers, only compiler
>> ordering).
>
> Within a single thread, all operations *are* ordered. The ordering relations
> are happens-before and sequenced-before. A is sequenced-before before B when
> A physically comes before B in the source code, or the sequencing rules say so
> (e.g. lhs of a comma op is sequenced before rhs).
>
> If A is sequenced-before B, then A also happens-before B. If you then
> introduce a synchronization edge between thread T1 and thread T2
> (e.g. store(mem_order_release) on T1, load(mem_order_acquire) on T2), then
> anything that happens-before the store also happens-before the load. ...
Show full article (3.13Kb)
no comments
Re: Compiler ordering barriers in C++0x         


Author: Anthony Williams
Date: May 3, 2008 14:21

"Dmitriy V'jukov" gmail.com> writes:
> On 2 май, 14:43, Anthony Williams yahoo.com> wrote:
> I can prove that I have happens-before relation if I can enforce
> correct compiler ordering. Consider following code:
>
> std...
Show full article (1.96Kb)
no comments
Re: Compiler ordering barriers in C++0x         


Author: Chris Thomasson
Date: May 4, 2008 15:20

[this is in response to msg #13688, which has not been processed yet... I
made a mistake, here is correction...]
Show full article (1.07Kb)
no comments
Re: Compiler ordering barriers in C++0x         


Author: Chris Thomasson
Date: May 4, 2008 15:20

"Dmitriy V'jukov" gmail.com> wrote in message
news:c5199756-8669-419e-84e9-1de582eff02e@y38g2000hsy.googlegroups.com...
[...]
> I can prove that I have happens-before relation if I can enforce
> correct compiler ordering. Consider following code:
>
> std::vector g_nonatomic_user_data;
> std::atomic_int g_atomic1;
> std::atomic_int g_atomic2;
>
> std::vector thread()
> {
> g_atomic1.store(1, std::memory_order_relaxed);
> // compiler store-load fence
> if (g_atomic2.load(std::memory_order_relaxed))
[...]

Are you sure that atomic store with 'memory_order_relaxed' semantics injects
a #StoreLoad after the operation?
Show full article (0.78Kb)
no comments
Re: Compiler ordering barriers in C++0x         


Author: Szabolcs Ferenczi
Date: May 4, 2008 15:20

On May 3, 2:13 pm, Anthony Williams yahoo.com> wrote:
> Szabolcs Ferenczi gmail.com> writes:
>> On May 2, 12:43 pm, Anthony Williams yahoo.com> wrote:
>>> [...]
>>> All accesses to shared data MUST be synchronized with atomics: [...]
>
>> Can you elaborate this point please. How can you generally synchronise
>> N processes with help of atomics? Do you mean only two processes under
>> certain circumstances?
>
> If any thread modifies shared data that is not of type atomic_xxx, the
> developer must ensure appropriate synchronization with any other thread that
> accesses that shared data in order to avoid a data race (and the undefined
> behaviour that comes with that).

It is clear that you must synchronise access to shared variable.
Normally you must use a Critical Region for that.
Show full article (2.27Kb)
no comments
1 2