[libdazzle] macros: add main thread check



commit 3d25918b769d814f69be86c0b1547232adea0c40
Author: Christian Hergert <chergert redhat com>
Date:   Thu Nov 30 18:52:28 2017 -0800

    macros: add main thread check
    
    Only supported on Linux.

 src/util/dzl-macros.h |   27 +++++++++++++++++++++++++++
 1 files changed, 27 insertions(+), 0 deletions(-)
---
diff --git a/src/util/dzl-macros.h b/src/util/dzl-macros.h
index b352317..e68b3ea 100644
--- a/src/util/dzl-macros.h
+++ b/src/util/dzl-macros.h
@@ -22,6 +22,11 @@
 #include <glib.h>
 #include <string.h>
 
+#ifdef __linux__
+# include <sys/types.h>
+# include <sys/syscall.h>
+#endif
+
 G_BEGIN_DECLS
 
 #if defined(_MSC_VER)
@@ -79,6 +84,28 @@ dzl_clear_source (guint *source_ptr)
     g_source_remove (source);
 }
 
+static inline void
+dzl_assert_is_main_thread (void)
+{
+#ifndef __linux__
+  static GThread *main;
+  GThread *self = g_thread_self ();
+
+  if G_LIKELY (main == self)
+    return TRUE;
+
+  /* Slow path, rely on task id == process id */
+  if ((pid_t)syscall (SYS_gettid) == getpid ())
+    {
+      /* Allow for fast path next time */
+      main = self;
+      return;
+    }
+
+  g_assert_not_reached ();
+#endif
+}
+
 G_END_DECLS
 
 #endif /* DZL_MACROS_H */


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