Re: [RFC] unify semaphore implementations

Paul Mackerras
2005-04-30T01:45:04Z

Trond Myklebust writes:

> The PPC implementation would be hard to port to x86, since it relies on
> the load-linked/store-conditional stuff to provide a fast primitive for
> atomic_dec_if_positive().

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. Or
you can use cmpxchg for down_trylock. I believe that down_trylock is
used much less than down and down_interruptible, so it shouldn't
matter if down_trylock is a few nanoseconds slower than down.

> The only way I found to implement that on x86 was to use cmpxchg. On my
> machine, therefore, a spinlock-based semaphore implementation turns out
> to be at least as fast for the "fast" path (and is naturally much more
> efficient for the slow path).

What is "your machine"? Is a single cmpxchg really slower than
locking and unlocking a spinlock? If so, by how much?

Paul.