Re: [PATCH v4] locking/mutex: Prevent lock starvation when spinning is disabled

Peter Zijlstra
2016-08-19T19:34:14Z

Please trim your emails..

On Fri, Aug 19, 2016 at 12:57:06PM -0400, Waiman Long wrote:

> >+static inline bool __mutex_trylock_pending(struct mutex *lock)
> >+{
> >+ return atomic_read(&lock->count)>= 0&&
> >+ atomic_xchg_acquire(&lock->count, -1) == 1;
> >+}
> >+
>
> Maybe you can make a more general __mutex_trylock function that is used in
> all three trylock attempts in the slowpath. For example,
>
> static inline bool __mutex_trylock(struct mutex *lock, bool waiter)
> {
> if (waiter) {
> return atomic_read(&lock->count) >= 0 &&
> atomic_xchg_acquire(&lock->count, -1) == 1;
> } else {
> return !need_yield_to_waiter(lock) &&
> !mutex_is_locked(lock) &&
> ((atomic_xchg_acquire(&lock->count, 0) == 1);
> }
> }

That seems more messy to me..