[Intel-gfx] [PATCH i-g-t] igt_core: add igt_constructor

Lyude lyude at redhat.com
Wed Dec 21 20:17:21 UTC 2016


This is a simple macro for executing a block of code at the beginning of
intel-gpu-tools, before any tests have been ran. Useful for
initialization of global resources used in IGT libraries.

Signed-off-by: Lyude <lyude at redhat.com>
---
 configure.ac   |  1 +
 lib/igt_core.h | 13 +++++++++++++
 2 files changed, 14 insertions(+)

diff --git a/configure.ac b/configure.ac
index fa31a52..411bfab 100644
--- a/configure.ac
+++ b/configure.ac
@@ -65,6 +65,7 @@ AC_CHECK_TYPES([sighandler_t],[],[],[AC_INCLUDES_DEFAULT
 #include <signal.h>])
 AC_CHECK_FUNCS([swapctl])
 AC_CHECK_FUNCS([asprintf])
+AX_GCC_FUNC_ATTRIBUTE([constructor])
 
 dnl Check for POSIX timers
 AC_CHECK_FUNCS(timer_create, [], [
diff --git a/lib/igt_core.h b/lib/igt_core.h
index 661a634..e76fd5f 100644
--- a/lib/igt_core.h
+++ b/lib/igt_core.h
@@ -281,6 +281,19 @@ void igt_simple_init_parse_opts(int *argc, char **argv,
 	} \
 	static void igt_tokencat(__real_main, __LINE__)(void) \
 
+/**
+ * igt_constructor:
+ *
+ * Convenience macro to run the provided code block when igt first starts,
+ * before any tests have been run. This should be used for parts of the igt
+ * library that require initialization of objects with global context.
+ *
+ * This code block will be executed exactly once.
+ */
+#define igt_constructor \
+	static void __igt_constructor_block(void) __attribute__((constructor)); \
+	static void __igt_constructor_block(void)
+
 __attribute__((format(printf, 1, 2)))
 void igt_skip(const char *f, ...) __attribute__((noreturn));
 __attribute__((format(printf, 5, 6)))
-- 
2.9.3



More information about the Intel-gfx mailing list