X-Git-Url: https://git.octo.it/?p=rrdtool.git;a=blobdiff_plain;f=src%2Frrd_thread_safe.c;h=112d82cf8662b378c454c86ed40451806d50aa4f;hp=4503932b09b00255679aa7db77bc600b6a3b644b;hb=e1c12d5c14b4a716ea999204cedb13e98466ff15;hpb=554df7224e8b3fec9ec5981812b453c43072f150 diff --git a/src/rrd_thread_safe.c b/src/rrd_thread_safe.c index 4503932..112d82c 100644 --- a/src/rrd_thread_safe.c +++ b/src/rrd_thread_safe.c @@ -1,5 +1,5 @@ /***************************************************************************** - * RRDtool 1.2.4 Copyright by Tobi Oetiker, 1997-2005 + * RRDtool 1.3rc3 Copyright by Tobi Oetiker, 1997-2008 * This file: Copyright 2003 Peter Stamfest * & Tobias Oetiker * Distributed under the GPL @@ -24,45 +24,60 @@ static pthread_once_t context_key_once = PTHREAD_ONCE_INIT; /* Free the thread-specific rrd_context - we might actually use rrd_free_context instead... */ -static void context_destroy_context(void *ctx_) +static void context_destroy_context( + void *ctx_) { struct rrd_context *ctx = ctx_; - if (ctx) rrd_free_context(ctx); + + if (ctx) + rrd_free_context(ctx); } /* Allocate the key */ -static void context_get_key() +static void context_get_key( + void) { pthread_key_create(&context_key, context_destroy_context); } -struct rrd_context *rrd_get_context(void) { +struct rrd_context *rrd_get_context( + void) +{ struct rrd_context *ctx; pthread_once(&context_key_once, context_get_key); ctx = pthread_getspecific(context_key); if (!ctx) { - ctx = rrd_new_context(); - pthread_setspecific(context_key, ctx); - } + ctx = rrd_new_context(); + pthread_setspecific(context_key, ctx); + } return ctx; } #ifdef HAVE_STRERROR_R -const char *rrd_strerror(int err) { +const char *rrd_strerror( + int err) +{ struct rrd_context *ctx = rrd_get_context(); - return strerror_r(err, ctx->lib_errstr, ctx->errlen); + + if (strerror_r(err, ctx->lib_errstr, ctx->errlen)) + return "strerror_r failed. sorry!"; + else + return ctx->lib_errstr; } #else #undef strerror -const char *rrd_strerror(int err) { +const char *rrd_strerror( + int err) +{ static pthread_mutex_t mtx = PTHREAD_MUTEX_INITIALIZER; struct rrd_context *ctx; + ctx = rrd_get_context(); pthread_mutex_lock(&mtx); strncpy(ctx->lib_errstr, strerror(err), ctx->errlen); + ctx->lib_errstr[ctx->errlen] = '\0'; pthread_mutex_unlock(&mtx); return ctx->lib_errstr; } #endif -