Re: [PATCH 1/19] MUTEX: Introduce simple mutex implementation

linux
2005-12-16T12:49:33Z

> Now my point about using LL/SC is that:
>
> 1,C,A cmpxchg(0,1) [failed]
> 1,C,A cmpxchg(1,3) [success]
> 3,C,A ...
>
> Can be turned into:
>
> 1,C,A x = LL()
> 1,C,A x |= 2;
> 1,C,A SC(3) [success]
> 3,C,A ...

... which can be turned back into

1,C,A x = load()
1,C,A x' = x | 2;
1,C,A cmpxchg(x,x') [success]
3,C,A ...

which will fail and retry in exactly the same contention cases as the
LL/SC. The only thing that LL gives you that's nice is a hint that
an SC is due very soon and so resisting a cache eviction for a couple
of cycles might be a good idea.

The reason that we tend to do the former is optimism that the lock
won't be held. If that's a bad assumption, make it more pessimistic.

LL/SC can detect double changes during the critical section, but it's
very similar in expressive power to load + CMPXCHG.