threads/168317: libthr: posix_mutex_trylock does not work with a PTHREAD_MUTEX_ADAPTIVE_NP thread

Alexis Ballier aballier at gentoo.org
Thu May 24 19:50:09 UTC 2012


>Number:         168317
>Category:       threads
>Synopsis:       libthr: posix_mutex_trylock does not work with a PTHREAD_MUTEX_ADAPTIVE_NP thread
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    freebsd-threads
>State:          open
>Quarter:        
>Keywords:       
>Date-Required:
>Class:          sw-bug
>Submitter-Id:   current-users
>Arrival-Date:   Thu May 24 19:50:08 UTC 2012
>Closed-Date:
>Last-Modified:
>Originator:     Alexis Ballier
>Release:        9.0-RELEASE
>Organization:
Gentoo
>Environment:
FreeBSD fbsd 9.0-RELEASE FreeBSD 9.0-RELEASE #0: Tue Jan  3 07:46:30 UTC 2012     root at farrell.cse.buffalo.edu:/usr/obj/usr/src/sys/GENERIC  amd64
>Description:
I discovered this running glib-2.32.3 testsuite.

If I feed pthread_mutex_trylock with a PTHREAD_MUTEX_ADAPTIVE_NP thread, it returns EINVAL instead of EBUSY if it fails to lock.

Reduced testcase:

# cat mutex.c 
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <pthread.h>

int main() {
        pthread_mutex_t mutex;
        pthread_mutexattr_t attr;
        int status;

        pthread_mutexattr_init (&attr);
        pthread_mutexattr_settype (&attr, PTHREAD_MUTEX_ADAPTIVE_NP);
        status = pthread_mutex_init (&mutex, &attr);
        printf("status = %i\n", status);

        status = pthread_mutex_trylock(&mutex);
        printf("status = %i\n", status);

        status = pthread_mutex_trylock(&mutex);
        printf("status = %i\n", status);

        printf("%s\n", strerror(status));
}

# gcc -pthread mutex.c -o mutex
# ./mutex 
status = 0
status = 0
status = 22
Invalid argument

However, with a PTHREAD_MUTEX_NORMAL thread it is fine:

# cat mutex2.c 
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <pthread.h>

int main() {
        pthread_mutex_t mutex;
        pthread_mutexattr_t attr;
        int status;

        pthread_mutexattr_init (&attr);
        pthread_mutexattr_settype (&attr, PTHREAD_MUTEX_NORMAL);
        status = pthread_mutex_init (&mutex, &attr);
        printf("status = %i\n", status);

        status = pthread_mutex_trylock(&mutex);
        printf("status = %i\n", status);

        status = pthread_mutex_trylock(&mutex);
        printf("status = %i\n", status);

        printf("%s\n", strerror(status));
}

# gcc -pthread mutex2.c -o mutex2
# ./mutex2 
status = 0
status = 0
status = 16
Device busy


Some investigation showed that svn rev 173154 got that right but then svn rev 173173 removed this.
>How-To-Repeat:
Try the above testcases, or run glib-2.32.3 testsuite, or watch bailing out any glib based program using trylock.
>Fix:
I'm attaching a patch against head that sould fix this.

Patch attached with submission follows:

Index: lib/libthr/thread/thr_mutex.c
===================================================================
--- lib/libthr/thread/thr_mutex.c	(revision 235924)
+++ lib/libthr/thread/thr_mutex.c	(working copy)
@@ -538,6 +538,7 @@
 	switch (PMUTEX_TYPE(m->m_flags)) {
 	case PTHREAD_MUTEX_ERRORCHECK:
 	case PTHREAD_MUTEX_NORMAL:
+	case PTHREAD_MUTEX_ADAPTIVE_NP:
 		ret = EBUSY; 
 		break;
 


>Release-Note:
>Audit-Trail:
>Unformatted:


More information about the freebsd-threads mailing list