Skip to content

Commit 4971c36

Browse files
committed
Don't let protected variable access to be reordered after spinlock release.
LWLockAcquireWithVar needs to set the protected variable while holding the spinlock. Need to use a volatile pointer to make sure it doesn't get reordered by the compiler. The other functions that accessed the protected variable already got this right. 9.4 only. Earlier releases didn't have this code, and in master, spinlock release acts as a compiler barrier.
1 parent 4dbc760 commit 4971c36

File tree

1 file changed

+3
-2
lines changed

1 file changed

+3
-2
lines changed

src/backend/storage/lmgr/lwlock.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -482,6 +482,7 @@ static inline bool
482482
LWLockAcquireCommon(LWLock *l, LWLockMode mode, uint64 *valptr, uint64 val)
483483
{
484484
volatile LWLock *lock = l;
485+
volatile uint64 *valp = valptr;
485486
PGPROC *proc = MyProc;
486487
bool retry = false;
487488
bool result = true;
@@ -637,8 +638,8 @@ LWLockAcquireCommon(LWLock *l, LWLockMode mode, uint64 *valptr, uint64 val)
637638
}
638639

639640
/* If there's a variable associated with this lock, initialize it */
640-
if (valptr)
641-
*valptr = val;
641+
if (valp)
642+
*valp = val;
642643

643644
/* We are done updating shared state of the lock itself. */
644645
SpinLockRelease(&lock->mutex);

0 commit comments

Comments
 (0)