pthread_mutex_trylock return value on recursion

Konstantin Belousov kostikbel at gmail.com
Wed Jun 29 05:53:13 UTC 2016


On Tue, Jun 28, 2016 at 12:38:33PM -0700, Jeremy Allison wrote:
> On Sun, Jun 26, 2016 at 03:40:05PM +0300, Konstantin Belousov wrote:
> > Hi.
> > In the lib/tdb/common/mutex.c, the tdb_runtime_check_for_robust_mutexes()
> > function, (AFAIU) which tests the implementation of shared robust
> > mutexes for suitability, has the following fragment:
> > https://github.com/samba-team/samba/blob/master/lib/tdb/common/mutex.c#L928
> > 
> > 	ret = pthread_mutex_trylock(m);
> > 	if (ret != EDEADLK) {
> > 
> > The preconditions are that the mutex was locked with EOWNERDEAD result,
> > so the current thread already owns the m' lock, and the call I cited
> > attempts to recursively lock the owned mutex.
> > 
> > According to POSIX, pthread_mutex_trylock() must always return EBUSY in
> > case the lock cannot be granted.
> > EDEADLK is only allowed for blocking
> > pthread_mutex_lock().  Glibc implementation indeed returns
> > EDEADLK for errorcheck mutexes, but this seems to be non-conformant.
> > 
> > Would you mind to check for both EDEADLK and EBUSY there, or add a
> > configure test and select between EBUSY and EDEADLK at compile time ?
> > According to the comment, the goal of the check is to distinguish
> > whether libpthread is loaded, so either non-zero error checked is good.
> 
> Something like the attached ?
Sure, exactly.  Thank you for looking.

> 
> What platform are you testing on so we know how you found this ?
FreeBSD 11 (not yet released).

> 
> Jeremy.

> From 370bfb85c621d78c68f3834496c4c64fc93a2f49 Mon Sep 17 00:00:00 2001
> From: Jeremy Allison <jra at samba.org>
> Date: Tue, 28 Jun 2016 12:26:40 -0700
> Subject: [PATCH] s3: tdb: On some platforms pthread_mutex_trylock() returns
>  EBUSY not EDEADLK.
> 
> Signed-off-by: Jeremy Allison <jra at samba.org>
> ---
>  lib/tdb/common/mutex.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/lib/tdb/common/mutex.c b/lib/tdb/common/mutex.c
> index c212842..280dec1 100644
> --- a/lib/tdb/common/mutex.c
> +++ b/lib/tdb/common/mutex.c
> @@ -925,7 +925,7 @@ _PUBLIC_ bool tdb_runtime_check_for_robust_mutexes(void)
>  	}
>  
>  	ret = pthread_mutex_trylock(m);
> -	if (ret != EDEADLK) {
> +	if (ret != EDEADLK && ret != EBUSY) {
>  		pthread_mutex_unlock(m);
>  		goto cleanup;
>  	}
> -- 
> 2.8.0.rc3.226.g39d4020
> 

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 819 bytes
Desc: not available
URL: <http://lists.samba.org/pipermail/samba-technical/attachments/20160629/f4de1841/signature.sig>


More information about the samba-technical mailing list