From mboxrd@z Thu Jan 1 00:00:00 1970 From: Ross Johnson To: Rotem Shacham Cc: Posix Mailing list Subject: Re: dmutex Date: Tue, 07 Aug 2001 20:26:00 -0000 Message-id: <3B70B14C.EB5C973D@ise.canberra.edu.au> References: X-SW-Source: 2001/msg00106.html Rotem Shacham wrote: > > Hi, > Saw a reference in an article about pthreads to a dmutex (debug mutex) that > can (for debug reasons) keep track of its owning thread. > > Is the above implemented ? > You can set the mutex "type" attribute to PTHREAD_MUTEX_ERRORCHECK via pthread_mutexattr_settype(). Then, once you're satisfied that your program is working correctly you can switch to using the default type or PTHREAD_MUTEX_NORMAL specifically, in order to get faster mutexes. pthread_mutexattr_settype (pthread_mutexattr_t * attr, int type) /* * ------------------------------------------------------ * * DOCPUBLIC * The pthread_mutexattr_settype() and * pthread_mutexattr_gettype() functions respectively set and * get the mutex type attribute. This attribute is set in the * type parameter to these functions. * * PARAMETERS * attr * pointer to an instance of pthread_mutexattr_t * * type * must be one of: * * PTHREAD_MUTEX_NORMAL * * PTHREAD_MUTEX_ERRORCHECK * * PTHREAD_MUTEX_RECURSIVE * * * DESCRIPTION * The pthread_mutexattr_settype() and * pthread_mutexattr_gettype() functions respectively set and * get the mutex type attribute. This attribute is set in the * type parameter to these functions. The default value of the * type attribute is PTHREAD_MUTEX_DEFAULT. * * The type of mutex is contained in the type attribute of the * mutex attributes. Valid mutex types include: * * PTHREAD_MUTEX_NORMAL * This type of mutex does not detect deadlock. A * thread attempting to relock this mutex without * first unlocking it will deadlock. Attempting to * unlock a mutex locked by a different thread * results in undefined behavior. Attempting to * unlock an unlocked mutex results in undefined * behavior. * * PTHREAD_MUTEX_ERRORCHECK * This type of mutex provides error checking. A * thread attempting to relock this mutex without * first unlocking it will return with an error. A * thread attempting to unlock a mutex which another * thread has locked will return with an error. A * thread attempting to unlock an unlocked mutex will * return with an error. * * PTHREAD_MUTEX_DEFAULT * Same as PTHREAD_MUTEX_NORMAL. * * PTHREAD_MUTEX_RECURSIVE * A thread attempting to relock this mutex without * first unlocking it will succeed in locking the * mutex. The relocking deadlock which can occur with * mutexes of type PTHREAD_MUTEX_NORMAL cannot occur * with this type of mutex. Multiple locks of this * mutex require the same number of unlocks to * release the mutex before another thread can * acquire the mutex. A thread attempting to unlock a * mutex which another thread has locked will return * with an error. A thread attempting to unlock an * unlocked mutex will return with an error. This * type of mutex is only supported for mutexes whose * process shared attribute is * PTHREAD_PROCESS_PRIVATE. * * RESULTS * 0 successfully set attribute, * EINVAL 'attr' or 'type' is invalid, * */ Ross