From b15e5f486f84ad4aefe3c5282cdd2c535b9966b1 Mon Sep 17 00:00:00 2001
From: Andres Freund <andres@anarazel.de>
Date: Fri, 10 Oct 2014 13:02:17 +0200
Subject: [PATCH 3/4] Don't use a spinlock while manipulating a lwlock's
 releaseOK state.

Possibly it's relevant for concurrency because it increases the number
of spinlock acquisition by a significant factor. And it's not too hard
to avoid.
---
 src/backend/storage/lmgr/lwlock.c | 53 ++++++++++++++++++---------------------
 1 file changed, 24 insertions(+), 29 deletions(-)

diff --git a/src/backend/storage/lmgr/lwlock.c b/src/backend/storage/lmgr/lwlock.c
index b10c121..2246fc3 100644
--- a/src/backend/storage/lmgr/lwlock.c
+++ b/src/backend/storage/lmgr/lwlock.c
@@ -156,6 +156,9 @@ static LWLockTranche MainLWLockTranche;
  */
 #define MAX_SIMUL_LWLOCKS	200
 
+/* force boolean variable `var' to be accessed once, be it for write or read */
+#define BOOL_ACCESS_ONCE(var)	(*((volatile bool *)&(var)))
+
 typedef struct LWLockHandle
 {
 	LWLock *lock;
@@ -710,27 +713,27 @@ LWLockWakeup(LWLock *lock, LWLockMode released_mode)
 
 	dlist_init(&wakeup);
 
-	/* remove the to-be-awakened PGPROCs from the queue */
-	releaseOK = true;
-
-	/* Acquire mutex.  Time spent holding mutex should be short! */
-#ifdef LWLOCK_STATS
-	lwstats->spin_delay_count += SpinLockAcquire(&lock->mutex);
-#else
-	SpinLockAcquire(&lock->mutex);
-#endif
-
 	/*
 	 * We're still waiting for backends to get scheduled, don't wake them up
 	 * again.
 	 */
-	if (!lock->releaseOK)
+	pg_read_barrier(); /* pairs with nwaiters-- */
+	if (!BOOL_ACCESS_ONCE(lock->releaseOK))
 	{
-		SpinLockRelease(&lock->mutex);
 		LOG_LWDEBUG("LWLockRelease", lock, "skipping due to releaseOK");
 		return;
 	}
 
+	/* Acquire mutex.  Time spent holding mutex should be short! */
+#ifdef LWLOCK_STATS
+	lwstats->spin_delay_count += SpinLockAcquire(&lock->mutex);
+#else
+	SpinLockAcquire(&lock->mutex);
+#endif
+
+	/* remove the to-be-awakened PGPROCs from the queue */
+	releaseOK = true;
+
 	dlist_foreach_modify(iter, &lock->waiters)
 	{
 		PGPROC *waiter = dlist_container(PGPROC, lwWaitLink, iter.cur);
@@ -762,8 +765,9 @@ LWLockWakeup(LWLock *lock, LWLockMode released_mode)
 		if(waiter->lwWaitMode == LW_EXCLUSIVE)
 			break;
 	}
-	lock->releaseOK = releaseOK;
 
+	/* The SpinLockRelease() below provides barrier semantics */
+	BOOL_ACCESS_ONCE(lock->releaseOK) = releaseOK;
 
 	/* We are done updating shared state of the lock queue. */
 	SpinLockRelease(&lock->mutex);
@@ -1012,17 +1016,13 @@ LWLockAcquireCommon(LWLock *lock, LWLockMode mode, uint64 *valptr, uint64 val)
 						break;
 					extraWaits++;
 				}
+
 				/*
 				 * Reset releaseOk - if somebody woke us they'll have set it
-				 * to false.
+				 * to false. No need for a barrier here - we got the lock and
+				 * will perform wakeup if necessary.
 				 */
-#ifdef LWLOCK_STATS
-				lwstats->spin_delay_count += SpinLockAcquire(&lock->mutex);
-#else
-				SpinLockAcquire(&lock->mutex);
-#endif
-				lock->releaseOK = true;
-				SpinLockRelease(&lock->mutex);
+				BOOL_ACCESS_ONCE(lock->releaseOK) = true;
 			}
 			break;
 		}
@@ -1062,6 +1062,9 @@ LWLockAcquireCommon(LWLock *lock, LWLockMode mode, uint64 *valptr, uint64 val)
 			extraWaits++;
 		}
 
+		/* The atomic subtraction provides the barrier */
+		BOOL_ACCESS_ONCE(lock->releaseOK) = true;
+
 		/* not waiting anymore */
 		pg_atomic_fetch_sub_u32(&lock->nwaiters, 1);
 
@@ -1070,14 +1073,6 @@ LWLockAcquireCommon(LWLock *lock, LWLockMode mode, uint64 *valptr, uint64 val)
 		LOG_LWDEBUG("LWLockAcquire", lock, "awakened");
 
 		/* Now loop back and try to acquire lock again. */
-#ifdef LWLOCK_STATS
-		lwstats->spin_delay_count += SpinLockAcquire(&lock->mutex);
-#else
-		SpinLockAcquire(&lock->mutex);
-#endif
-		lock->releaseOK = true;
-		SpinLockRelease(&lock->mutex);
-
 		result = false;
 	}
 
-- 
1.8.3.251.g1462b67

