Re: how to ensure other threads load new value from memory
- From: Markus <philipjfry@xxxxxx>
- Date: 12 Sep 2008 14:13:06 GMT
David Schwartz <davids@xxxxxxxxxxxxx> schrieb:
On Sep 12, 6:53 am, "Dmitriy V'jukov" <dvyu...@xxxxxxxxx> wrote:nnot affect
On Sep 12, 5:41 pm, David Schwartz <dav...@xxxxxxxxxxxxx> wrote:
On Sep 12, 6:32 am, Markus <philipj...@xxxxxx> wrote:
If the compiler for some reason can prove that the synchronization ca
y compilerbuffer[] (perhaps because it is done with simple inline assembly or b
new contents of buffer!intrinsics), I see no reason why it should be obliged to consider the
If the compiler can prove something that is false, then it is broken.
If mutex is implemented is such a way that compiler can prove that it
cannot affect something which it actually affects than mutex
implementation is broken.
And this is why, as compilers get smarter, it may become necessary to
coordinate the design of synchronization primitives with the design of
the compiler. In the case of GCC, the 'volatile' keyword is overloaded
for this purpose. Note that this is *not* the 'volatile' specified in
the C standard, but totally different semantics implemented with the
same keyword. (Also, named invalidations are used.)
For example, from the Linux kernel header files:
static __always_inline void __raw_spin_unlock(raw_spinlock_t *lock)
{
asm volatile(UNLOCK_LOCK_PREFIX "incb %0"
: "+m" (lock->slock)
:
: "memory", "cc");
}
Note the 'volatile' which tells the compiler that it must not try to
understand what this code does or try to move it. Note also the
"memory", which is a named invalidation (it says 'assume memory may
have changed curing this call').
Now you might say "why can't the compiler look inside the assembly
code and see that only the spinlock is affected, so it can assume no
other memory addresses have changed? And the answer is -- because we
specifically told it that it could not do that.
DS
Thanks for your answer, David!
Did I get that correct: the C programming standard is not sufficient to
guarantee multi-threaded programs to work. Like OpenMP changes the semantics
of a parallel block and adds some consistency model, it is something
necessary beyond the programming standard to make a compiler able creating
"threadable" code.
Regards,
Markus
.
- Follow-Ups:
- Re: how to ensure other threads load new value from memory
- From: David Schwartz
- Re: how to ensure other threads load new value from memory
- References:
- how to ensure other threads load new value from memory
- From: Markus
- Re: how to ensure other threads load new value from memory
- From: David Schwartz
- Re: how to ensure other threads load new value from memory
- From: Markus
- Re: how to ensure other threads load new value from memory
- From: David Schwartz
- Re: how to ensure other threads load new value from memory
- From: Dmitriy V'jukov
- Re: how to ensure other threads load new value from memory
- From: David Schwartz
- how to ensure other threads load new value from memory
- Prev by Date: Re: how to ensure other threads load new value from memory
- Next by Date: Re: how to ensure other threads load new value from memory
- Previous by thread: Re: how to ensure other threads load new value from memory
- Next by thread: Re: how to ensure other threads load new value from memory
- Index(es):
Relevant Pages
|
Loading