From 945f3664cd9351d41b8e57a1792e4735274e42d3 Mon Sep 17 00:00:00 2001
From: John Peterson <jw.peterson@inl.gov>
Date: Mon, 4 Jun 2012 08:39:46 -0600
Subject: [PATCH] Adding LIBMESH_TLS in Metis where appropriate.

---
 contrib/metis/GKlib/Makefile     |    3 ++
 contrib/metis/GKlib/error.c      |   41 ++++++++++++++++++++++++++++++-------
 contrib/metis/GKlib/gk_externs.h |   23 ++++++++++++++++----
 contrib/metis/GKlib/memory.c     |   14 +++++++++++-
 4 files changed, 66 insertions(+), 15 deletions(-)

diff --git a/contrib/metis/GKlib/Makefile b/contrib/metis/GKlib/Makefile
index fe00e89..aea7b32 100644
--- a/contrib/metis/GKlib/Makefile
+++ b/contrib/metis/GKlib/Makefile
@@ -1,3 +1,6 @@
+# The location of the mesh library
+LIBMESH_DIR ?= ../../..
+
 include ../../../Make.common
 
 libmesh_INCLUDE += -I. 
diff --git a/contrib/metis/GKlib/error.c b/contrib/metis/GKlib/error.c
index 5b09f9f..5effc55 100644
--- a/contrib/metis/GKlib/error.c
+++ b/contrib/metis/GKlib/error.c
@@ -15,21 +15,42 @@ This file contains functions dealing with error reporting and termination
 
 #include <GKlib.h>
 
+/**
+ * LIBMESH CHANGE: __thread is not portable across all platforms.  We detect
+ * this using configure and set the appropriate value in LIBMESH_TLS, which is
+ * defined in libmesh_config.h
+ */
+#include "libmesh_config.h"
+
 
 /* These are the jmp_buf for the graceful exit in case of severe errors.
    Multiple buffers are defined to allow for recursive invokation. */
 #define MAX_JBUFS 128
-__thread int gk_cur_jbufs=-1;
-__thread jmp_buf gk_jbufs[MAX_JBUFS];
-__thread jmp_buf gk_jbuf;
+
+#ifdef LIBMESH_TLS
+LIBMESH_TLS int gk_cur_jbufs=-1;
+LIBMESH_TLS jmp_buf gk_jbufs[MAX_JBUFS];
+LIBMESH_TLS jmp_buf gk_jbuf;
+#else
+int gk_cur_jbufs=-1;
+jmp_buf gk_jbufs[MAX_JBUFS];
+jmp_buf gk_jbuf;
+#endif
 
 typedef void (*gksighandler_t)(int);
 
 /* These are the holders of the old singal handlers for the trapped signals */
-static __thread gksighandler_t old_SIGMEM_handler;  /* Custom signal */
-static __thread gksighandler_t old_SIGERR_handler;  /* Custom signal */
-static __thread gksighandler_t old_SIGMEM_handlers[MAX_JBUFS];  /* Custom signal */
-static __thread gksighandler_t old_SIGERR_handlers[MAX_JBUFS];  /* Custom signal */
+#ifdef LIBMESH_TLS
+static LIBMESH_TLS gksighandler_t old_SIGMEM_handler;  /* Custom signal */
+static LIBMESH_TLS gksighandler_t old_SIGERR_handler;  /* Custom signal */
+static LIBMESH_TLS gksighandler_t old_SIGMEM_handlers[MAX_JBUFS];  /* Custom signal */
+static LIBMESH_TLS gksighandler_t old_SIGERR_handlers[MAX_JBUFS];  /* Custom signal */
+#else
+static gksighandler_t old_SIGMEM_handler;  /* Custom signal */
+static gksighandler_t old_SIGERR_handler;  /* Custom signal */
+static gksighandler_t old_SIGMEM_handlers[MAX_JBUFS];  /* Custom signal */
+static gksighandler_t old_SIGERR_handlers[MAX_JBUFS];  /* Custom signal */
+#endif
 
 /* The following is used to control if the gk_errexit() will actually abort or not.
    There is always a single copy of this variable */
@@ -178,7 +199,11 @@ char *gk_strerror(int errnum)
   return strerror(errnum);
 #else 
 #ifndef SUNOS
-  static __thread char buf[1024];
+#ifdef LIBMESH_TLS
+  static LIBMESH_TLS char buf[1024];
+#else
+  static char buf[1024];
+#endif
 
   strerror_r(errnum, buf, 1024);
 
diff --git a/contrib/metis/GKlib/gk_externs.h b/contrib/metis/GKlib/gk_externs.h
index c31b3a7..eb3229b 100644
--- a/contrib/metis/GKlib/gk_externs.h
+++ b/contrib/metis/GKlib/gk_externs.h
@@ -10,16 +10,29 @@
 #ifndef _GK_EXTERNS_H_
 #define _GK_EXTERNS_H_
 
+/**
+ * LIBMESH CHANGE: __thread is not portable across all platforms.  We detect
+ * this using configure and set the appropriate value in LIBMESH_TLS, which is
+ * defined in libmesh_config.h
+ */
+#include "libmesh_config.h"
 
 /*************************************************************************
 * Extern variable definition. Hopefully, the __thread makes them thread-safe.
 **************************************************************************/
 #ifndef _GK_ERROR_C_
+
 /* declared in error.c */
-extern __thread int gk_cur_jbufs;
-extern __thread jmp_buf gk_jbufs[];
-extern __thread jmp_buf gk_jbuf;
+#ifdef LIBMESH_TLS
+extern LIBMESH_TLS int gk_cur_jbufs;
+extern LIBMESH_TLS jmp_buf gk_jbufs[];
+extern LIBMESH_TLS jmp_buf gk_jbuf;
+#else
+extern int gk_cur_jbufs;
+extern jmp_buf gk_jbufs[];
+extern jmp_buf gk_jbuf;
+#endif // LIBMESH_TLS
 
-#endif
+#endif // _GK_ERROR_C_
 
-#endif
+#endif // _GK_EXTERNS_H_
diff --git a/contrib/metis/GKlib/memory.c b/contrib/metis/GKlib/memory.c
index 8fc7706..0964326 100644
--- a/contrib/metis/GKlib/memory.c
+++ b/contrib/metis/GKlib/memory.c
@@ -15,9 +15,19 @@ can be used to define other memory allocation routines.
 
 #include <GKlib.h>
 
-/* This is for the global mcore that tracks all heap allocations */
-static __thread gk_mcore_t *gkmcore = NULL;
+/**
+ * LIBMESH CHANGE: __thread is not portable across all platforms.  We detect
+ * this using configure and set the appropriate value in LIBMESH_TLS, which is
+ * defined in libmesh_config.h
+ */
+#include "libmesh_config.h"
 
+/* This is for the global mcore that tracks all heap allocations */
+#ifdef LIBMESH_TLS
+static LIBMESH_TLS gk_mcore_t *gkmcore = NULL;
+#else
+static gk_mcore_t *gkmcore = NULL;
+#endif
 
 /*************************************************************************/
 /*! Define the set of memory allocation routines for each data type */
-- 
1.7.7.3

