Robert Haas <robertmh...@gmail.com> writes:
> This discussion seems to miss the fact that there are two levels of
> reordering that can happen.  First, the compiler can move things
> around.  Second, the CPU can move things around.

Right, I think that's exactly the problem with the previous wording of
that comment; it doesn't address the two logical levels involved.
I've rewritten it, see what you think.

 *      Another caution for users of these macros is that it is the caller's
 *      responsibility to ensure that the compiler doesn't re-order accesses
 *      to shared memory to precede the actual lock acquisition, or follow the
 *      lock release.  Typically we handle this by using volatile-qualified
 *      pointers to refer to both the spinlock itself and the shared data
 *      structure being accessed within the spinlocked critical section.
 *      That fixes it because compilers are not allowed to re-order accesses
 *      to volatile objects relative to other such accesses.
 *
 *      On platforms with weak memory ordering, the TAS(), TAS_SPIN(), and
 *      S_UNLOCK() macros must further include hardware-level memory fence
 *      instructions to prevent similar re-ordering at the hardware level.
 *      TAS() and TAS_SPIN() must guarantee that loads and stores issued after
 *      the macro are not executed until the lock has been obtained.  
Conversely,
 *      S_UNLOCK() must guarantee that loads and stores issued before the macro
 *      have been executed before the lock is released.

                        regards, tom lane

-- 
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers

Reply via email to