diff options
author | Andreas Baumann <abaumann@yahoo.com> | 2009-03-30 18:22:58 +0200 |
---|---|---|
committer | Andreas Baumann <abaumann@yahoo.com> | 2009-03-30 18:22:58 +0200 |
commit | 7bf88dd4cdba8e069384942581e92213316d59b0 (patch) | |
tree | d22ab613b5f3bf695406f5365f9f49602599746e /src | |
parent | f296127efcef4a8c120af41d7edacc0a0deaff78 (diff) | |
download | wolfbones-7bf88dd4cdba8e069384942581e92213316d59b0.tar.gz wolfbones-7bf88dd4cdba8e069384942581e92213316d59b0.tar.bz2 |
thread-safe version of strerror_r now, still missing tests
Diffstat (limited to 'src')
-rw-r--r-- | src/port/string.c | 17 |
1 files changed, 14 insertions, 3 deletions
diff --git a/src/port/string.c b/src/port/string.c index da0dfce..83b1daa 100644 --- a/src/port/string.c +++ b/src/port/string.c @@ -44,16 +44,25 @@ char *wolf_port_strdup( const char *s ) { #if !defined HAVE_STRERROR_R || defined TEST_STRERROR_R #include "port/stdio.h" /* for snprintf */ +#include "port/stdbool.h" /* for bool */ #include <sys/types.h> /* for size_t */ #include <errno.h> /* for errno */ +#include "mutex.h" /* for mutexes */ + +static bool mutex_initialized = false; +static wolf_mutex_t mutex; -/* FIXME: this version is NOT thread and signal safe! But we have to port - * mutexes first - */ int wolf_port_strerror_r( int num, char *buf, size_t buflen ) { int safe_errno = errno; const char *msg; + /* critical section as strerror is not thread-safe */ + if( !mutex_initialized ) { + wolf_mutex_init( &mutex ); + mutex_initialized = true; + } + wolf_mutex_lock( &mutex ); + msg = strerror( num ); /* TODO: can we detect illegal error numbers? For sure on Linux NULL * is never returned and a strcmp with prefix "Unknown error" or similar @@ -64,11 +73,13 @@ int wolf_port_strerror_r( int num, char *buf, size_t buflen ) { /* Linux returns an empty string in this case, why? */ (void)snprintf( buf, buflen, "Unknown error %d", num ); errno = EINVAL; + wolf_mutex_unlock( &mutex ); return -1; } strncpy( buf, msg, buflen-1 ); errno = safe_errno; + wolf_mutex_unlock( &mutex ); return 0; } |