[glib/wip/mutexes: 22/58] Add glib-ctor functionality



commit 9f29daafeb8133611a4ea24f5013ebb8bb623d73
Author: Ryan Lortie <desrt desrt ca>
Date:   Sun Sep 18 02:16:07 2011 -0400

    Add glib-ctor functionality
    
    A pair of macros to define a constructor function (on compilers that
    support such a concept) and to ensure that the function is run exactly
    once (on compilers that lack such support).
    
    Presently only GCC is implemented.

 glib/Makefile.am |    1 +
 glib/glib-ctor.h |   25 +++++++++++++++++++++++++
 2 files changed, 26 insertions(+), 0 deletions(-)
---
diff --git a/glib/Makefile.am b/glib/Makefile.am
index 7f0847d..dd045bc 100644
--- a/glib/Makefile.am
+++ b/glib/Makefile.am
@@ -142,6 +142,7 @@ libglib_2_0_la_SOURCES = 	\
 	gkeyfile.c		\
 	glibintl.h		\
 	glib_trace.h		\
+	glib-ctor.h		\
 	glib-private.h		\
 	glib-private.c		\
 	glist.c			\
diff --git a/glib/glib-ctor.h b/glib/glib-ctor.h
new file mode 100644
index 0000000..12df4dd
--- /dev/null
+++ b/glib/glib-ctor.h
@@ -0,0 +1,25 @@
+#ifndef __GLIB_CTOR_H__
+
+#ifdef __GNUC__
+#define GLIB_CTOR(func) \
+  __attribute__((constructor)) static void func (void)
+#define GLIB_ENSURE_CTOR(func) G_STMT_START { } G_STMT_END
+#else
+/* could be vastly improved... */
+#define GLIB_CTOR(func) \
+  static GMutex   g__##func##_mutex = G_MUTEX_INIT;   \
+  static gboolean g__##func##_initialised;            \
+  static void func (void)
+#define GLIB_ENSURE_CTOR(func) \
+  G_STMT_START {                                      \
+    g_mutex_lock (&g__##func##_mutex);                \
+    if (!g__##func##_initialised)                     \
+      {                                               \
+        g__##func##_initialised = TRUE;               \
+        func ();                                      \
+      }                                               \
+    g_mutex_unlock (&g__##func##_mutex);              \
+  } G_STMT_END
+#endif
+
+#endif /* __GLIB_CTOR_H__ */



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]