From 1dabdce6993b73408b950cb8c348c4999178b9a0 Mon Sep 17 00:00:00 2001
From: Kyotaro Horiguchi <horiguchi.kyotaro@lab.ntt.co.jp>
Date: Thu, 18 Apr 2019 10:22:49 +0900
Subject: [PATCH 01/10] Define macros to make XLogReadRecord a state machine

To minimize apparent impact on code, use some macros as syntax
sugar. This is a similar stuff with ExecInterpExpr but a bit
different. The most significant difference is that this stuff allows
some functions are leaved midst of their work then continue. Roughly
speaking this is used as the follows.

enum retval
some_func()
{
  static .. internal_variables;

  XLR_SWITCH(INITIAL_STATUS);
  ...
  XLR_LEAVE(STATUS1, RETVAL_CONTINUE);
  ...
  XLR_LEAVE(STATUS2, RETVAL_CONTINUE2);
  ...
  XLR_SWITCH_END();

  XLR_RETURN(RETVAL_FINISH);
}

The caller uses the function as follows:

  while (some_func() != RETVAL_FINISH)
  {
    <do some work>;
  }
---
 src/backend/access/transam/xlogreader.c | 83 +++++++++++++++++++++++++++++++++
 1 file changed, 83 insertions(+)

diff --git a/src/backend/access/transam/xlogreader.c b/src/backend/access/transam/xlogreader.c
index 41dae916b4..69d20e1f2f 100644
--- a/src/backend/access/transam/xlogreader.c
+++ b/src/backend/access/transam/xlogreader.c
@@ -29,6 +29,89 @@
 #include "utils/memutils.h"
 #endif
 
+/*
+ * Use computed-goto-based state dispatch when computed gotos are available.
+ * But use a separate symbol so that it's easy to adjust locally in this file
+ * for development and testing.
+ */
+#ifdef HAVE_COMPUTED_GOTO
+#define XLR_USE_COMPUTED_GOTO
+#endif							/* HAVE_COMPUTED_GOTO */
+
+/*
+ * The state machine functions relies on static local variables. They cannot
+ * be reentered after non-local exit using ereport/elog for consistency. The
+ * assertion macros protect the functions from reenter after non-local exit.
+ */
+#ifdef USE_ASSERT_CHECKING
+#define XLR_REENT_PROTECT_ENTER() \
+	do { Assert(!__xlr_running); __xlr_running = true; } while (0)
+#define XLR_REENT_PROTECT_LEAVE()\
+	do { __xlr_running = false; } while (0)
+#else
+#define XLR_REENT_PROTECT_ENTER()
+#define XLR_REENT_PROTECT_LEAVE()
+#endif
+
+/*
+ * Macros for state dispatch.
+ *
+ * XLR_SWITCH - prologue code for state machine including switch itself.
+ * XLR_CASE - labels the implementation of named state.
+ * XLR_LEAVE - leave the function and return here at the next call.
+ * XLR_RETURN - return from the function and set state to initial state.
+ * XLR_END - just hides the closing brace if not in use.
+ */
+#if defined(XLR_USE_COMPUTED_GOTO)
+#define XLR_SWITCH(name)										\
+	static bool __xlr_running PG_USED_FOR_ASSERTS_ONLY = false; \
+	static void *__xlr_init_state = &&name;								\
+	static void *__xlr_state = &&name;									\
+	do {																\
+		XLR_REENT_PROTECT_ENTER();										\
+		goto *__xlr_state;												\
+		XLR_CASE(name);													\
+	} while (0)
+#define XLR_CASE(name)		name:
+#define XLR_LEAVE(name, code)											\
+	do {																\
+		__xlr_state = (&&name);											\
+		XLR_REENT_PROTECT_LEAVE();										\
+		return (code);													\
+		XLR_CASE(name);													\
+	} while (0)
+#define XLR_RETURN(code)										\
+	do {														\
+		__xlr_state = __xlr_init_state;							\
+		XLR_REENT_PROTECT_LEAVE();								\
+		return (code);											\
+	} while (0)
+#define XLR_SWITCH_END()
+#else							/* !XLR_USE_COMPUTED_GOTO */
+#define XLR_SWITCH(name)												\
+	static bool __xlr_running = false PG_USED_FOR_ASSERTS_ONLY;			\
+	static int __xlr_init_state = name;									\
+	static int __xlr_state = name;										\
+	XLR_REENT_PROTECT_ENTER();											\
+	switch (__xlr_state) {												\
+	XLR_CASE(name)
+#define XLR_CASE(name)		case name:
+#define XLR_LEAVE(name, code)											\
+	do {																\
+		__xlr_state = (name);											\
+		XLR_REENT_PROTECT_LEAVE();										\
+		return (code);													\
+		XLR_CASE(name);													\
+	} while (0)
+#define XLR_RETURN(code)						\
+	do {										\
+		__xlr_state = __xlr_init_state;									\
+		XLR_REENT_PROTECT_LEAVE();										\
+		return (code);													\
+	} while (0)
+#define XLR_SWITCH_END()	}
+#endif							/* XLR_USE_COMPUTED_GOTO */
+
 static bool allocate_recordbuf(XLogReaderState *state, uint32 reclength);
 
 static bool ValidXLogRecordHeader(XLogReaderState *state, XLogRecPtr RecPtr,
-- 
2.16.3

