From owner-freebsd-threads@FreeBSD.ORG Thu Sep 23 21:50:03 2010 Return-Path: Delivered-To: freebsd-threads@hub.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id 0266E106566C for ; Thu, 23 Sep 2010 21:50:03 +0000 (UTC) (envelope-from gnats@FreeBSD.org) Received: from freefall.freebsd.org (freefall.freebsd.org [IPv6:2001:4f8:fff6::28]) by mx1.freebsd.org (Postfix) with ESMTP id CB1F58FC19 for ; Thu, 23 Sep 2010 21:50:02 +0000 (UTC) Received: from freefall.freebsd.org (localhost [127.0.0.1]) by freefall.freebsd.org (8.14.4/8.14.4) with ESMTP id o8NLo2g3079385 for ; Thu, 23 Sep 2010 21:50:02 GMT (envelope-from gnats@freefall.freebsd.org) Received: (from gnats@localhost) by freefall.freebsd.org (8.14.4/8.14.4/Submit) id o8NLo2f5079384; Thu, 23 Sep 2010 21:50:02 GMT (envelope-from gnats) Date: Thu, 23 Sep 2010 21:50:02 GMT Message-Id: <201009232150.o8NLo2f5079384@freefall.freebsd.org> To: freebsd-threads@FreeBSD.org From: John Baldwin Cc: Subject: Re: threads/150889: PTHREAD_MUTEX_INITIALIZER + pthread_mutex_destroy() == EINVAL X-BeenThere: freebsd-threads@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list Reply-To: John Baldwin List-Id: Threading on FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 23 Sep 2010 21:50:03 -0000 The following reply was made to PR threads/150889; it has been noted by GNATS. From: John Baldwin To: freebsd-threads@freebsd.org Cc: Jilles Tjoelker , Christopher Faylor , freebsd-gnats-submit@freebsd.org Subject: Re: threads/150889: PTHREAD_MUTEX_INITIALIZER + pthread_mutex_destroy() == EINVAL Date: Thu, 23 Sep 2010 17:41:07 -0400 On Thursday, September 23, 2010 5:07:46 pm Jilles Tjoelker wrote: > On Thu, Sep 23, 2010 at 03:41:51PM -0400, Christopher Faylor wrote: > > I don't see how this represents buggy code. It should be possible to > > destroy a mutex which is allocated statically. Currently, if a mutex is > > assigned to PTHREAD_MUTEX_INITIALIZER and then used once, it can be > > successfully destroyed. It is only receive an EINVAL when there has > > been no intervening call to any mutex function. I don't think that a > > PTHREAD_MUTEX_INITIALIZER using program should have to check for that. > > One may want to destroy a mutex to help memory leak checkers and detect > bugs, and then this is indeed a problem. > > > However, regardless, this is still a bug in pthread_mutex_destroy right? > > It is inconsistent at best. > > It seems best to make the proposed change. This will allow > pthread_mutex_destroy() on a destroyed mutex to succeed (which used to > return EINVAL), but pthread_mutex_lock() already succeeded as well > (initializing the mutex in the process). Hmm, I think that POSIX actually require these to fail (ideally with EBUSY rather than EINVAL). Presumably pthread_mutex_destroy() needs to mark mutexes with a value different from PTHREAD_MUTEX_INITIALIZER when it destroys them (similar to MTX_DEAD in the kernel). This is actually very useful behavior for catching bugs and we should catch that. We probably should make pthread_mutex_destroy() not fail but do whatever is sensible for a mutex initialized statically in that case however. > If/when pthread_mutex_t is made a struct, this can be revisited, and > most likely the destroyed and PTHREAD_MUTEX_INITIALIZER states will be > different (PTHREAD_MUTEX_INITIALIZER will likely be a normal state that > does not need special initialization to use). I would argue that they should already be different states. I'm surprised our pthread_mutex_destroy() is that broken. -- John Baldwin