Re: pthread_mutex_unlock (was Re: sched_yield() makes OpenLDAP slow)

From: Howard Chu
Date: Wed Jan 25 2006 - 20:05:13 EST


Robert Hancock wrote:
Howard Chu wrote:
Kaz's post clearly interprets the POSIX spec differently from you. The policy can decide *which of the waiting threads* gets the mutex, but the releasing thread is totally out of the picture. For good or bad, the current pthread_mutex_unlock() is not POSIX-compliant. Now then, if we're forced to live with that, for efficiency's sake, that's OK, assuming that valid workarounds exist, such as inserting a sched_yield() after the unlock.

http://groups.google.com/group/comp.programming.threads/msg/16c01eac398a1139?hl=en&;


Did you read the rest of this post?

"In any event, all the mutex fairness in the world won't solve the
problem. Consider if this lock/unlock cycle is inside a larger
lock/unlock cycle. Yielding at the unlock or blocking at the lock will
increase the dreadlock over the larger mutex.

Basic "fairness" isn't the issue. Fairness is concerned with which of *multiple waiting threads* gets the mutex, and that is certainly irrelevant here. The issue is that the releasing thread should not be a candidate.

The mutex functions are a core part of the thread specification; they have a fundamental behavior, and the definition says if there are blocked threads waiting on a mutex when it gets unlocked, one of the waiting threads gets the mutex. Which of the waiting threads gets it is unspecified in the core spec. On a system that implements the scheduling option, the scheduling policy specifies which thread. The scheduling policy is an optional feature, it serves only to refine the core functionality. A program written to the basic core specification should not break when run in an environment that implements optional features.

The spec may be mandating a non-optimal behavior, but that's a side-issue - someone should file an objection with the Open Group to get it redefined if it's such a bad idea. But for now, the NPTL implementation is non-conformant.

Standards aren't just academic exercises. They're meant to be useful. If the standard is too thinly specified, is ambiguous, or allows nonsensical behavior, it's not useful and should be fixed at the source, not just ignored and papered over in implementations.

--
-- Howard Chu
Chief Architect, Symas Corp. http://www.symas.com
Director, Highland Sun http://highlandsun.com/hyc
OpenLDAP Core Team http://www.openldap.org/project/

-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/