From: Ian Jackson Subject: Re: [PATCH RFC v2 09/13] libxl: introduce lock in libxl_ctx Date: Mon, 28 Nov 2011 15:47:04 +0000 Message-ID: <20179.44280.709362.448756@mariner.uk.xensource.com> References: <1319827031-15395-1-git-send-email-ian.jackson@eu.citrix.com> <1319827031-15395-2-git-send-email-ian.jackson@eu.citrix.com> <1319827031-15395-3-git-send-email-ian.jackson@eu.citrix.com> <1319827031-15395-4-git-send-email-ian.jackson@eu.citrix.com> <1319827031-15395-5-git-send-email-ian.jackson@eu.citrix.com> <1319827031-15395-6-git-send-email-ian.jackson@eu.citrix.com> <1319827031-15395-7-git-send-email-ian.jackson@eu.citrix.com> <1319827031-15395-8-git-send-email-ian.jackson@eu.citrix.com> <1319827031-15395-9-git-send-email-ian.jackson@eu.citrix.com> <1319827031-15395-10-git-send-email-ian.jackson@eu.citrix.com> <1320055382.23193.38.camel@zakaz.uk.xensource.com> Mime-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Return-path: In-Reply-To: <1320055382.23193.38.camel@zakaz.uk.xensource.com> List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Sender: xen-devel-bounces@lists.xensource.com Errors-To: xen-devel-bounces@lists.xensource.com To: Ian Campbell Cc: "xen-devel@lists.xensource.com" List-Id: xen-devel@lists.xenproject.org Ian Campbell writes ("Re: [Xen-devel] [PATCH RFC v2 09/13] libxl: introduce lock in libxl_ctx"): > On Fri, 2011-10-28 at 19:37 +0100, Ian Jackson wrote: > > + memcpy(&ctx->lock, &mutex_value, sizeof(ctx->lock)); > > Is this subtly different to > ctx->lock = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP; > in some way I'm missing? I have added a comment about this. (In my own tree; I'll repost the series shortly.) > > + pthread_mutex_t lock; /* protects data structures hanging off the ctx */ > > + /* always use MUTEX_LOCK and MUTEX_UNLOCK to manipulate this */ > > MUTEX is something of an implementation detail (although I admit we are > unlikely to use anything else), perhaps CTX_(UN)LOCK? Renamed. > Perhaps give the variable a less obvious name to help discourage direct > use? I think the comment right next to it ought to be sufficient. > > +#define MUTEX_LOCK do { \ > > + int mutex_r = pthread_mutex_lock(&CTX->lock); \ > > + assert(!mutex_r); \ > > This assert is to catch EINVAL ("the mutex has not been properly > initialized") rather than EDEADLK ("the mutex is already locked by the > calling thread") since we asked for a non-error-checking recursive lock? Yes. > Since it is OK to take this lock recursively then it might be as well to > say so explicitly? > > This is the first lock in libxl so I guess there isn't much of a locking > hierarchy yet. Are there any particular considerations which a caller > must make wrt its own locking? I have added a comment explaining this. No requirements are imposed on libxl's caller. (Other than the reentrancy ones on callbacks.) Ian.