Re: pg_memory_barrier() doesn't compile, let alone work, for me

From: Martijn van Oosterhout <kleptog(at)svana(dot)org>
To: Robert Haas <robertmhaas(at)gmail(dot)com>
Cc: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>, pgsql-hackers(at)postgresql(dot)org
Subject: Re: pg_memory_barrier() doesn't compile, let alone work, for me
Date: 2013-07-16 20:42:33
Message-ID: 20130716204233.GB28628@svana.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On Sun, Jul 14, 2013 at 09:26:38PM -0400, Robert Haas wrote:
> I'm pretty sure we've got latent memory-ordering risks in our existing
> code which we just haven't detected and fixed yet. Consider, for
> example, this exciting code from GetNewTransactionId:
>
> myproc->subxids.xids[nxids] = xid;
> mypgxact->nxids = nxids + 1;
>
> I don't believe that's technically safe even on an architecture like
> x86, because the compiler could decide to reorder those assignments.
> Of course there is probably no reason to do so, and even if it does
> you'd have to get really unlucky to see a user-visible failure, and if
> you did you'd probably misguess the cause.

You're probably right. Note that it's not even just the compiler that
might reorder them, the CPU/cache subsystem/memory bus all play their
part in memory reordering. x86 is pretty forgiving, which is why it
works.

I found this to be a really good explanation of all the things that can
go wrong with memory ordering. It also explains why, in the long run,
memory barriers are not optimal.

http://herbsutter.com/2013/02/11/atomic-weapons-the-c-memory-model-and-modern-hardware/

That talk discusses how the hardware world is converging on SC [1] as
the memory model to use. And C11/C++11 atomics will implement this for
the programmer. With these you can actually make guarentees. For
example, by marking mypgxact->nxids as an atomic type the compiler will
emit all the necessary markings to let the CPU know what you want, so
everything works the way you expect it to. Even on arcane
architechtures. No explicit barriers needed.

Unfortunatly, it won't help on compilers that don't support it.

[1] http://en.wikipedia.org/wiki/Sequential_consistency

There are places where you put code in and verify it does what you
want. With this one you can put test programs in and it can tell you
all possibly results due to memory reordering.

http://svr-pes20-cppmem.cl.cam.ac.uk/cppmem/help.html

Have a nice day,
--
Martijn van Oosterhout <kleptog(at)svana(dot)org> http://svana.org/kleptog/
> He who writes carelessly confesses thereby at the very outset that he does
> not attach much importance to his own thoughts.
-- Arthur Schopenhauer

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Martijn van Oosterhout 2013-07-16 21:07:27 Re: Proposal - Support for National Characters functionality
Previous Message Kevin Grittner 2013-07-16 20:22:43 Re: Differences in WHERE clause of SELECT