Commit a15c2908 authored by David Goodell's avatar David Goodell
Browse files

[svn-r6638] Add a thread-safe MPIU_Strerror.

This was a long-standing TODO.

No reviewer.
parent 86443474
......@@ -5131,6 +5131,7 @@ fi
# 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)
......
......@@ -1794,9 +1794,17 @@ typedef struct MPICH_Nestinfo {
#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
......
......@@ -15,24 +15,8 @@ int MPID_Abort( struct MPID_Comm *comm, int mpi_errno, int exit_code, const char
/*
* 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()
......
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
......
/* -*- 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
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment