From mboxrd@z Thu Jan 1 00:00:00 1970 From: Ian Campbell Subject: [PATCH 2 of 7] libxc: allow caller to specify no re-entrancy protection when opening the interface Date: Thu, 18 Nov 2010 10:50:34 +0000 Message-ID: <3de336d9c11335920388.1290077434@zakaz.uk.xensource.com> References: Mime-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Return-path: In-Reply-To: List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Sender: xen-devel-bounces@lists.xensource.com Errors-To: xen-devel-bounces@lists.xensource.com To: xen-devel@lists.xensource.com, xen-api@lists.xensource.com Cc: Ian Campbell List-Id: xen-devel@lists.xenproject.org # HG changeset patch # User root@localhost.localdomain # Date 1290077186 18000 # Node ID 3de336d9c113359203885ca6cf6360e5250dfe82 # Parent 61797efad6c24b6b63b03ac0a4532671a1a9a40a libxc: allow caller to specify no re-entrancy protection when opening the interface Used by language bindings which provide their own re-entrancy which conflicts with pthreads. Signed-off-by: Ian Campbell diff -r 61797efad6c2 -r 3de336d9c113 tools/libxc/xc_private.c --- a/tools/libxc/xc_private.c Thu Nov 18 05:46:26 2010 -0500 +++ b/tools/libxc/xc_private.c Thu Nov 18 05:46:26 2010 -0500 @@ -32,6 +32,7 @@ xc_interface *xc_interface_open(xentooll unsigned open_flags) { xc_interface xch_buf, *xch = &xch_buf; + xch->flags = open_flags; xch->fd = -1; xch->dombuild_logger_file = 0; xc_clear_last_error(xch); @@ -546,30 +547,37 @@ _xc_init_errbuf(void) char *_xc_strerror(xc_interface *xch, int errcode) { + if ( xch->flags & XC_OPENFLAG_NON_REENTRANT ) + { + return strerror(errcode); + } + else + { #define XS_BUFSIZE 32 - char *errbuf; - static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; - char *strerror_str; + char *errbuf; + static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; + char *strerror_str; - pthread_once(&errbuf_pkey_once, _xc_init_errbuf); + pthread_once(&errbuf_pkey_once, _xc_init_errbuf); - errbuf = pthread_getspecific(errbuf_pkey); - if (errbuf == NULL) { - errbuf = malloc(XS_BUFSIZE); - pthread_setspecific(errbuf_pkey, errbuf); + errbuf = pthread_getspecific(errbuf_pkey); + if (errbuf == NULL) { + errbuf = malloc(XS_BUFSIZE); + pthread_setspecific(errbuf_pkey, errbuf); + } + + /* + * Thread-unsafe strerror() is protected by a local mutex. We copy the + * string to a thread-private buffer before releasing the mutex. + */ + pthread_mutex_lock(&mutex); + strerror_str = strerror(errcode); + strncpy(errbuf, strerror_str, XS_BUFSIZE); + errbuf[XS_BUFSIZE-1] = '\0'; + pthread_mutex_unlock(&mutex); + + return errbuf; } - - /* - * Thread-unsafe strerror() is protected by a local mutex. We copy - * the string to a thread-private buffer before releasing the mutex. - */ - pthread_mutex_lock(&mutex); - strerror_str = strerror(errcode); - strncpy(errbuf, strerror_str, XS_BUFSIZE); - errbuf[XS_BUFSIZE-1] = '\0'; - pthread_mutex_unlock(&mutex); - - return errbuf; } void bitmap_64_to_byte(uint8_t *bp, const uint64_t *lp, int nbits) diff -r 61797efad6c2 -r 3de336d9c113 tools/libxc/xc_private.h --- a/tools/libxc/xc_private.h Thu Nov 18 05:46:26 2010 -0500 +++ b/tools/libxc/xc_private.h Thu Nov 18 05:46:26 2010 -0500 @@ -67,6 +67,7 @@ struct xc_interface { int fd; + int flags; xentoollog_logger *error_handler, *error_handler_tofree; xentoollog_logger *dombuild_logger, *dombuild_logger_tofree; struct xc_error last_error; /* for xc_get_last_error */ diff -r 61797efad6c2 -r 3de336d9c113 tools/libxc/xenctrl.h --- a/tools/libxc/xenctrl.h Thu Nov 18 05:46:26 2010 -0500 +++ b/tools/libxc/xenctrl.h Thu Nov 18 05:46:26 2010 -0500 @@ -132,8 +132,17 @@ xc_interface *xc_interface_open(xentooll * if dombuild_logger=NULL, will log to a file */ +/* + * Note: if XC_OPENFLAG_NON_REENTRANT is passed then libxc must not be + * called reentrantly and the calling application is responsible for + * providing mutual exclusion surrounding all libxc calls itself. + * + * In particular xc_{get,clear}_last_error only remain valid for the + * duration of the critical section containing the call which failed. + */ enum xc_open_flags { - XC_OPENFLAG_DUMMY = 01, /* do not actually open a xenctrl interface */ + XC_OPENFLAG_DUMMY = 1<<0, /* do not actually open a xenctrl interface */ + XC_OPENFLAG_NON_REENTRANT = 1<<1, /* assume library is only every called from a single thread */ }; /**