Robert Haas <robertmhaas@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
compilerdoesn'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
shareddata* structure being accessed within the spinlocked critical section.* That fixes it because compilers are
notallowed to re-order accesses* to volatile objects relative to other such accesses.** On platforms with weak
memoryordering, the TAS(), TAS_SPIN(), and* S_UNLOCK() macros must further include hardware-level memory fence*
instructionsto prevent similar re-ordering at the hardware level.* TAS() and TAS_SPIN() must guarantee that loads
andstores issued after* the macro are not executed until the lock has been obtained. Conversely,* S_UNLOCK()
mustguarantee that loads and stores issued before the macro* have been executed before the lock is released.
regards, tom lane