public inbox for ltp@lists.linux.it
 help / color / mirror / Atom feed
From: Li Wang <liwang@redhat.com>
To: ltp@lists.linux.it
Subject: [LTP] [PATCH v3 1/2] include: add an exponential backoff macro for function repeat
Date: Sat, 21 Apr 2018 10:15:59 +0800	[thread overview]
Message-ID: <20180421021600.17549-1-liwang@redhat.com> (raw)

Signed-off-by: Li Wang <liwang@redhat.com>
Cc: Richard Palethorpe <rpalethorpe@suse.de>
Cc: Cyril Hrubis <chrubis@suse.cz>
---
 include/tst_common.h | 33 +++++++++++++++++++++++++++++++++
 1 file changed, 33 insertions(+)

diff --git a/include/tst_common.h b/include/tst_common.h
index e4466d5..32adb74 100644
--- a/include/tst_common.h
+++ b/include/tst_common.h
@@ -35,4 +35,37 @@
 #define LTP_ALIGN(x, a)    __LTP_ALIGN_MASK(x, (typeof(x))(a) - 1)
 #define __LTP_ALIGN_MASK(x, mask)  (((x) + (mask)) & ~(mask))
 
+/**
+ * TST_RETRY_FUNC() - Repeatedly retry a function with an increasing delay.
+ * @FUNC - The function which will be retried
+ * @ERET - The value returned from @FUNC on success
+ *
+ * This macro will call @FUNC in a loop with a delay between retries. If @FUNC
+ * returns @ERET then the loop exits. The delay between retries starts at one
+ * micro second and is then doubled each iteration until it exceeds one second
+ * (the total time sleeping will be aproximately one second as well). When the
+ * delay exceeds one second tst_brk() is called.
+ */
+#define TST_RETRY_FUNC(FUNC, ERET) \
+	TST_RETRY_FN_EXP_BACKOFF(FUNC, ERET, 1)
+
+#define TST_RETRY_FN_EXP_BACKOFF(FUNC, ERET, MAX_DELAY)	\
+({	int tst_delay = 1;					\
+	for (;;) {						\
+		typeof(FUNC) ret = FUNC;			\
+		if (ret == ERET)				\
+			break;					\
+		if (tst_delay < MAX_DELAY * 1000000) {		\
+			tst_res(TINFO,				\
+				#FUNC" returned %i, retrying"	\
+				" in %ius", ret, tst_delay);	\
+			usleep(tst_delay);			\
+			tst_delay *= 2;				\
+		} else {					\
+			tst_brk(TBROK, #FUNC" failed");		\
+		}						\
+	}							\
+	ERET;							\
+})
+
 #endif /* TST_COMMON_H__ */
-- 
2.9.3


             reply	other threads:[~2018-04-21  2:15 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-04-21  2:15 Li Wang [this message]
2018-04-23  9:58 ` [LTP] [PATCH v3 1/2] include: add an exponential backoff macro for function repeat Cyril Hrubis
2018-04-23 10:53   ` Li Wang

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20180421021600.17549-1-liwang@redhat.com \
    --to=liwang@redhat.com \
    --cc=ltp@lists.linux.it \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox