Re: [RFC] unify semaphore implementations

From: Paul Mackerras
Date: Sat Apr 30 2005 - 00:14:16 EST


I wrote:

> The only fast path that needs atomic_dec_if_positive is down_trylock.
> You can use atomic_dec for down_trylock instead; the only downside to
> that is that if somebody was holding the semaphore but nobody was
> waiting, the holder will take the slow path when it does the up.

Better still, on machines without ll/sc we can do down_trylock as:

return atomic_read(&sem->count) <= 0
|| atomic_add_negative(-1, &sem->count);

Then we will only take the slow path unnecessarily on up() if another
cpu decrements the count between the test and the atomic_add_negative,
which should be very rare. Doing the test will also avoid doing the
atomic op if the semaphore is already held.

Paul.
-
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/