Bug 168317 - [libthr] [patch] posix_mutex_trylock does not work with a PTHREAD_MUTEX_ADAPTIVE_NP thread
Summary: [libthr] [patch] posix_mutex_trylock does not work with a PTHREAD_MUTEX_ADAPT...
Status: Closed FIXED
Alias: None
Product: Base System
Classification: Unclassified
Component: threads (show other bugs)
Version: 9.0-RELEASE
Hardware: Any Any
: Normal Affects Only Me
Assignee: freebsd-threads (Nobody)
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2012-05-24 20:50 UTC by Alexis Ballier
Modified: 2017-03-28 09:48 UTC (History)
0 users

See Also:


Attachments
file.diff (385 bytes, patch)
2012-05-24 20:50 UTC, Alexis Ballier
no flags Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Alexis Ballier 2012-05-24 20:50:08 UTC
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.

Fix: I'm attaching a patch against head that sould fix this.

Patch attached with submission follows:
How-To-Repeat: Try the above testcases, or run glib-2.32.3 testsuite, or watch bailing out any glib based program using trylock.
Comment 1 mezz.freebsd 2012-05-26 05:15:44 UTC
Thanks for find it! We have been using this patch to get the glib works.

http://www.marcuscom.com:8080/cgi-bin/cvsweb.cgi/ports-experimental/devel/glib20/files/patch-glib_gthread-posix.c

Cheers,
Mezz


-- 
mezz.freebsd@gmail.com - mezz@FreeBSD.org
FreeBSD GNOME Team
http://www.FreeBSD.org/gnome/ - gnome@FreeBSD.org
Comment 2 dfilter service freebsd_committer freebsd_triage 2012-05-27 02:25:00 UTC
Author: davidxu
Date: Sun May 27 01:24:51 2012
New Revision: 236135
URL: http://svn.freebsd.org/changeset/base/236135

Log:
  Return EBUSY for PTHREAD_MUTEX_ADAPTIVE_NP too when the mutex could not
  be acquired.
  
  PR:	168317
  MFC after:	3 days

Modified:
  head/lib/libthr/thread/thr_mutex.c

Modified: head/lib/libthr/thread/thr_mutex.c
==============================================================================
--- head/lib/libthr/thread/thr_mutex.c	Sun May 27 01:24:08 2012	(r236134)
+++ head/lib/libthr/thread/thr_mutex.c	Sun May 27 01:24:51 2012	(r236135)
@@ -538,6 +538,7 @@ mutex_self_trylock(struct pthread_mutex 
 	switch (PMUTEX_TYPE(m->m_flags)) {
 	case PTHREAD_MUTEX_ERRORCHECK:
 	case PTHREAD_MUTEX_NORMAL:
+	case PTHREAD_MUTEX_ADAPTIVE_NP:
 		ret = EBUSY; 
 		break;
 
_______________________________________________
svn-src-all@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/svn-src-all
To unsubscribe, send any mail to "svn-src-all-unsubscribe@freebsd.org"
Comment 3 dfilter service freebsd_committer freebsd_triage 2012-05-30 02:53:08 UTC
Author: davidxu
Date: Wed May 30 01:52:53 2012
New Revision: 236275
URL: http://svn.freebsd.org/changeset/base/236275

Log:
  MFC r236135:
  
  Return EBUSY for PTHREAD_MUTEX_ADAPTIVE_NP too when the mutex could not
  be acquired.
  
  PR:	168317

Modified:
  stable/9/lib/libthr/thread/thr_mutex.c
Directory Properties:
  stable/9/lib/libthr/   (props changed)

Modified: stable/9/lib/libthr/thread/thr_mutex.c
==============================================================================
--- stable/9/lib/libthr/thread/thr_mutex.c	Wed May 30 01:52:01 2012	(r236274)
+++ stable/9/lib/libthr/thread/thr_mutex.c	Wed May 30 01:52:53 2012	(r236275)
@@ -538,6 +538,7 @@ mutex_self_trylock(struct pthread_mutex 
 	switch (PMUTEX_TYPE(m->m_flags)) {
 	case PTHREAD_MUTEX_ERRORCHECK:
 	case PTHREAD_MUTEX_NORMAL:
+	case PTHREAD_MUTEX_ADAPTIVE_NP:
 		ret = EBUSY; 
 		break;
 
_______________________________________________
svn-src-all@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/svn-src-all
To unsubscribe, send any mail to "svn-src-all-unsubscribe@freebsd.org"
Comment 4 dfilter service freebsd_committer freebsd_triage 2012-05-30 02:54:23 UTC
Author: davidxu
Date: Wed May 30 01:54:14 2012
New Revision: 236276
URL: http://svn.freebsd.org/changeset/base/236276

Log:
  MFC r236135:
  
  Return EBUSY for PTHREAD_MUTEX_ADAPTIVE_NP too when the mutex could not
  be acquired.
  
  PR:	168317

Modified:
  stable/8/lib/libthr/thread/thr_mutex.c
Directory Properties:
  stable/8/lib/libthr/   (props changed)

Modified: stable/8/lib/libthr/thread/thr_mutex.c
==============================================================================
--- stable/8/lib/libthr/thread/thr_mutex.c	Wed May 30 01:52:53 2012	(r236275)
+++ stable/8/lib/libthr/thread/thr_mutex.c	Wed May 30 01:54:14 2012	(r236276)
@@ -484,6 +484,7 @@ mutex_self_trylock(struct pthread_mutex 
 	switch (m->m_type) {
 	case PTHREAD_MUTEX_ERRORCHECK:
 	case PTHREAD_MUTEX_NORMAL:
+	case PTHREAD_MUTEX_ADAPTIVE_NP:
 		ret = EBUSY; 
 		break;
 
_______________________________________________
svn-src-all@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/svn-src-all
To unsubscribe, send any mail to "svn-src-all-unsubscribe@freebsd.org"
Comment 5 David Xu freebsd_committer freebsd_triage 2012-05-31 02:42:45 UTC
State Changed
From-To: open->closed

Fixed!
Comment 6 commit-hook freebsd_committer freebsd_triage 2017-03-28 09:48:37 UTC
A commit references this bug:

Author: jbeich
Date: Tue Mar 28 09:48:24 UTC 2017
New revision: 437111
URL: https://svnweb.freebsd.org/changeset/ports/437111

Log:
  devel/glib20: drop adaptive mutex workaround

  PR:		168317

Changes:
  head/devel/glib20/Makefile
  head/devel/glib20/files/patch-glib_gthread-posix.c