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

Поиск
Список
Период
Сортировка
От Martijn van Oosterhout
Тема Re: pg_memory_barrier() doesn't compile, let alone work, for me
Дата
Msg-id 20130716204233.GB28628@svana.org
обсуждение исходный текст
Ответ на Re: pg_memory_barrier() doesn't compile, let alone work, for me  (Robert Haas <robertmhaas@gmail.com>)
Список 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@svana.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

В списке pgsql-hackers по дате отправления:

Предыдущее
От: Kevin Grittner
Дата:
Сообщение: Re: Differences in WHERE clause of SELECT
Следующее
От: Martijn van Oosterhout
Дата:
Сообщение: Re: Proposal - Support for National Characters functionality