diff options
Diffstat (limited to 'src/port/string.c')
-rw-r--r-- | src/port/string.c | 8 |
1 files changed, 8 insertions, 0 deletions
diff --git a/src/port/string.c b/src/port/string.c index 398788c..9c360d0 100644 --- a/src/port/string.c +++ b/src/port/string.c @@ -49,19 +49,23 @@ char *wolf_port_strdup( const char *s ) { #include <errno.h> /* for errno */ #include "threads/mutex.h" /* for mutexes */ +#ifdef HAVE_THREADS static bool mutex_initialized = false; static wolf_mutex_t mutex; +#endif 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 */ +#ifdef HAVE_THREADS if( !mutex_initialized ) { wolf_mutex_init( &mutex ); mutex_initialized = true; } wolf_mutex_lock( &mutex ); +#endif msg = strerror( num ); /* TODO: can we detect illegal error numbers? For sure on Linux NULL @@ -73,13 +77,17 @@ 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; +#ifdef HAVE_THREADS wolf_mutex_unlock( &mutex ); +#endif return -1; } strncpy( buf, msg, buflen-1 ); errno = safe_errno; +#ifdef HAVE_THREADS wolf_mutex_unlock( &mutex ); +#endif return 0; } |