[LTP] [PATCH v2 2/4] lib/test.h: create macro to declare init/cleanup functions

Alexey Kodanev
2014-02-11T09:48:17Z

Use thread-safe versions of tst_res's functions if tests linked
with pthread library.

Signed-off-by: Alexey Kodanev <alexey.kodanev@oracle.com>
---
include/test.h | 31 +++++++++++++++++++++++++++++++
1 files changed, 31 insertions(+), 0 deletions(-)

diff --git a/include/test.h b/include/test.h
index 29824d3..e3de278 100644
--- a/include/test.h
+++ b/include/test.h
@@ -1,6 +1,7 @@
/*
* Copyright (c) 2000 Silicon Graphics, Inc. All Rights Reserved.
* Copyright (c) 2009-2013 Cyril Hrubis chrubis@suse.cz
+ * Copyright (c) 2014 Oracle and/or its affiliates. All Rights Reserved.
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of version 2 of the GNU General Public License as
@@ -114,6 +115,36 @@
extern int Forker_pids[];
extern int Forker_npids;

+/*
+ * Macro to use for making functions called only once in
+ * multi-threaded tests such as init or cleanup function.
+ * The first call to @name_fn function by any thread shall
+ * call the @exec_fn. Subsequent calls shall not call @exec_fn.
+ * *_fn functions must not take any arguments.
+ */
+#define DECLARE_ONCE_FN(name_fn, exec_fn) \
+ static pthread_once_t name_fn##_ex = PTHREAD_ONCE_INIT; \
+ void name_fn(void) \
+ { \
+ pthread_once(&name_fn##_ex, exec_fn); \
+ }
+
+/*
+ * Will be defined if test is compiled with -pthread.
+ * Then we can use thread-safe versions.
+ */
+#ifdef _REENTRANT
+#define tst_res tst_res_r
+#define tst_resm tst_resm_r
+#define tst_resm_hexd tst_resm_hexd_r
+#define tst_brk tst_brk_r
+#define tst_brkm tst_brkm_r
+#define tst_require_root tst_require_root_r
+#define tst_flush tst_flush_r
+#define tst_exit tst_exit_r
+#define tst_environ tst_environ_r
+#endif
+
/* lib/tst_res.c */
const char *strttype(int ttype);
void tst_res(int ttype, char *fname, char *arg_fmt, ...)
--
1.7.1