[mpich2-commits] r6638 - in mpich2/trunk: . src/include src/util/mem

goodell at mcs.anl.gov goodell at mcs.anl.gov
Mon May 10 21:58:28 CDT 2010


Author: goodell
Date: 2010-05-10 21:58:28 -0500 (Mon, 10 May 2010)
New Revision: 6638

Added:
   mpich2/trunk/src/util/mem/strerror.c
Modified:
   mpich2/trunk/configure.in
   mpich2/trunk/src/include/mpiimpl.h
   mpich2/trunk/src/include/mpiutil.h
   mpich2/trunk/src/util/mem/Makefile.sm
Log:
Add a thread-safe MPIU_Strerror.

This was a long-standing TODO.

No reviewer.

Modified: mpich2/trunk/configure.in
===================================================================
--- mpich2/trunk/configure.in	2010-05-11 02:58:25 UTC (rev 6637)
+++ mpich2/trunk/configure.in	2010-05-11 02:58:28 UTC (rev 6638)
@@ -5131,6 +5131,7 @@
 # We would like to use strerror in the file namepublisher; it is also used
 # in MPIU_Strerror (whose implementation is broken if strerror is not found)
 AC_CHECK_FUNCS(strerror strncasecmp)
+AC_FUNC_STRERROR_R
 
 # Use snprintf if possible when creating messages
 AC_CHECK_FUNCS(snprintf)

Modified: mpich2/trunk/src/include/mpiimpl.h
===================================================================
--- mpich2/trunk/src/include/mpiimpl.h	2010-05-11 02:58:25 UTC (rev 6637)
+++ mpich2/trunk/src/include/mpiimpl.h	2010-05-11 02:58:28 UTC (rev 6638)
@@ -1794,9 +1794,17 @@
 #define MPICH_MAX_NESTINFO 16
 #endif /* MPICH_DEBUG_NESTING */
 
+/* arbitrary, just needed to avoid cleaning up heap allocated memory at thread
+ * destruction time */
+#define MPIU_STRERROR_BUF_SIZE (1024)
+
 typedef struct MPICH_PerThread_t {
     int              nest_count;   /* For layered MPI implementation */
     int              op_errno;     /* For errors in predefined MPI_Ops */
+
+    /* error string storage for MPIU_Strerror */
+    char strerrbuf[MPIU_STRERROR_BUF_SIZE];
+
 #ifdef MPICH_DEBUG_NESTING
     MPICH_Nestinfo_t nestinfo[MPICH_MAX_NESTINFO];
 #endif

Modified: mpich2/trunk/src/include/mpiutil.h
===================================================================
--- mpich2/trunk/src/include/mpiutil.h	2010-05-11 02:58:25 UTC (rev 6637)
+++ mpich2/trunk/src/include/mpiutil.h	2010-05-11 02:58:28 UTC (rev 6638)
@@ -15,24 +15,8 @@
 /*
  * MPIU_Sterror()
  *
- * Thread safe implementation of strerror().  The multi-threaded version 
- * will need to use thread specific storage for the string.
- * This prevents the need for allocation of heap memory each time the 
- * function is called.  Granted, stack memory could be used,
- * but allocation of large strings on the stack in a multi-threaded 
- * environment is not wise since thread stack can be relatively
- * small and a deep nesting of routines that each allocate a reasonably 
- * size error for a message can result in stack overrun.
- */
-#if defined(HAVE_STRERROR)
-#   if (MPICH_THREAD_LEVEL < MPI_THREAD_MULTIPLE || USE_THREAD_IMPL == MPICH_THREAD_IMPL_GLOBAL_MUTEX)
-#       define MPIU_Strerror(errno_) strerror(errno_)
-#   else
-#       error need a thread safe implementation of MPIU_Strerror
-#   endif
-#else
-#   define MPIU_Strerror(errno_) "(strerror() not found)"
-#endif
+ * Thread safe implementation of strerror(), whenever possible. */
+const char *MPIU_Strerror(int errnum);
 
 /*
  * MPIU_Assert()

Modified: mpich2/trunk/src/util/mem/Makefile.sm
===================================================================
--- mpich2/trunk/src/util/mem/Makefile.sm	2010-05-11 02:58:25 UTC (rev 6637)
+++ mpich2/trunk/src/util/mem/Makefile.sm	2010-05-11 02:58:28 UTC (rev 6638)
@@ -1,4 +1,4 @@
-lib${MPILIBNAME}_a_SOURCES = trmem.c handlemem.c safestr.c argstr.c
+lib${MPILIBNAME}_a_SOURCES = trmem.c handlemem.c safestr.c argstr.c strerror.c
 HEADERS = 
 INCLUDES = -I../../include -I${top_srcdir}/src/include
 

Added: mpich2/trunk/src/util/mem/strerror.c
===================================================================
--- mpich2/trunk/src/util/mem/strerror.c	                        (rev 0)
+++ mpich2/trunk/src/util/mem/strerror.c	2010-05-11 02:58:28 UTC (rev 6638)
@@ -0,0 +1,53 @@
+/* -*- Mode: C; c-basic-offset:4 ; indent-tabs-mode:nil ; -*- */
+/*
+ *  (C) 2001 by Argonne National Laboratory.
+ *      See COPYRIGHT in top-level directory.
+ */
+
+/* This would live in safestr.c, but it requires thread-private storage support
+ * from mpiimpl.h and friends.  safestr.c is meant to be able to be used in
+ * different software packages, perhaps someday by moving it to MPL. */
+#include "mpiimpl.h"
+
+#if defined(HAVE_STRERROR_R) && !defined(HAVE_STRERROR_R_DECL)
+int strerror_r(int errnum, char *strerrbuf, size_t buflen);
+#endif
+
+/* ideally, provides a thread-safe version of strerror */
+const char *MPIU_Strerror(int errnum)
+{
+#if defined(HAVE_STRERROR_R)
+    char *buf;
+    MPIU_THREADPRIV_DECL;
+    MPIU_THREADPRIV_GET;
+    buf = MPIU_THREADPRIV_FIELD(strerrbuf);
+#  if defined(STRERROR_R_CHAR_P)
+    /* strerror_r returns char ptr (old GNU-flavor).  Static strings for known
+     * errnums are in returned buf, unknown errnums put a message in buf and
+     * return buf */
+    buf = strerror_r(errnum, buf, MPIU_STRERROR_BUF_SIZE);
+#  else
+    /* strerror_r returns an int */
+    strerror_r(errnum, buf, MPIU_STRERROR_BUF_SIZE);
+#  endif
+    return buf;
+
+#elif defined(HAVE_STRERROR)
+    /* MT - not guaranteed to be thread-safe, but on may platforms it will be
+     * anyway for the most common cases (looking up an error string in a table
+     * of constants).
+     *
+     * Using a mutex here would be an option, but then you need a version
+     * without the mutex to call when interpreting errors from mutex functions
+     * themselves. */
+    return strerror(errnum);
+
+#else
+    /* nowadays this case is most likely to happen because of a configure or
+     * internal header file inclusion bug rather than an actually missing
+     * strerror routine */
+    return "(strerror() unavailable on this platform)"
+
+#endif
+}
+



More information about the mpich2-commits mailing list