Bug 24261

Summary: pthread_mutex_lock/pthread_mutex_unlock does not honor PTHREAD_MUTEX_ERRORCHECK
Product: Base System Reporter: nuspl <nuspl>
Component: miscAssignee: Daniel Eischen <deischen>
Status: Closed FIXED    
Severity: Affects Only Me    
Priority: Normal    
Version: Unspecified   
Hardware: Any   
OS: Any   
Attachments:
Description Flags
file.diff none

Description nuspl 2001-01-12 00:20:00 UTC
The pthread_mutex_lock() and pthread_mutex_unlock() routines do not
honor the PTHREAD_MUTEX_ERRORCHECK type.

Fix: Applying the attached patch to 
/usr/src/lib/libc_r/uthread/uthread_mutex.c
adds the needed error checks to the routines.

The patch for mutex_unlock_common() is a quick and dirty fix.  It could
be improved.
How-To-Repeat: #include <sys/types.h>
#include <stdio.h>
#include <pthread.h>
#include <errno.h>

int
main(int argc, char **argv)
{
	int failed = -1;
	int rv;
	pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
	pthread_mutexattr_t attr;

	rv = pthread_mutexattr_init(&attr);
	if (rv != 0) {
		(void)perror("pthread_mutexattr_init");
		return failed;
	}
	
	rv = pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ERRORCHECK);
	if (rv != 0) {
		perror("pthread_mutexattr_settype");
		goto unwind_attr;
	}

	rv = pthread_mutex_init(&mutex, &attr);
	if (rv != 0) {
		perror("pthread_mutex_init");
		goto unwind_attr;
	}

	rv = pthread_mutex_unlock(&mutex);
	if (rv != EPERM) {
		perror("pthread_mutex_unlock");
	}
	
	rv = pthread_mutex_lock(&mutex);
	if (rv != 0) {
		perror("pthread_mutex_lock");
		goto unwind_mutex;
	}

	rv = pthread_mutex_lock(&mutex);
	if (rv == EDEADLK) {
		failed = 0;
	} else {
		(void)fprintf(stderr, "rv=%d\n", rv);
		failed = 1;
	}
	
	(void)fprintf(stderr,
		      "PTHREAD_MUTEX_ERRORCHECK %s\n",
		      (failed ? "failed" : "passed"));

 unwind_mutex:
	(void)pthread_mutex_destroy(&mutex);
 unwind_attr:
	(void)pthread_mutexattr_destroy(&attr);

	return (failed);
}
Comment 1 Mike Heffner freebsd_committer freebsd_triage 2001-06-29 05:39:46 UTC
Responsible Changed
From-To: freebsd-bugs->deischen

Daniel is probably the best person to deal with this.
Comment 2 eischen 2001-06-29 06:21:30 UTC
This problem doesn't exist under 5.0-current.  I'll check -stable
next week, but I don't think it exists there either.

-- 
Dan
Comment 3 Daniel Eischen freebsd_committer freebsd_triage 2001-06-29 12:47:31 UTC
State Changed
From-To: open->closed

This problem doesn't exist under -stable or -current.