pthread_mutex_trylock() should never block

Mike Makonnen mtm at
Thu Jan 29 08:12:20 PST 2004

On Thu, Jan 29, 2004 at 09:06:42AM -0500, Daniel Eischen wrote:
> > 
> > EDEADLK is returned by pthread_mutex_lock() only on error
> > checking mutexes that would otherwise deadlock if they were not error
> > checking. Since pthread_mutex_trylock() must return immediately whether
> > the mutex is error-checking or not, I think for consistency sake it should
> > return EBUSY regardless of what type of mutex it is operating on. Also,
> > the 'ERRORS' section lists EDEADLK as a _possible_ return value only for
> > pthread_mutex_lock().
> I looked at "pthread_mutex_trylock() function shall be equivalent to
> pthread_mutex_lock()" and inferred that it even meant returned
> errors should be equivalent.
> > I'm not a standards expert but I think it is permissable for an
> > implementation to return EDEADLK from pthread_mutex_trylock(). However,
> > I think it's better to always return EBUSY, rather than EDEADLK in
> > some cases and EBUSY in others. 
> Well, anything that relies on pthread_mutex_trylock() != EBUSY
> to mean that the function worked is broken (clearly 0 is the
> only return that means success).  And if the application wants
> to know if the current thread has the mutex locked, then
> returning EBUSY doesn't tell you anything, whereas EDEADLK
> does.  Yeah, I know, applications should keep track of which
> mutexes they have locked!

Agreed :-)

Mike Makonnen  | GPG-KEY:
mtm at | Fingerprint: 00E8 61BC 0D75 7FFB E4D3  6BF1 B239 D010 3215 D418
mtm at FreeBSD.Org| FreeBSD - Unleash the Daemon !

More information about the freebsd-threads mailing list