From: Ian Campbell Subject: Re: [PATCH 15/15] libxl: New event generation API Date: Tue, 13 Dec 2011 13:59:08 +0000 Message-ID: <1323784748.20077.304.camel@zakaz.uk.xensource.com> References: <1323108621-22654-1-git-send-email-ian.jackson@eu.citrix.com> <1323108621-22654-16-git-send-email-ian.jackson@eu.citrix.com> <20194.22539.574524.956955@mariner.uk.xensource.com> <1323463293.20936.11.camel@dagon.hellion.org.uk> <20197.59428.699013.507183@mariner.uk.xensource.com> <1323692225.20077.183.camel@zakaz.uk.xensource.com> <20197.62462.189827.239656@mariner.uk.xensource.com> <1323697723.20077.212.camel@zakaz.uk.xensource.com> <20198.9737.680162.103418@mariner.uk.xensource.com> 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: Stefano Stabellini Cc: "xen-devel@lists.xensource.com" , Ian Jackson List-Id: xen-devel@lists.xenproject.org On Mon, 2011-12-12 at 19:24 +0000, Stefano Stabellini wrote: > diff --git a/tools/libxl/libxl.c b/tools/libxl/libxl.c > index cd613f7..94bdbba 100644 > --- a/tools/libxl/libxl.c > +++ b/tools/libxl/libxl.c > @@ -63,6 +63,12 @@ int libxl_ctx_alloc(libxl_ctx **pctx, int version, > * only as an initialiser, not as an expression. */ > memcpy(&ctx->lock, &mutex_value, sizeof(ctx->lock)); > > + if(pthread_key_create(&ctx->tls_key, NULL) < 0) { > + LIBXL__LOG_ERRNOVAL(ctx, LIBXL__LOG_ERROR, errno, > + "cannot create tls key"); > + return ERROR_FAIL; You need a corresponding pthread_key_delete in the free function. > [...] > @@ -65,17 +66,41 @@ int libxl__ptr_add(libxl__gc *gc, void *ptr) > return 0; > } > > +libxl__gc *libxl__init_gc(libxl_ctx *ctx) > +{ > + libxl__gc *gc = (libxl__gc *) pthread_getspecific(ctx->tls_key); > + if (gc == NULL) { > + gc = (libxl__gc *) malloc(sizeof(libxl__gc)); > + if (gc == NULL) > + return NULL; > + gc->alloc_maxsize = 0; > + gc->alloc_ptrs = 0; > + gc->owner = ctx; > + gc->nested = 1; > + pthread_setspecific(ctx->tls_key, gc); pthread_setspecific can fail. > [...] > void libxl__free_all(libxl__gc *gc) > { > void *ptr; > int i; > > + gc->nested--; > + if (gc->nested > 0) > + return; > + > for (i = 0; i < gc->alloc_maxsize; i++) { > ptr = gc->alloc_ptrs[i]; > gc->alloc_ptrs[i] = NULL; > free(ptr); > } > free(gc->alloc_ptrs); > + pthread_setspecific(CTX->tls_key, NULL); As above this can also fail. I think this one might be a bit harder to deal with in general. > + free(gc); > } > Ian.